From 49b32de5e537009e718b7422404e984bad6be007 Mon Sep 17 00:00:00 2001 From: Mason Larobina Date: Sun, 4 Apr 2010 20:17:30 +0800 Subject: Require plugins have either a init, after or cleanup hook. --- examples/data/scripts/uzbl-event-manager | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-) (limited to 'examples/data/scripts/uzbl-event-manager') diff --git a/examples/data/scripts/uzbl-event-manager b/examples/data/scripts/uzbl-event-manager index 75a1c13..ab13fbb 100755 --- a/examples/data/scripts/uzbl-event-manager +++ b/examples/data/scripts/uzbl-event-manager @@ -189,7 +189,8 @@ class Plugin(object): self.handlers = set([]) # Plugins init hook - self.init = getattr(plugin, 'init') + init = getattr(plugin, 'init', None) + self.init = init if callable(init) else None # Plugins optional after hook after = getattr(plugin, 'after', None) @@ -199,6 +200,8 @@ class Plugin(object): cleanup = getattr(plugin, 'cleanup', None) self.cleanup = cleanup if callable(cleanup) else None + assert init or after or cleanup, "missing hooks in plugin" + # Export plugin's instance methods to plugin namespace for attr in self.special_functions: plugin.__dict__[attr] = getattr(self, attr) @@ -328,8 +331,9 @@ class Uzbl(object): # Initialise each plugin with the current uzbl instance. for plugin in self.parent.plugins.values(): - self.logger.debug('calling %r plugin init hook' % plugin.name) - plugin.init(self) + if plugin.init: + self.logger.debug('calling %r plugin init hook' % plugin.name) + plugin.init(self) # Allow plugins to use exported features of other plugins by calling an # optional `after` function in the plugins namespace. @@ -518,8 +522,11 @@ class UzblEventDaemon(object): info = imp.find_module(name, [dir,]) module = imp.load_module(name, *info) - assert callable(getattr(module, 'init', None)),\ - "plugin missing init function: %r" % module + + # Check if the plugin has a callable hook. + hooks = filter(callable, [getattr(module, attr, None) \ + for attr in ['init', 'after', 'cleanup']]) + assert hooks, "no hooks in plugin %r" % module logger.debug('creating plugin instance for %r plugin' % name) plugin = Plugin(self, name, path, module) -- cgit v1.2.3