-
Notifications
You must be signed in to change notification settings - Fork 0
/
brain.py
64 lines (56 loc) · 2.06 KB
/
brain.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# -*- coding: utf-8-*-
import logging
import pkgutil
class Brain(object):
def __init__(self, profile):
"""
Instantiates a new Brain object, which cross-references user
input with a list of modules. Note that the order of brain.modules
matters, as the Brain will cease execution on the first module
that accepts a given input.
Arguments:
profile -- contains information related to the rule
"""
self.profile = profile
self.modules = self.get_modules()
self.outflag=False
self._logger = logging.getLogger(__name__)
@classmethod
def get_modules(cls):
"""
Dynamically loads all the modules in the modules folder and sorts
them by the PRIORITY key. If no PRIORITY is defined for a given
module, a priority of 0 is assumed.
"""
logger = logging.getLogger(__name__)
locations = ["./plugins"]
logger.debug("Looking for modules in: %s",
', '.join(["'%s'" % location for location in locations]))
modules = []
for finder, name, ispkg in pkgutil.walk_packages(locations):
try:
loader = finder.find_module(name)
mod = loader.load_module(name)
except:
logger.warning("Skipped module '%s' due to an error.", name,
exc_info=True)
else:
modules.append(mod)
modules.sort(key=lambda mod: mod.PRIORITY if hasattr(mod, 'PRIORITY')
else 0, reverse=True)
return modules
def action(self,plugin):
"""
"""
NoActionModule=True
for module in self.modules:
if plugin == module.__name__:
NoActionModule=False
self._logger.debug("Calling action for for module %s", module.__name__)
try:
self.outflag=module.handle(self.profile)
except:
self._logger.error('Failed to execute module',
exc_info=True)
if NoActionModule:
self._logger.info("No action %r available ",plugin)