Completed
Push — master ( d8d66e...fdfd57 )
by Thomas
01:10
created

doorpi.status.status_lib.get()   F

Complexity

Conditions 13

Size

Total Lines 30

Duplication

Lines 0
Ratio 0 %
Metric Value
cc 13
dl 0
loc 30
rs 2.7716

How to fix   Complexity   

Complexity

Complex classes like doorpi.status.status_lib.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
        event_handler = kwargs['DoorPiObject'].event_handler
14
15
        status = {}
16
        for name_requested in kwargs['name']:
17
            if name_requested in 'sources':
18
                status['sources'] = event_handler.sources
19
            if name_requested in 'events':
20
                status['events'] = event_handler.events
21
            if name_requested in 'events_by_source':
22
                status['events_by_source'] = event_handler.events_by_source
23
            if name_requested in 'actions':
24
                status['actions'] = {}
25
                for event in event_handler.actions:
26
                    status['actions'][event] = []
27
                    for action in event_handler.actions[event]:
28
                        status['actions'][event].append(str(action))
29
            if name_requested in 'threads':
30
                status['threads'] = str(event_handler.threads)
31
            if name_requested in 'idle':
32
                status['idle'] = event_handler.idle
33
34
        return status
35
    except Exception as exp:
36
        logger.exception(exp)
37
        return {'Error': 'could not create '+str(__name__)+' object - '+str(exp)}
38
39
def is_active(doorpi_object):
40
    return True if doorpi_object.event_handler else False
41