load_module_status()   A
last analyzed

Complexity

Conditions 1

Size

Total Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
dl 0
loc 3
rs 10
c 0
b 0
f 0
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
import importlib
9
import json
0 ignored issues
show
Unused Code introduced by Thomas
The import json seems to be unused.
Loading history...
10
11
DEFAULT_MODULE_ATTR = ['__doc__', '__file__', '__name__', '__package__', '__path__', '__version__']
12
13
def check_module_status(module):
14
    module['is_fulfilled'] = False if module['fulfilled_with_one'] else True
15
    for module_name in module['libraries'].keys():
16
        status = {}
17
        try:
18
            package = importlib.import_module(module_name)
19
            content = dir(package)
20
21
            for attr in DEFAULT_MODULE_ATTR:
22
                if attr in content:
23
                    status[attr.replace('__', '')] = getattr(package, attr) or ''
24
                else:
25
                    status[attr.replace('__', '')] = 'unknown'
26
27
            status['installed'] = True
28
            if module['fulfilled_with_one']: module['is_fulfilled'] = True
29
            status['content'] = content
30
31
        except Exception as exp:
32
            status = {'installed': False, 'error': str(exp)}
33
            if not module['fulfilled_with_one']: module['is_fulfilled'] = False
34
35
        finally:
36
            module['libraries'][module_name]['status'] = status
37
38
    return module
39
40
def load_module_status(module_name):
41
    module = importlib.import_module('doorpi.status.requirements_lib.'+module_name).REQUIREMENT
42
    return check_module_status(module)
43
44
REQUIREMENTS_DOORPI = {
45
    'config':           load_module_status('req_config'),
46
    'sipphone':         load_module_status('req_sipphone'),
47
    'event_handler':    load_module_status('req_event_handler'),
48
    'webserver':    load_module_status('req_webserver'),
49
    'keyboard':    load_module_status('req_keyboard'),
50
    'system':    load_module_status('req_system')
51
}
52
53
def get(*args, **kwargs):
0 ignored issues
show
Unused Code introduced by Thomas
The argument args seems to be unused.
Loading history...
54
    try:
55
        if len(kwargs['name']) == 0: kwargs['name'] = ['']
56
        if len(kwargs['value']) == 0: kwargs['value'] = ['']
57
58
        status = {}
59
        for name_requested in kwargs['name']:
60
            for possible_name in REQUIREMENTS_DOORPI.keys():
61
                if name_requested in possible_name:
62
                    status[possible_name] = REQUIREMENTS_DOORPI[possible_name]
63
64
        return status
65
    except Exception as exp:
66
        logger.exception(exp)
67
        return {'Error': 'could not create '+str(__name__)+' object - '+str(exp)}
68
69
def is_active(doorpi_object):
0 ignored issues
show
Unused Code introduced by Thomas
The argument doorpi_object seems to be unused.
Loading history...
70
    return True
71