]>
Commit | Line | Data |
---|---|---|
931c88a4 | 1 | # Erebus IRC bot - Author: John Runyon |
2 | # module loading/unloading/tracking code | |
3 | ||
a28e2ae9 | 4 | from __future__ import print_function |
5 | ||
b9c6eb1d | 6 | import sys, time |
e4255e70 | 7 | import modlib |
db50981b | 8 | |
a28e2ae9 | 9 | if sys.version_info.major >= 3: |
10 | from importlib import reload | |
11 | ||
db50981b | 12 | modules = {} |
e4255e70 | 13 | dependents = {} |
a8553c45 | 14 | #dependents[modname] = [list of modules which depend on modname] |
db50981b | 15 | |
16 | def isloaded(modname): return modname in modules | |
d431e543 | 17 | def modhas(modname, attname): return getattr(modules[modname], attname, None) is not None |
db50981b | 18 | |
b9c6eb1d | 19 | def load(parent, modname, dependent=False): |
20 | #wrapper to call _load and print return | |
21 | if dependent: | |
a28e2ae9 | 22 | print("(Loading dependency %s..." % (modname), end=' ') |
b9c6eb1d | 23 | else: |
a28e2ae9 | 24 | print("%09.3f [MOD] [?] Loading %s..." % (time.time() % 100000, modname), end=' ') |
b9c6eb1d | 25 | modstatus = _load(parent, modname, dependent) |
26 | if not modstatus: | |
a62d0d18 | 27 | if dependent: |
a28e2ae9 | 28 | print("failed: %s)" % (modstatus), end=' ') |
a62d0d18 | 29 | else: |
a28e2ae9 | 30 | print("failed: %s." % (modstatus)) |
b9c6eb1d | 31 | elif modstatus == True: |
32 | if dependent: | |
a28e2ae9 | 33 | print("OK)", end=' ') |
b9c6eb1d | 34 | else: |
a28e2ae9 | 35 | print("OK.") |
b9c6eb1d | 36 | else: |
a62d0d18 | 37 | if dependent: |
a28e2ae9 | 38 | print("OK: %s)" % (modstatus), end=' ') |
a62d0d18 | 39 | else: |
a28e2ae9 | 40 | print("OK: %s." % (modstatus)) |
b9c6eb1d | 41 | return modstatus |
42 | ||
43 | def _load(parent, modname, dependent=False): | |
a62d0d18 | 44 | successstatus = [] |
db50981b | 45 | if not isloaded(modname): |
96d0b31e | 46 | try: |
a28e2ae9 | 47 | mod = __import__('modules.'+modname, globals(), locals(), ['*'], 0) |
83ed3882 | 48 | # ^ fromlist doesn't actually do anything(?) but it means we don't have to worry about this returning the top-level "modules" object |
8ba56606 | 49 | reload(mod) #in case it's been previously loaded. |
83ed3882 | 50 | except Exception as e: |
96d0b31e | 51 | return modlib.error(e) |
20df9fbb | 52 | |
e4255e70 | 53 | |
6b2c681d | 54 | if not hasattr(mod, 'modinfo'): |
55 | return modlib.error('no modinfo') | |
56 | ||
a76c4bd8 | 57 | if parent.APIVERSION not in mod.modinfo['compatible']: |
e4255e70 | 58 | return modlib.error('API-incompatible') |
59 | ||
db50981b | 60 | modules[modname] = mod |
e4255e70 | 61 | dependents[modname] = [] |
62 | ||
63 | for dep in mod.modinfo['depends']: | |
a62d0d18 | 64 | if bool(int(parent.cfg.get('autoloads', dep, default=1))): |
65 | if dep not in modules: | |
66 | depret = load(parent, dep, dependent=True) | |
67 | if depret is not None and not depret: | |
68 | return depret | |
69 | else: | |
70 | return modlib.error("dependent %s disabled" % (dep)) | |
e4255e70 | 71 | dependents[dep].append(modname) |
72 | ||
a62d0d18 | 73 | for dep in mod.modinfo['softdeps']: |
74 | if bool(int(parent.cfg.get('autoloads', dep, default=1))): | |
75 | if dep not in modules: | |
76 | depret = load(parent, dep, dependent=True) | |
77 | if depret is not None: | |
78 | if not depret: | |
79 | successstatus.append("softdep %s failed" % (dep)) | |
80 | else: | |
81 | successstatus.append("softdep %s disabled" % (dep)) | |
82 | #swallow errors loading - softdeps are preferred, not required | |
83 | ||
e4255e70 | 84 | |
db50981b | 85 | ret = mod.modstart(parent) |
a62d0d18 | 86 | if ret is None: |
87 | ret = True | |
88 | if not ret: | |
db50981b | 89 | del modules[modname] |
e4255e70 | 90 | del dependents[modname] |
91 | for dep in mod.modinfo['depends']: | |
92 | dependents[dep].remove(modname) | |
a62d0d18 | 93 | |
94 | successstatus = ';'.join(successstatus) | |
95 | if len(successstatus) > 0 and ret: | |
96 | if ret == True: | |
97 | return successstatus | |
98 | else: | |
99 | return "%s (%s)" % (ret, successstatus) | |
100 | else: | |
101 | return ret | |
e4255e70 | 102 | else: #if not isloaded...else: |
103 | return modlib.error('already loaded') | |
db50981b | 104 | |
105 | def unload(parent, modname): | |
106 | if isloaded(modname): | |
e4255e70 | 107 | for dependent in dependents[modname]: |
108 | unload(parent, dependent) | |
a8553c45 | 109 | for dep in modules[modname].modinfo['depends']: |
e4255e70 | 110 | dependents[dep].remove(modname) |
5a81c82b | 111 | ret = modules[modname].modstop(parent) |
112 | del modules[modname] | |
113 | return ret | |
db50981b | 114 | else: |
e4255e70 | 115 | return modlib.error('already unloaded') |
db50981b | 116 | |
117 | def reloadmod(parent, modname): | |
118 | if isloaded(modname): | |
d431e543 | 119 | if modhas(modname, 'modrestart'): modules[modname].modrestart(parent) |
120 | else: modules[modname].modstop(parent) | |
db50981b | 121 | |
e3878612 | 122 | try: |
65b86c90 | 123 | reload(modules[modname]) |
96d0b31e | 124 | except BaseException as e: |
e3878612 | 125 | return modlib.error(e) |
db50981b | 126 | |
8ba56606 | 127 | if modhas(modname, 'modrestarted'): ret = modules[modname].modrestarted(parent) |
128 | else: ret = modules[modname].modstart(parent) | |
db50981b | 129 | |
8ba56606 | 130 | return ret |
db50981b | 131 | else: |
e3878612 | 132 | return load(parent, modname) |
133 | ||
db50981b | 134 | |
135 | def loadall(parent, modlist): | |
136 | for m in modlist: load(parent, m) | |
137 | def unloadall(parent, modlist): | |
138 | for m in modlist: unload(parent, m) | |
139 | def reloadall(parent, modlist): | |
140 | for m in modlist: reloadmod(parent, m) |