From 146ea605c388bf75404928e75687a4bca549e7a1 Mon Sep 17 00:00:00 2001 From: Adam Sampson Date: Tue, 21 Dec 2004 19:10:59 +0000 Subject: [PATCH] Implement a config_option hook. This required a bit of rearrangement in order that plugins were actually loaded when the config options were being parsed. --- PLUGINS | 12 +++++++++++ config | 3 ++- rawdoglib/plugins.py | 48 +++++++++++++++++++++++--------------------- rawdoglib/rawdog.py | 7 +++---- 4 files changed, 42 insertions(+), 28 deletions(-) diff --git a/PLUGINS b/PLUGINS index 34d7581..7e9a022 100644 --- 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 22929fb..4c696be 100644 --- 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. diff --git a/rawdoglib/plugins.py b/rawdoglib/plugins.py index eb81c94..2844e8c 100644 --- a/rawdoglib/plugins.py +++ b/rawdoglib/plugins.py @@ -18,32 +18,34 @@ 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 = {} diff --git a/rawdoglib/rawdog.py b/rawdoglib/rawdog.py index 8d86ce4..6e05dd5 100644 --- a/rawdoglib/rawdog.py +++ b/rawdoglib/rawdog.py @@ -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() -- 2.35.1