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