]>
Commit | Line | Data |
---|---|---|
1 | # Erebus IRC bot - Author: John Runyon | |
2 | # module loading/unloading/tracking code | |
3 | ||
4 | import sys | |
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): | |
14 | if not isloaded(modname): | |
15 | mod = __import__(modname) | |
16 | reload(mod) | |
17 | ||
18 | if 1 not in mod.modinfo['compatible']: | |
19 | return modlib.error('API-incompatible') | |
20 | ||
21 | modules[modname] = mod | |
22 | dependents[modname] = [] | |
23 | ||
24 | for dep in mod.modinfo['depends']: | |
25 | if dep not in modules: | |
26 | depret = load(parent, dep) | |
27 | if not depret: | |
28 | return | |
29 | dependents[dep].append(modname) | |
30 | ||
31 | ||
32 | ret = mod.modstart(parent) | |
33 | if ret is not None and not ret: | |
34 | del modules[modname] | |
35 | del dependents[modname] | |
36 | for dep in mod.modinfo['depends']: | |
37 | dependents[dep].remove(modname) | |
38 | return ret | |
39 | else: #if not isloaded...else: | |
40 | return modlib.error('already loaded') | |
41 | ||
42 | def unload(parent, modname): | |
43 | if isloaded(modname): | |
44 | for dependent in dependents[modname]: | |
45 | unload(parent, dependent) | |
46 | for dep in dependents[modname]: | |
47 | dependents[dep].remove(modname) | |
48 | return modules[modname].modstop(parent) | |
49 | else: | |
50 | return modlib.error('already unloaded') | |
51 | ||
52 | def reloadmod(parent, modname): | |
53 | if isloaded(modname): | |
54 | if modhas(modname, 'modrestart'): modules[modname].modrestart(parent) | |
55 | else: modules[modname].modstop(parent) | |
56 | ||
57 | reload(modules[modname]) | |
58 | ||
59 | if modhas(modname, 'modrestarted'): modules[modname].modrestarted(parent) | |
60 | else: modules[modname].modstart(parent) | |
61 | ||
62 | else: | |
63 | load(parent, modname) | |
64 | ||
65 | def loadall(parent, modlist): | |
66 | for m in modlist: load(parent, m) | |
67 | def unloadall(parent, modlist): | |
68 | for m in modlist: unload(parent, m) | |
69 | def reloadall(parent, modlist): | |
70 | for m in modlist: reloadmod(parent, m) | |
71 | ||
72 | sys.path.append('modules') |