-
Notifications
You must be signed in to change notification settings - Fork 4
/
__init__.py
113 lines (78 loc) · 2.8 KB
/
__init__.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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
"""Plugins
Default kdb plugins
"""
from __future__ import print_function
import sys
from traceback import format_exc
from inspect import getmembers, isclass
from pymills.utils import safe__import__
from circuits.tools import kill
from circuits import Event, Component
from cidict import cidict
from ..utils import log
from ..plugin import BasePlugin
DEFAULTS = ["core", "channels", "help", "irc"]
class load(Event):
"""load Event"""
class query(Event):
"""query Event"""
class unload(Event):
"""unload Event"""
class Plugins(Component):
channel = "plugins"
def init(self, init_args=None, init_kwargs=None):
self.init_args = init_args or tuple()
self.init_kwargs = init_kwargs or dict()
self.plugins = cidict()
def query(self, name=None):
if name is None:
return self.plugins
else:
return self.plugins.get(name, None)
def load(self, name, package=__package__):
if name in self.plugins:
msg = log("Not loading already loaded plugin: {0:s}", name)
return msg
try:
fqplugin = "{0:s}.{1:s}".format(package, name)
if fqplugin in sys.modules:
reload(sys.modules[fqplugin])
m = safe__import__(name, globals(), locals(), package)
p1 = lambda x: isclass(x) and issubclass(x, BasePlugin) # noqa
p2 = lambda x: x is not BasePlugin # noqa
predicate = lambda x: p1(x) and p2(x) # noqa
plugins = getmembers(m, predicate)
for name, Plugin in plugins:
instance = Plugin(*self.init_args, **self.init_kwargs)
instance.register(self)
log(
"Registered Component: {0:s}",
instance
)
if name not in self.plugins:
self.plugins[name] = set()
self.plugins[name].add(instance)
msg = log("Loaded plugin: {0:s}", name)
return msg
except Exception, e:
msg = log(
"Could not load plugin: {0:s} Error: {1:s}",
name,
e
)
log(format_exc())
return msg
def unload(self, name):
if name in self.plugins:
instances = self.plugins[name]
for instance in instances:
kill(instance)
log("Unregistered Component: {0:s}", instance)
if hasattr(instance, "cleanup"):
instance.cleanup()
log("Cleaned up Component: {0:s}", instance)
del self.plugins[name]
msg = log("Unloaded plugin: {0:s}", name)
else:
msg = log("Not unloading unloaded plugin: {0:s}", name)
return msg