X-Git-Url: https://jfr.im/git/erebus.git/blobdiff_plain/a8553c45336be4a753376127fcad5c9034cae707..4477123de1254afa80243f7f9eab62c5cbf7b786:/ctlmod.py diff --git a/ctlmod.py b/ctlmod.py index be1095f..2799f01 100644 --- a/ctlmod.py +++ b/ctlmod.py @@ -1,9 +1,17 @@ # Erebus IRC bot - Author: John Runyon +# vim: fileencoding=utf-8 # module loading/unloading/tracking code -import sys, time +from __future__ import print_function + +import sys, time, importlib import modlib +if sys.version_info.major >= 3: + from importlib import reload +else: + importlib.invalidate_caches = lambda: None + modules = {} dependents = {} #dependents[modname] = [list of modules which depend on modname] @@ -12,28 +20,36 @@ def isloaded(modname): return modname in modules def modhas(modname, attname): return getattr(modules[modname], attname, None) is not None def load(parent, modname, dependent=False): - #wrapper to call _load and print return + """Wrapper to call _load and print the return value.""" if dependent: - print "(Loading dependency %s..." % (modname), + print("(Loading dependency %s..." % (modname), end=' ') else: - print "%09.3f [MOD] [?] Loading %s..." % (time.time() % 100000, modname), + print("%09.3f [MOD] [?] Loading %s..." % (time.time() % 100000, modname), end=' ') modstatus = _load(parent, modname, dependent) if not modstatus: - print str(modstatus) + if dependent: + print("failed: %s)" % (modstatus), end=' ') + else: + print("failed: %s." % (modstatus)) elif modstatus == True: if dependent: - print "OK)", + print("OK)", end=' ') else: - print "OK." + print("OK.") else: - print modstatus + if dependent: + print("OK: %s)" % (modstatus), end=' ') + else: + print("OK: %s." % (modstatus)) return modstatus def _load(parent, modname, dependent=False): + """Load and return the new status of the module.""" + successstatus = [] if not isloaded(modname): + importlib.invalidate_caches() try: - mod = __import__('modules.'+modname, globals(), locals(), ['*'], -1) - # ^ fromlist doesn't actually do anything(?) but it means we don't have to worry about this returning the top-level "modules" object + mod = importlib.import_module('modules.'+modname) reload(mod) #in case it's been previously loaded. except Exception as e: return modlib.error(e) @@ -49,20 +65,44 @@ def _load(parent, modname, dependent=False): dependents[modname] = [] for dep in mod.modinfo['depends']: - if dep not in modules: - depret = load(parent, dep, dependent=True) - if depret is not None and not depret: - return depret #TODO FIXME + if bool(int(parent.cfg.get('autoloads', dep, default=1))): + if dep not in modules: + depret = load(parent, dep, dependent=True) + if depret is not None and not depret: + return depret + else: + return modlib.error("dependent %s disabled" % (dep)) dependents[dep].append(modname) + for dep in mod.modinfo['softdeps']: + if bool(int(parent.cfg.get('autoloads', dep, default=1))): + if dep not in modules: + depret = load(parent, dep, dependent=True) + if depret is not None: + if not depret: + successstatus.append("softdep %s failed" % (dep)) + else: + successstatus.append("softdep %s disabled" % (dep)) + #swallow errors loading - softdeps are preferred, not required + ret = mod.modstart(parent) - if ret is not None and not ret: + if ret is None: + ret = True + if not ret: del modules[modname] del dependents[modname] for dep in mod.modinfo['depends']: dependents[dep].remove(modname) - return ret + + successstatus = ';'.join(successstatus) + if len(successstatus) > 0 and ret: + if ret == True: + return successstatus + else: + return "%s (%s)" % (ret, successstatus) + else: + return ret else: #if not isloaded...else: return modlib.error('already loaded')