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