]> jfr.im git - erebus.git/blob - ctlmod.py
reworked module and sql debug messages
[erebus.git] / ctlmod.py
1 # Erebus IRC bot - Author: John Runyon
2 # module loading/unloading/tracking code
3
4 import sys, time
5 import modlib
6
7 modules = {}
8 dependents = {}
9
10 def isloaded(modname): return modname in modules
11 def modhas(modname, attname): return getattr(modules[modname], attname, None) is not None
12
13 def load(parent, modname, dependent=False):
14 #wrapper to call _load and print return
15 if dependent:
16 print "Loading dependency %s..." % (modname),
17 else:
18 print "%05.3f [MOD] [#] Loading %s... " % (time.time() % 100000, modname),
19 modstatus = _load(parent, modname, dependent)
20 if not modstatus:
21 print str(modstatus)
22 elif modstatus == True:
23 if dependent:
24 print "OK. ",
25 else:
26 print "OK."
27 else:
28 print modstatus
29 return modstatus
30
31 def _load(parent, modname, dependent=False):
32 if not isloaded(modname):
33 sys.path.insert(0, 'modules')
34 try:
35 mod = __import__(modname)
36 reload(mod) #in case it's been previously loaded.
37 except BaseException as e: #we don't want even sys.exit() to crash us (in case of malicious module) so use BaseException
38 return modlib.error(e)
39 finally:
40 del sys.path[0] #remove ./modules from path, in case there's a name conflict
41
42
43 if not hasattr(mod, 'modinfo'):
44 return modlib.error('no modinfo')
45
46 if parent.APIVERSION not in mod.modinfo['compatible']:
47 return modlib.error('API-incompatible')
48
49 modules[modname] = mod
50 dependents[modname] = []
51
52 for dep in mod.modinfo['depends']:
53 if dep not in modules:
54 depret = load(parent, dep, dependent=True)
55 if not depret:
56 return
57 dependents[dep].append(modname)
58
59
60 ret = mod.modstart(parent)
61 if ret is not None and not ret:
62 del modules[modname]
63 del dependents[modname]
64 for dep in mod.modinfo['depends']:
65 dependents[dep].remove(modname)
66 return ret
67 else: #if not isloaded...else:
68 return modlib.error('already loaded')
69
70 def unload(parent, modname):
71 if isloaded(modname):
72 for dependent in dependents[modname]:
73 unload(parent, dependent)
74 for dep in dependents[modname]:
75 dependents[dep].remove(modname)
76 ret = modules[modname].modstop(parent)
77 del modules[modname]
78 return ret
79 else:
80 return modlib.error('already unloaded')
81
82 def reloadmod(parent, modname):
83 if isloaded(modname):
84 if modhas(modname, 'modrestart'): modules[modname].modrestart(parent)
85 else: modules[modname].modstop(parent)
86
87 try:
88 reload(modules[modname])
89 except BaseException as e:
90 return modlib.error(e)
91
92 if modhas(modname, 'modrestarted'): ret = modules[modname].modrestarted(parent)
93 else: ret = modules[modname].modstart(parent)
94
95 return ret
96 else:
97 return load(parent, modname)
98
99
100 def loadall(parent, modlist):
101 for m in modlist: load(parent, m)
102 def unloadall(parent, modlist):
103 for m in modlist: unload(parent, m)
104 def reloadall(parent, modlist):
105 for m in modlist: reloadmod(parent, m)
106
107 sys.path.append('modules')