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