Check if plugin as a parser command before adding it into the OrderedList.

Indeed some plugins may not need to define one.
main
Jonathan Grizou 12 years ago
parent fa836aaad9
commit f29f1a96b5

@ -38,7 +38,8 @@ def execute(raw_args = sys.argv):
# Extend with plugin commands # Extend with plugin commands
plugins.load_plugins(ui, config.plugins.split()) plugins.load_plugins(ui, config.plugins.split())
for p in plugins.get_plugins().values(): 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") parser = argparse.ArgumentParser(description="research papers repository")
subparsers = parser.add_subparsers(title="valid commands", dest="command") subparsers = parser.add_subparsers(title="valid commands", dest="command")

@ -12,11 +12,10 @@ class PapersPlugin(object):
functionality by defining a subclass of PapersPlugin and overriding functionality by defining a subclass of PapersPlugin and overriding
the abstract methods defined here. the abstract methods defined here.
""" """
def __init__(self, ui): def __init__(self):
"""Perform one-time plugin setup. """Perform one-time plugin setup.
""" """
self.name = self.__module__.split('.')[-1] self.name = self.__module__.split('.')[-1]
self.ui = ui
#ui and given again to stay consistent with the core papers cmd. #ui and given again to stay consistent with the core papers cmd.
#two options: #two options:
@ -25,7 +24,7 @@ class PapersPlugin(object):
#this may end up with a lot of function with config/ui in argument #this may end up with a lot of function with config/ui in argument
#or just keep it that way... #or just keep it that way...
def parser(self, subparsers): 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 This is a basic example
""" """
parser = subparsers.add_parser(self.name, help="echo string in argument") parser = subparsers.add_parser(self.name, help="echo string in argument")
@ -59,24 +58,24 @@ def load_plugins(ui, names):
""" """
for name in names: for name in names:
modname = '%s.%s.%s.%s' % ('papers', PLUGIN_NAMESPACE, name, name) modname = '%s.%s.%s.%s' % ('papers', PLUGIN_NAMESPACE, name, name)
#try:
try: try:
try: namespace = importlib.import_module(modname)
namespace = importlib.import_module(modname) except ImportError as exc:
except ImportError as exc: # Again, this is hacky:
# Again, this is hacky: if exc.args[0].endswith(' ' + name):
if exc.args[0].endswith(' ' + name): ui.warning('plugin {} not found'.format(name))
ui.warning('plugin {} not found'.format(name))
else:
raise
else: else:
for obj in namespace.__dict__.values(): raise
if isinstance(obj, type) and issubclass(obj, PapersPlugin) \ else:
and obj != PapersPlugin: for obj in namespace.__dict__.values():
_classes.append(obj) if isinstance(obj, type) and issubclass(obj, PapersPlugin) \
_instances[obj] = obj(ui) and obj != PapersPlugin:
_classes.append(obj)
_instances[obj] = obj()
except: #except:
ui.warning('error loading plugin {}'.format(name)) # ui.warning('error loading plugin {}'.format(name))
def get_plugins(): def get_plugins():

Loading…
Cancel
Save