From f29f1a96b544b951ea71122f15d7734f0994cfe8 Mon Sep 17 00:00:00 2001 From: Jonathan Grizou Date: Thu, 4 Jul 2013 00:08:00 +0200 Subject: [PATCH] Check if plugin as a parser command before adding it into the OrderedList. Indeed some plugins may not need to define one. --- papers/papers_cmd.py | 3 ++- papers/plugins.py | 35 +++++++++++++++++------------------ 2 files changed, 19 insertions(+), 19 deletions(-) diff --git a/papers/papers_cmd.py b/papers/papers_cmd.py index dd118e9..9156324 100644 --- a/papers/papers_cmd.py +++ b/papers/papers_cmd.py @@ -38,7 +38,8 @@ def execute(raw_args = sys.argv): # Extend with plugin commands plugins.load_plugins(ui, config.plugins.split()) for p in plugins.get_plugins().values(): - cmds.update(collections.OrderedDict([(p.name, p)])) + if getattr(p, 'parser') and getattr(p, 'command'): + cmds.update(collections.OrderedDict([(p.name, p)])) parser = argparse.ArgumentParser(description="research papers repository") subparsers = parser.add_subparsers(title="valid commands", dest="command") diff --git a/papers/plugins.py b/papers/plugins.py index 296b496..062196e 100644 --- a/papers/plugins.py +++ b/papers/plugins.py @@ -12,11 +12,10 @@ class PapersPlugin(object): functionality by defining a subclass of PapersPlugin and overriding the abstract methods defined here. """ - def __init__(self, ui): + def __init__(self): """Perform one-time plugin setup. """ self.name = self.__module__.split('.')[-1] - self.ui = ui #ui and given again to stay consistent with the core papers cmd. #two options: @@ -25,7 +24,7 @@ class PapersPlugin(object): #this may end up with a lot of function with config/ui in argument #or just keep it that way... def parser(self, subparsers): - """ Should retrun the parser with plugins specific command. + """ Should return the parser with plugins specific command. This is a basic example """ parser = subparsers.add_parser(self.name, help="echo string in argument") @@ -59,24 +58,24 @@ def load_plugins(ui, names): """ for name in names: modname = '%s.%s.%s.%s' % ('papers', PLUGIN_NAMESPACE, name, name) + #try: try: - try: - namespace = importlib.import_module(modname) - except ImportError as exc: - # Again, this is hacky: - if exc.args[0].endswith(' ' + name): - ui.warning('plugin {} not found'.format(name)) - else: - raise + namespace = importlib.import_module(modname) + except ImportError as exc: + # Again, this is hacky: + if exc.args[0].endswith(' ' + name): + ui.warning('plugin {} not found'.format(name)) else: - for obj in namespace.__dict__.values(): - if isinstance(obj, type) and issubclass(obj, PapersPlugin) \ - and obj != PapersPlugin: - _classes.append(obj) - _instances[obj] = obj(ui) + raise + else: + for obj in namespace.__dict__.values(): + if isinstance(obj, type) and issubclass(obj, PapersPlugin) \ + and obj != PapersPlugin: + _classes.append(obj) + _instances[obj] = obj() - except: - ui.warning('error loading plugin {}'.format(name)) + #except: + # ui.warning('error loading plugin {}'.format(name)) def get_plugins():