Add Box class and input filtering hooks, and remove the stats code again.
[rawdog/.git] / rawdoglib / plugins.py
index 0e3d276c56d59244ecb71d2cea49de3cd5695806..f491b05fd89495c1356001d61a58050702cf9907 100644 (file)
 
 import os, os.path, imp
 
-def load_plugins(config):
-       count = 0
-       for dir in config["plugindirs"]:
-               try:
-                       files = os.listdir(dir)
-               except OSError:
-                       # Ignore directories that can't be read.
-                       files = []
-
-               for file in files:
-                       if file == "" or file[0] == ".":
-                               continue
-
-                       desc = None
-                       for d in imp.get_suffixes():
-                               if file.endswith(d[0]):
-                                       desc = d
-                       if desc is None:
-                               continue
-
-                       fn = os.path.join(dir, file)
-                       config.log("Loading plugin ", fn)
-                       f = open(fn, "r")
-                       mod = imp.load_module("plugin%d" % (count,), f, fn, desc)
-                       count += 1
-                       f.close()
+class Box:
+       """Utility class that holds a mutable value. Useful for passing
+       immutable types by reference."""
+       def __init__(self, value = None):
+               self.value = value
+
+plugin_count = 0
+
+def load_plugins(dir, config):
+       global plugin_count
+
+       try:
+               files = os.listdir(dir)
+       except OSError:
+               # Ignore directories that can't be read.
+               return
+
+       for file in files:
+               if file == "" or file[0] == ".":
+                       continue
+
+               desc = None
+               for d in imp.get_suffixes():
+                       if file.endswith(d[0]) and d[2] == imp.PY_SOURCE:
+                               desc = d
+               if desc is None:
+                       continue
+
+               fn = os.path.join(dir, file)
+               config.log("Loading plugin ", fn)
+               f = open(fn, "r")
+               mod = imp.load_module("plugin%d" % (plugin_count,), f, fn, desc)
+               plugin_count += 1
+               f.close()
 
 attached = {}
 
@@ -56,8 +64,10 @@ def attach_hook(hookname, func):
 def call_hook(hookname, *args):
        """Call all the functions attached to a hook with the given
        arguments, in the order they were added, stopping if a hook function
-       returns False."""
+       returns False. Returns True if any hook function returned False (i.e.
+       returns True if any hook function handled the request)."""
        for func in attached.get(hookname, []):
                if not func(*args):
-                       break
+                       return True
+       return False