|
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): |
|
|
|
|
|
|
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: |
|
|
|
|
|
|
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: |
|
|
|
|
|
|
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: |
|
|
|
|
|
|
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: |
|
|
|
|
|
|
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: |
|
|
|
|
|
|
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: |
|
|
|
|
|
|
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: |
|
|
|
|
|
|
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
|
|
|
|