get()   F
last analyzed

Complexity

Conditions 23

Size

Total Lines 55

Duplication

Lines 0
Ratio 0 %

Importance

Changes 7
Bugs 0 Features 0
Metric Value
cc 23
dl 0
loc 55
rs 3.5378
c 7
b 0
f 0

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

Complexity

Complex classes like get() often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

1
#!/usr/bin/env python
2
# -*- coding: utf-8 -*-
3
4
import logging
5
logger = logging.getLogger(__name__)
6
logger.debug("%s loaded", __name__)
7
8
def get(*args, **kwargs):
0 ignored issues
show
Unused Code introduced by
The argument args seems to be unused.
Loading history...
9
    try:
10
        if len(kwargs['name']) == 0: kwargs['name'] = ['']
11
        if len(kwargs['value']) == 0: kwargs['value'] = ['']
12
13
        sipphone = kwargs['DoorPiObject'].sipphone
14
15
        status = {}
16
        for name_requested in kwargs['name']:
17
            status = {}
18
            sipphone.thread_register('status_thread')
19
20
            if name_requested in 'name':
21
                status['name'] = sipphone.name
22
23
            if name_requested in 'sound_codecs':
24
                status['sound_codecs'] = sipphone.sound_codecs
25
            if name_requested in 'sound_devices':
26
                status['sound_devices'] = sipphone.sound_devices
27
28
            if name_requested in 'sound_enable':
29
                if len(sipphone.sound_codecs) and len(sipphone.sound_devices):
30
                    status['sound_enable'] = True
31
                else:
32
                    status['sound_enable'] = False
33
34
            if name_requested in 'video_codecs':
35
                status['video_codecs'] = sipphone.video_codecs
36
            if name_requested in 'video_devices':
37
                status['video_devices'] = sipphone.video_devices
38
39
            if name_requested in 'video_enable':
40
                if len(sipphone.video_codecs) and len(sipphone.video_devices):
41
                    status['video_enable'] = True
42
                else:
43
                    status['video_enable'] = False
44
45
            if name_requested in 'recorder':
46
                status['has_recorder'] = True if sipphone.recorder else False
47
                if status['has_recorder']:
48
                    status['recorder_filename'] = sipphone.recorder.record_filename
49
                    status['recorder_parsed_filename'] = sipphone.recorder.parsed_record_filename
50
51
            if name_requested in 'player':
52
                status['has_player'] = True if sipphone.player else False
53
                if status['has_player']:
54
                    status['player_filename'] = sipphone.player.player_filename
55
56
            if name_requested in 'current_call':
57
                status['current_call'] = sipphone.current_call_dump
58
59
        return status
60
    except Exception as exp:
61
        logger.exception(exp)
62
        return {'Error': 'could not create '+str(__name__)+' object - '+str(exp)}
63
64
def is_active(doorpi_object):
65
    return True if doorpi_object.sipphone else False
66