Implement a config_option hook. This required a bit of rearrangement in
authorAdam Sampson <ats@offog.org>
Tue, 21 Dec 2004 19:10:59 +0000 (19:10 +0000)
committerAdam Sampson <ats@offog.org>
Tue, 21 Dec 2004 19:10:59 +0000 (19:10 +0000)
order that plugins were actually loaded when the config options were
being parsed.

PLUGINS
config
rawdoglib/plugins.py
rawdoglib/rawdog.py

diff --git a/PLUGINS b/PLUGINS
index 34d758128edfcd2fcef5f663ed5f754d218f6ada..7e9a0223924276fb69d53f3f3dbfa01a6769606d 100644 (file)
--- a/PLUGINS
+++ b/PLUGINS
@@ -39,3 +39,15 @@ been loaded, but before rawdog starts processing command-line arguments.
 
 Run just before rawdog saves the state file and exits.
 
+### config_option(config, name, value)
+
+Called when rawdog encounters a config file option that it doesn't
+recognise with the given name and value. The rawdoglib.rawdog.parse_*
+functions will probably be useful when dealing with config options. You
+can raise ValueError to have rawdog print an appropriate error message.
+You should return False from this hook if name is an option you
+recognise.
+
+Note that using config.log in this hook will probably not do what you
+want, because the verbose flag may not yet have been turned on.
+
diff --git a/config b/config
index 22929fbeb85424fe5e5ed11613af0f88c36d947a..4c696bec848cbfc299d5e96f58f4965e71a467f1 100644 (file)
--- a/config
+++ b/config
@@ -12,7 +12,8 @@
 
 # rawdog can be extended using plugin modules written in Python. This
 # option specifies the directories to search for plugins to load. If a
-# directory does not exist or cannot be read, it will be ignored.
+# directory does not exist or cannot be read, it will be ignored.  This
+# option must appear before any options that are implemented by plugins.
 plugindirs plugins
 
 # The maximum number of articles to show on the generated page.
index eb81c942293887468cae595fadb5e22d07b14a21..2844e8c63a9d66f6264c6dff20a3aaa205692702 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 = []
+plugin_count = 0
 
-               for file in files:
-                       if file == "" or file[0] == ".":
-                               continue
+def load_plugins(dir, config):
+       global plugin_count
 
-                       desc = None
-                       for d in imp.get_suffixes():
-                               if file.endswith(d[0]):
-                                       desc = d
-                       if desc is None:
-                               continue
+       try:
+               files = os.listdir(dir)
+       except OSError:
+               # Ignore directories that can't be read.
+               return
 
-                       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()
+       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 = {}
 
index 8d86ce4c0067dfdb738192f41ef00adedb67e357..6e05dd5ed0bbac65d2bf189d274c19e5d1f0e94c 100644 (file)
@@ -481,7 +481,6 @@ class Config:
                        "feedslist" : [],
                        "feeddefaults" : {},
                        "defines" : {},
-                       "plugindirs" : [],
                        "outputfile" : "output.html",
                        "maxarticles" : 200,
                        "maxage" : 0,
@@ -555,7 +554,8 @@ class Config:
                                raise ConfigError("Bad line in config: " + line)
                        self["defines"][l[0]] = l[1]
                elif l[0] == "plugindirs":
-                       self["plugindirs"] = parse_list(l[1])
+                       for dir in parse_list(l[1]):
+                               plugins.load_plugins(dir, self)
                elif l[0] == "outputfile":
                        self["outputfile"] = l[1]
                elif l[0] == "maxarticles":
@@ -602,7 +602,7 @@ class Config:
                        self["changeconfig"] = parse_bool(l[1])
                elif l[0] == "include":
                        self.load(l[1], False)
-               else:
+               elif not plugins.call_hook("config_option", self, l[0], l[1]):
                        raise ConfigError("Unknown config command: " + l[0])
 
        def log(self, *args):
@@ -1119,7 +1119,6 @@ def main(argv):
 
        rawdog.sync_from_config(config)
 
-       plugins.load_plugins(config)
        plugins.call_hook("startup", rawdog, config)
        stats = Stats()