]> jfr.im git - erebus.git/blobdiff - modlib.py
core - add exception hooks
[erebus.git] / modlib.py
index ae19fb5e0f77e3311000886b22073db3f91e1d2c..b09272e89b3704d8daa3ef3f7d03b0555db3e05e 100644 (file)
--- a/modlib.py
+++ b/modlib.py
@@ -62,6 +62,7 @@ class modlib(object):
                self.hooks = {}
                self.numhooks = {}
                self.chanhooks = {}
+               self.exceptionhooks = []
                self.helps = []
                self.parent = None
 
@@ -77,12 +78,14 @@ class modlib(object):
                #"specified" values will be printed. unspecified values will result in "OK" or "failed"
                self.parent = parent
                for cmd, func in self.hooks.items():
-                       self.parent.hook(cmd, func)
-                       self.parent.hook("%s.%s" % (self.name, cmd), func)
+                       parent.hook(cmd, func)
+                       parent.hook("%s.%s" % (self.name, cmd), func)
                for num, func in self.numhooks.items():
-                       self.parent.hooknum(num, func)
+                       parent.hooknum(num, func)
                for chan, func in self.chanhooks.items():
-                       self.parent.hookchan(chan, func)
+                       parent.hookchan(chan, func)
+               for exc, func in self.exceptionhooks:
+                       parent.hookexception(exc, func)
 
                for func, args, kwargs in self.helps:
                        try:
@@ -98,6 +101,8 @@ class modlib(object):
                        parent.unhooknum(num, func)
                for chan, func in self.chanhooks.items():
                        parent.unhookchan(chan, func)
+               for exc, func in self.exceptionhooks:
+                       parent.unhookexception(exc, func)
 
                for func, args, kwargs in self.helps:
                        try:
@@ -106,6 +111,14 @@ class modlib(object):
                                pass
                return True
 
+       def hookexception(self, exc):
+               def realhook(func):
+                       self.exceptionhooks.append((exc, func))
+                       if self.parent is not None:
+                               self.parent.hookexception(exc, func)
+                       return func
+               return realhook
+
        def hooknum(self, num):
                def realhook(func):
                        self.numhooks[str(num)] = func