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