]> jfr.im git - erebus.git/blobdiff - ctlmod.py
* - change old code to use newer cfg.getboolean instead of bool(int())
[erebus.git] / ctlmod.py
index 73ae1fcd015d228180dc7ad8de64dc7a21f6072d..dbd2a36bdf5185a9935ea8115b85ea035ddc0d29 100644 (file)
--- a/ctlmod.py
+++ b/ctlmod.py
@@ -1,19 +1,50 @@
 # Erebus IRC bot - Author: John Runyon
 # module loading/unloading/tracking code
 
-import sys
+import sys, time
 import modlib
 
 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):
+def load(parent, modname, dependent=False):
+       #wrapper to call _load and print return
+       if dependent:
+               print "(Loading dependency %s..." % (modname),
+       else:
+               print "%09.3f [MOD] [?] Loading %s..." % (time.time() % 100000, modname),
+       modstatus = _load(parent, modname, dependent)
+       if not modstatus:
+               if dependent:
+                       print "failed: %s)" % (modstatus),
+               else:
+                       print "failed: %s." % (modstatus)
+       elif modstatus == True:
+               if dependent:
+                       print "OK)",
+               else:
+                       print "OK."
+       else:
+               if dependent:
+                       print "OK: %s)" % (modstatus),
+               else:
+                       print "OK: %s." % (modstatus)
+       return modstatus
+
+def _load(parent, modname, dependent=False):
+       successstatus = []
        if not isloaded(modname):
-               mod = __import__(modname)
-               reload(mod)
+               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
+                       reload(mod) #in case it's been previously loaded.
+               except Exception as e:
+                       return modlib.error(e)
+
 
                if not hasattr(mod, 'modinfo'):
                        return modlib.error('no modinfo')
@@ -25,20 +56,44 @@ def load(parent, modname):
                dependents[modname] = []
 
                for dep in mod.modinfo['depends']:
-                       if dep not in modules:
-                               depret = load(parent, dep)
-                               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:
+                                               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')
 
@@ -46,7 +101,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]
@@ -60,13 +115,14 @@ def reloadmod(parent, modname):
                else: modules[modname].modstop(parent)
 
                try:
-                       return reload(modules[modname])
-               except BaseException, e:
+                       reload(modules[modname])
+               except BaseException as e:
                        return modlib.error(e)
 
-               if modhas(modname, 'modrestarted'): modules[modname].modrestarted(parent)
-               else: modules[modname].modstart(parent)
+               if modhas(modname, 'modrestarted'): ret = modules[modname].modrestarted(parent)
+               else: ret = modules[modname].modstart(parent)
 
+               return ret
        else:
                return load(parent, modname)
 
@@ -77,5 +133,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')