-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathplugin_manager.py
executable file
·44 lines (35 loc) · 1.35 KB
/
plugin_manager.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
#!/usr/bin/env python
import sys
import os
import traceback
class Plugin(object):
pass
class PluginManager():
def __init__(self, path=None, plugin_init_args={}):
if path:
self.plugin_dir = path
else:
self.plugin_dir = os.path.dirname(__file__) + '/plugins/'
self.plugins = {}
self._load_plugins()
self._register_plugins(**plugin_init_args)
def _load_plugins(self):
sys.path.append(self.plugin_dir)
plugin_files = [fn for fn in os.listdir(self.plugin_dir) if fn.startswith('plugin_') and fn.endswith('.py')]
plugin_modules = [m.split('.')[0] for m in plugin_files]
for module in plugin_modules:
m = __import__(module)
def _register_plugins(self, **kwargs):
for plugin in Plugin.__subclasses__():
obj = plugin(**kwargs)
self.plugins[obj] = obj.keywords if hasattr(obj, 'keywords') else []
def call_method(self, method, args={}, keywords=[]):
result = {}
for plugin in self.plugins:
if not keywords or (set(keywords) & set(self.plugins[plugin])):
try:
name_space = plugin.__class__.__name__
result[name_space] = getattr(plugin, method)(**args)
except AttributeError:
pass
return result