-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplugin_manager.py
90 lines (68 loc) · 2.4 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
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
'''
Created on Nov 5, 2011
@author: David
'''
import os
import json
from plugin_base import PluginBase
from fun_thread import FunThread
PLUGIN_PATH = 'plugins'
# TODO: put plugin management in its own module
# code from:
# http://www.luckydonkey.com/2008/01/02/python-style-plugins-made-easy/
def get_plugins(bot):
plugins = {}
try:
f = open("plugins/conf/blacklist.json")
blacklist = json.load(f)['blacklist']
f.close()
except Exception:
blacklist = []
def add_subclass(modulename):
name = ".".join(modulename.split(".")[1:])
if name in blacklist:
print "Plugin %s blacklisted" % (name,)
return
def add_plugin(e):
print "Adding plugin %s" % (name,)
e._set_bot_instance(bot)
e.plugin_init()
evs = e.events
for ev in evs:
if not ev in plugins:
plugins[ev] = []
plugins[ev].append(e)
module=__import__(modulename)
#walk the dictionaries to get to the last one
d=module.__dict__
for m in modulename.split('.')[1:]:
d=d[m].__dict__
for key, entry in d.items():
if key == PluginBase.__name__: #@UndefinedVariable
continue
try:
if issubclass(entry, PluginBase):
add_plugin(entry())
except TypeError:
continue
for root, dirs, files in os.walk(PLUGIN_PATH): #@UnusedVariable
for name in files:
if name.endswith(".py") and not name.startswith("__"):
path = os.path.join(root, name)
modulename = path.rsplit('.', 1)[0].replace(os.sep, '.')
add_subclass(modulename)
bot._plugins = plugins
def handle_plugin(event_generic, plugin, args):
if not event_generic in plugin.events:
return
funs = plugin.events[event_generic]
for ev in funs:
t = FunThread(ev.callback, args)
t.start()
# TODO: some kind of thread manager would be nice =)
def handle_plugins(bot, event_generic, args = []):
if not event_generic in bot._plugins:
return
plugs = bot._plugins[event_generic]
for p in plugs:
handle_plugin(event_generic, p, args)