From 4730deeca816578c33be4bfaa16cb99e3f546726 Mon Sep 17 00:00:00 2001 From: Max Goodman Date: Tue, 15 May 2012 16:57:22 -0700 Subject: [PATCH] Build plugin JS in js.py. --- r2/r2/lib/js.py | 13 +++++++++++-- r2/r2/lib/plugin.py | 19 +++++++++++++------ 2 files changed, 24 insertions(+), 8 deletions(-) diff --git a/r2/r2/lib/js.py b/r2/r2/lib/js.py index 56b0bbf85..65f6bf40f 100755 --- a/r2/r2/lib/js.py +++ b/r2/r2/lib/js.py @@ -280,10 +280,19 @@ module["flot"] = Module("jquery.flot.js", def use(*names): return "\n".join(module[name].use() for name in names) +def load_plugin_modules(): + from r2.lib.plugin import PluginLoader + for plugin in PluginLoader.available_plugins(): + plugin_cls = plugin.load() + plugin_cls().add_js(module) + commands = {} def build_command(fn): - commands[fn.__name__] = fn - return fn + def wrapped(*args): + load_plugin_modules() + fn(*args) + commands[fn.__name__] = wrapped + return wrapped @build_command def enumerate_modules(): diff --git a/r2/r2/lib/plugin.py b/r2/r2/lib/plugin.py index c7ad62818..11d6a07d7 100644 --- a/r2/r2/lib/plugin.py +++ b/r2/r2/lib/plugin.py @@ -25,13 +25,16 @@ class Plugin(object): def on_load(self): pass - def add_js(self): - from r2.lib import js + def add_js(self, module_registry=None): + if not module_registry: + from r2.lib import js + module_registry = js.module + for name, module in self.js.iteritems(): - if name not in js.module: - js.module[name] = module + if name not in module_registry: + module_registry[name] = module else: - js.module[name].extend(module) + module_registry[name].extend(module) def add_routes(self, mc): pass @@ -54,11 +57,15 @@ class PluginLoader(object): def __getitem__(self, key): return self.plugins[key] + @staticmethod + def available_plugins(name=None): + return pkg_resources.iter_entry_points('r2.plugin', name) + def load_plugins(self, plugin_names): g = config['pylons.g'] for name in plugin_names: try: - entry_point = pkg_resources.iter_entry_points('r2.plugin', name).next() + entry_point = self.available_plugins(name).next() except StopIteration: g.log.warning('Unable to locate plugin "%s". Skipping.' % name) continue