Test Failed
Push — develop ( 720679...ba0508 )
by Dean
02:45
created

ModuleManager   A

Complexity

Total Complexity 17

Size/Duplication

Total Lines 86
Duplicated Lines 0 %

Test Coverage

Coverage 77.5%

Importance

Changes 0
Metric Value
wmc 17
dl 0
loc 86
ccs 31
cts 40
cp 0.775
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A initialize() 0 3 1
C start() 0 25 7
A discover() 0 14 1
A get() 0 2 1
C construct() 0 32 7
1 1
from plugin.core.message import InterfaceMessages
2
3 1
import logging
4
5
log = logging.getLogger(__name__)
6 1
7 1
8 1
class ModuleManagerMeta(type):
9
    def __getitem__(self, key):
0 ignored issues
show
Coding Style Best Practice introduced by
The first argument of the metaclass method __getitem__ should be named cls.
Loading history...
10
        return self.modules[key]
11 1
12 1
13
class ModuleManager(object):
14 1
    __metaclass__ = ModuleManagerMeta
15
16 1
    modules = {}
17
18 1
    @classmethod
19
    def initialize(cls):
20 1
        cls.modules = dict(cls.construct())
21
22 1
    @classmethod
23 1
    def discover(cls):
24 1
        from plugin.modules.mapper.main import Mapper
25 1
        from plugin.modules.matcher.main import Matcher
26 1
        from plugin.modules.scheduler.main import Scheduler
27
        from plugin.modules.sessions.main import Sessions
28 1
        from plugin.modules.upgrade.main import Upgrade
29
30
        return [
31
            Mapper,
32
            Matcher,
33
            Scheduler,
34
            Sessions,
35
            Upgrade
36 1
        ]
37
38 1
    @classmethod
39 1
    def construct(cls):
40
        if InterfaceMessages.critical:
0 ignored issues
show
Bug introduced by
The Class InterfaceMessages does not seem to have a member named critical.

This check looks for calls to members that are non-existent. These calls will fail.

The member could have been renamed or removed.

Loading history...
41
            log.info('Module construction has been cancelled due to a critical plugin error')
42
            return
43
44 1
        try:
45
            available = cls.discover()
46 1
        except Exception as ex:
0 ignored issues
show
Best Practice introduced by
Catching very general exceptions such as Exception is usually not recommended.

Generally, you would want to handle very specific errors in the exception handler. This ensure that you do not hide other types of errors which should be fixed.

So, unless you specifically plan to handle any error, consider adding a more specific exception.

Loading history...
47 1
            log.error('Unable to import modules: %s', ex, exc_info=True)
48 1
            return
49
50
        # Construct modules
51
        constructed = []
52 1
53
        for module in available:
54 1
            if InterfaceMessages.critical:
0 ignored issues
show
Bug introduced by
The Class InterfaceMessages does not seem to have a member named critical.

This check looks for calls to members that are non-existent. These calls will fail.

The member could have been renamed or removed.

Loading history...
55
                log.info('Module construction has been cancelled due to a critical plugin error')
56
                return
57
58 1
            try:
59
                if module.__key__ is None:
60 1
                    # Automatically set module `__key__` (if one isn't specified)
61 1
                    module.__key__ = module.__class__.__name__.lower()
62 1
63
                yield module.__key__, module()
64 1
65 1
                constructed.append(module.__key__)
66 1
            except Exception as ex:
0 ignored issues
show
Best Practice introduced by
Catching very general exceptions such as Exception is usually not recommended.

Generally, you would want to handle very specific errors in the exception handler. This ensure that you do not hide other types of errors which should be fixed.

So, unless you specifically plan to handle any error, consider adding a more specific exception.

Loading history...
67
                log.warn('Unable to construct module: %r', module)
68 1
69 1
        log.debug('Constructed %d module(s): %s', len(constructed), ', '.join(constructed))
70
71 1
    @classmethod
72
    def start(cls, keys=None):
73
        if InterfaceMessages.critical:
0 ignored issues
show
Bug introduced by
The Class InterfaceMessages does not seem to have a member named critical.

This check looks for calls to members that are non-existent. These calls will fail.

The member could have been renamed or removed.

Loading history...
74
            log.info('Module startup has been cancelled due to a critical plugin error')
75 1
            return
76
77 1
        # Start modules
78
        started = []
79
80
        for key, module in cls.modules.items():
81
            if keys is not None and key not in keys:
82
                continue
83
84
            if InterfaceMessages.critical:
0 ignored issues
show
Bug introduced by
The Class InterfaceMessages does not seem to have a member named critical.

This check looks for calls to members that are non-existent. These calls will fail.

The member could have been renamed or removed.

Loading history...
85
                log.info('Module startup has been cancelled due to a critical plugin error')
86
                return
87
88
            try:
89
                module.start()
90
91
                started.append(key)
92
            except Exception as ex:
0 ignored issues
show
Best Practice introduced by
Catching very general exceptions such as Exception is usually not recommended.

Generally, you would want to handle very specific errors in the exception handler. This ensure that you do not hide other types of errors which should be fixed.

So, unless you specifically plan to handle any error, consider adding a more specific exception.

Loading history...
93
                log.warn('Unable to start %r module - %s', key, ex, exc_info=True)
94
95
        log.debug('Started %d module(s): %s', len(started), ', '.join(started))
96
97
    def get(self, key, default=None):
98
        return self.modules.get(key, default)
99