Simplifies loading of plugins parsers.

- Plugins now only present a get_commands method taking the parser to
  populate as argument and returning an iterable with which the cmds
  dictionary is updated (i.e. the plugins returns the command callbacks).
- The name attribute in plugins is now a class attribute.
main
Olivier Mangin 12 years ago
parent 6b738a3f6c
commit 6e68bd5251

@ -11,6 +11,7 @@ from . import commands
from . import plugins from . import plugins
from .__init__ import __version__ from .__init__ import __version__
cmds = collections.OrderedDict([ cmds = collections.OrderedDict([
('init', commands.init_cmd), ('init', commands.init_cmd),
('add', commands.add_cmd), ('add', commands.add_cmd),
@ -26,16 +27,18 @@ cmds = collections.OrderedDict([
('update', commands.update_cmd), ('update', commands.update_cmd),
]) ])
def _update_check(config, ui): def _update_check(config, ui):
if config.version_warning: if config.version_warning:
code_version = int(__version__) code_version = int(__version__)
repo_version = int(config.version) repo_version = int(config.version)
if repo_version > code_version: if repo_version > code_version:
ui.print_('warning: your repository was generated with an newer version' ui.warning(
' of papers (v{}) than the one you are using (v{}).\n'.format( 'your repository was generated with an newer version'
repo_version, code_version) + ' of papers (v{}) than the one you are using (v{}).'
' you should not use papers until you install the ' '\n'.format(repo_version, code_version) +
'You should not use papers until you install the '
'newest version. (use version_warning in you papersrc ' 'newest version. (use version_warning in you papersrc '
'to bypass this error)') 'to bypass this error)')
sys.exit() sys.exit()
@ -58,17 +61,16 @@ def execute(raw_args = sys.argv):
_update_check(config, ui) _update_check(config, ui)
# Extend with plugin commands
plugins.load_plugins(ui, config.plugins.split())
for p in plugins.get_plugins().values():
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")
for cmd_mod in cmds.values(): for cmd_mod in cmds.values():
subparser = cmd_mod.parser(subparsers) # why do we return the subparser ? cmd_mod.parser(subparsers)
# Extend with plugin commands
plugins.load_plugins(ui, config.plugins.split())
for p in plugins.get_plugins().values():
cmds.update(p.get_commands(subparsers))
args = parser.parse_args(raw_args[1:]) args = parser.parse_args(raw_args[1:])
cmd = args.command cmd = args.command

@ -11,34 +11,14 @@ 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):
"""Perform one-time plugin setup.
"""
self.name = self.__module__.split('.')[-1]
#ui and given again to stay consistent with the core papers cmd. name = None
#two options:
#- create specific cases in script papers/papers
#- do not store self.ui and use them if needed when command is called
#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 return the parser with plugins specific command.
This is a basic example
"""
parser = subparsers.add_parser(self.name, help="echo string in argument")
parser.add_argument('strings', nargs='*', help='the strings')
return parser
def command(self, args): def get_commands(self, subparsers):
"""This function will be called with argument defined in the parser above """Populates the parser with plugins specific command.
This is a basic example Returns iterable of pairs (command name, command function to call).
""" """
return []
strings = args.strings
for s in strings:
print(s)
@classmethod @classmethod
def get_instance(cls): def get_instance(cls):

@ -36,9 +36,9 @@ DFT_BIBSTYLE_INFO = 'ieeetr'
class TexnotePlugin(PapersPlugin): class TexnotePlugin(PapersPlugin):
def __init__(self): name = SECTION
self.name = SECTION
def __init__(self):
self.texcmds = collections.OrderedDict([ self.texcmds = collections.OrderedDict([
('remove', self.remove), ('remove', self.remove),
('edit', self.edit), ('edit', self.edit),
@ -61,7 +61,7 @@ class TexnotePlugin(PapersPlugin):
if not files.check_file(TPL_BIB): if not files.check_file(TPL_BIB):
self.generate_bib() self.generate_bib()
def parser(self, subparsers): def get_commands(self, subparsers):
parser = subparsers.add_parser(self.name, help='edit advance note in latex') parser = subparsers.add_parser(self.name, help='edit advance note in latex')
sub = parser.add_subparsers(title='valid texnote commands', dest='texcmd') sub = parser.add_subparsers(title='valid texnote commands', dest='texcmd')
# remove # remove
@ -113,7 +113,7 @@ class TexnotePlugin(PapersPlugin):
p = sub.add_parser('extract_note', p = sub.add_parser('extract_note',
help='extract core note from its reference') help='extract core note from its reference')
add_references_argument(p, single=True) add_references_argument(p, single=True)
return parser return [(self.name, self.command)]
def command(self, args): def command(self, args):
self._ensure_init() self._ensure_init()

Loading…
Cancel
Save