Passed
Push — develop ( 9e1b8b...5d0b21 )
by Dean
03:00
created

ModuleManagerMeta   A

Complexity

Total Complexity 1

Size/Duplication

Total Lines 3
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 1
c 0
b 0
f 0
dl 0
loc 3
ccs 3
cts 3
cp 1
rs 10

1 Method

Rating   Name   Duplication   Size   Complexity  
A __getitem__() 0 2 1
1 1
import logging
2
3 1
log = logging.getLogger(__name__)
4
5
6 1
class ModuleManagerMeta(type):
7 1
    def __getitem__(self, key):
8 1
        return self.modules[key]
9
10
11 1
class ModuleManager(object):
12 1
    __metaclass__ = ModuleManagerMeta
13
14 1
    modules = {}
15
16 1
    @classmethod
17
    def initialize(cls):
18 1
        cls.modules = dict(cls.construct())
19
20 1
    @classmethod
21
    def discover(cls):
22 1
        from plugin.modules.matcher.main import Matcher
23 1
        from plugin.modules.scheduler.main import Scheduler
24 1
        from plugin.modules.sessions.main import Sessions
25 1
        from plugin.modules.upgrade.main import Upgrade
26
27 1
        return [
28
            Matcher,
29
            Scheduler,
30
            Sessions,
31
            Upgrade
32
        ]
33
34 1
    @classmethod
35
    def construct(cls):
36 1
        try:
37 1
            available = cls.discover()
38
        except Exception, ex:
39
            log.error('Unable to import modules: %s', ex, exc_info=True)
40
            return
41
42 1
        constructed = []
43
44 1
        for module in available:
45 1
            try:
46 1
                if module.__key__ is None:
47
                    # Automatically set module `__key__` (if one isn't specified)
48
                    module.__key__ = module.__class__.__name__.lower()
49
50 1
                yield module.__key__, module()
51
52 1
                constructed.append(module.__key__)
53
            except Exception, ex:
54
                log.warn('Unable to construct module: %r', module)
55
56 1
        log.debug('Constructed %d module(s): %s', len(constructed), ', '.join(constructed))
57
58 1
    @classmethod
59 1
    def start(cls, keys=None):
60 1
        started = []
61
62 1
        for key, module in cls.modules.items():
63 1
            if keys is not None and key not in keys:
64 1
                continue
65
66 1
            try:
67 1
                module.start()
68
69 1
                started.append(key)
70
            except Exception, ex:
71
                log.warn('Unable to start %r module - %s', key, ex, exc_info=True)
72
73 1
        log.debug('Started %d module(s): %s', len(started), ', '.join(started))
74
75 1
    def get(self, key, default=None):
76
        return self.modules.get(key, default)
77