forked from FreifunkBremen/nodewatcher
-
Notifications
You must be signed in to change notification settings - Fork 0
/
PluginManager.py
63 lines (57 loc) · 1.82 KB
/
PluginManager.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
import sys
import os
import imp
import logging
import config
logger = logging.getLogger(__name__)
class PluginManager(object):
modulename = None
def __init__(self, plugins):
self.plugins = []
if not self.modulename:
raise NotImplementedError(
"%s.modulename not set!",
self.__class__.__name__
)
for plugin in plugins:
location = os.path.join('plugins', plugin[0])
if not os.path.isdir(location):
logger.error("Plugin %s not found", plugin[0])
continue
try:
info = imp.find_module(self.modulename, [location])
except ImportError:
logger.exception(
"Exception while importing plugin %s",
plugin[0]
)
continue
module = imp.load_module(self.modulename, *info)
try:
cls = getattr(module, plugin[0])
except AttributeError:
logger.error(
"Plugin %s has not class to load",
plugin[0],
self.modulename
)
continue
finally:
if info[0]:
info[0].close()
if cls:
# TODO: instantiate on demand
try:
self.plugins.append(cls(plugin[1]))
except:
logger.exception(
"Exception while instiating %s",
cls.__name__
)
def quit(self):
for notifier in self.plugins:
if hasattr(notifier, 'quit'):
try:
notifier.quit()
except TypeError:
pass