]> jfr.im git - erebus.git/commitdiff
Added a few module featuresm depends needs testing!
authorJohn Runyon <redacted>
Tue, 3 Dec 2013 10:15:55 +0000 (04:15 -0600)
committerJohn Runyon <redacted>
Tue, 3 Dec 2013 10:15:55 +0000 (04:15 -0600)
ctlmod.py
erebus.py
modlib.py
modules/eval.py
modules/modtest.py

index 86943d17e670497cbb48b1e156d0d9833d72ad00..25046aad38b0823ea098a3e6eac18844f8c2933b 100644 (file)
--- a/ctlmod.py
+++ b/ctlmod.py
@@ -1,6 +1,8 @@
 import sys
+import modlib
 
 modules = {}
+dependents = {}
 
 def isloaded(modname): return modname in modules
 def modhas(modname, attname): return getattr(self.modules[modname], attname, None) is not None
@@ -8,19 +10,41 @@ def modhas(modname, attname): return getattr(self.modules[modname], attname, Non
 def load(parent, modname):
        if not isloaded(modname):
                mod = __import__(modname)
+               reload(mod)
+
+               if 1 not in mod.modinfo['compatible']:
+                       return modlib.error('API-incompatible')
+
                modules[modname] = mod
+               dependents[modname] = []
+
+               for dep in mod.modinfo['depends']:
+                       if dep not in modules:
+                               depret = load(parent, dep)
+                               if not depret:
+                                       return
+                       dependents[dep].append(modname)
+
+
                ret = mod.modstart(parent)
-               if not ret:
+               if ret is not None and not ret:
                        del modules[modname]
+                       del dependents[modname]
+                       for dep in mod.modinfo['depends']:
+                               dependents[dep].remove(modname)
                return ret
-       else:
-               return -1
+       else: #if not isloaded...else:
+               return modlib.error('already loaded')
 
 def unload(parent, modname):
        if isloaded(modname):
+               for dependent in dependents[modname]:
+                       unload(parent, dependent)
+               for dep in dependents[modname]:
+                       dependents[dep].remove(modname)
                self.modules[modname].modstop(parent)
        else:
-               return -1
+               return modlib.error('already unloaded')
 
 def reloadmod(parent, modname):
        if isloaded(modname):
index 6e1289e12786e7719a36df9500a6d25aba62e14a..c75183ca31fe6a5239b7d6c6723a41e01e982b84 100644 (file)
--- a/erebus.py
+++ b/erebus.py
@@ -109,16 +109,8 @@ class Erebus(object):
                        if bot.conn.state == 0:
                                bot.connect()
 
-       #module functions
-       def modlist(self): pass
-       def hasmod(self, name): pass
-       def loadmod(self, name): pass
-       def unloadmod(self, name): pass
-       def reloadmod(self, name): pass
-
        #bind functions
        def hook(self, word, handler):
-               print "hooked %r to %r" % (word, handler)
                self.msghandlers[word] = handler
        def unhook(self, word):
                del self.msghandlers[word]
index 72c9fa2be988745e545a21c3c2051bab5bcd20d2..323ef0222579ce7bcfbcd1f363b37c60a2b17f16 100644 (file)
--- a/modlib.py
+++ b/modlib.py
@@ -1,3 +1,13 @@
+class error(object):
+       def __init__(self, desc):
+               self.errormsg = desc
+       def __nonzero__(self):
+               return False #object will test to False
+       def __repr__(self):
+               return '<modlib.error %r>' % self.errormsg
+       def __str__(self):
+               return self.errormsg
+
 class modlib(object):
        def __init__(self, name):
                self.hooks = {}
index 04f08fadf817d60d02e9843eacd13a9efb4c7922..ff65ceabcb6fc1655896450e409e0978cb7ff1c4 100644 (file)
@@ -1,10 +1,18 @@
+# module info
+modinfo = {
+       'author': 'John Runyon (DimeCadmium)',
+       'license': 'public domain',
+       'compatible': [1],
+       'depends': [],
+}
+
 # preamble
 import modlib
 lib = modlib.modlib(__name__)
 modstart = lib.modstart
 modstop = lib.modstop
 
-#module code
+# module code
 import sys
 
 
index 928a834ec5713c30fd48f63ed86a7c4e80a0f4ed..75417e3f521b910c7b2db14db9165c6b9978001f 100644 (file)
@@ -1,10 +1,18 @@
+# module info
+modinfo = {
+       'author': 'John Runyon (DimeCadmium)',
+       'license': 'public domain',
+       'compatible': [1], # compatible module API versions
+       'depends': [], # other modules required to work properly?
+}
+
 # preamble
 import modlib
 lib = modlib.modlib(__name__)
 modstart = lib.modstart
 modstop = lib.modstop
 
-#module code
+# module code
 @lib.hook('test')
 def cmd_test(bot, user, chan, *args):
        bot.msg(chan, "You said: !test %s" % (' '.join([str(arg) for arg in args])))