]> jfr.im git - erebus.git/blobdiff - ctlmod.py
admin_config - add !getconfig, remove some unused functions
[erebus.git] / ctlmod.py
index ab9145a249baf0b501e2bc6c4cd93eb6f298c9ef..3e30892d25fbb7c19e32c014c99cce82dbacd9c1 100644 (file)
--- a/ctlmod.py
+++ b/ctlmod.py
@@ -1,43 +1,60 @@
 # 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, traceback
 import modlib
 
+if sys.version_info.major >= 3:
+       from importlib import reload # reload is only available as a global in Py2, only in importlib in Py3
+else:
+       importlib.invalidate_caches = lambda: None # invalidate_caches doesn't exist in Py2
+
 modules = {}
 dependents = {}
+#dependents[modname] = [list of modules which depend on modname]
 
 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 "%05.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))
+                       if isinstance(modstatus, modlib.error) and isinstance(modstatus.errormsg, BaseException):
+                               traceback.print_exception(modstatus.errormsg)
        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):
-               sys.path.insert(0, 'modules')
+               importlib.invalidate_caches()
                try:
-                       mod = __import__(modname)
+                       mod = importlib.import_module('modules.'+modname)
                        reload(mod) #in case it's been previously loaded.
-               except BaseException as e: #we don't want even sys.exit() to crash us (in case of malicious module) so use BaseException
+               except Exception as e:
                        return modlib.error(e)
-               finally:
-                       del sys.path[0] #remove ./modules from path, in case there's a name conflict
 
 
                if not hasattr(mod, 'modinfo'):
@@ -50,20 +67,46 @@ 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 not depret:
-                                       return
+                       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 and 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:
+               try:
+                       ret = mod.modstart(parent)
+               except Exception as e:
+                       return modlib.error(e)
+               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')
 
@@ -71,7 +114,7 @@ def unload(parent, modname):
        if isloaded(modname):
                for dependent in dependents[modname]:
                        unload(parent, dependent)
-               for dep in dependents[modname]:
+               for dep in modules[modname].modinfo['depends']:
                        dependents[dep].remove(modname)
                ret = modules[modname].modstop(parent)
                del modules[modname]
@@ -103,5 +146,3 @@ def unloadall(parent, modlist):
        for m in modlist: unload(parent, m)
 def reloadall(parent, modlist):
        for m in modlist: reloadmod(parent, m)
-
-sys.path.append('modules')