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.
This commit is contained in:
parent
6b738a3f6c
commit
6e68bd5251
@ -11,6 +11,7 @@ from . import commands
|
||||
from . import plugins
|
||||
from .__init__ import __version__
|
||||
|
||||
|
||||
cmds = collections.OrderedDict([
|
||||
('init', commands.init_cmd),
|
||||
('add', commands.add_cmd),
|
||||
@ -26,18 +27,20 @@ cmds = collections.OrderedDict([
|
||||
('update', commands.update_cmd),
|
||||
])
|
||||
|
||||
|
||||
def _update_check(config, ui):
|
||||
if config.version_warning:
|
||||
code_version = int(__version__)
|
||||
repo_version = int(config.version)
|
||||
|
||||
if repo_version > code_version:
|
||||
ui.print_('warning: your repository was generated with an newer version'
|
||||
' of papers (v{}) than the one you are using (v{}).\n'.format(
|
||||
repo_version, code_version) +
|
||||
' you should not use papers until you install the '
|
||||
'newest version. (use version_warning in you papersrc '
|
||||
'to bypass this error)')
|
||||
ui.warning(
|
||||
'your repository was generated with an newer version'
|
||||
' of papers (v{}) than the one you are using (v{}).'
|
||||
'\n'.format(repo_version, code_version) +
|
||||
'You should not use papers until you install the '
|
||||
'newest version. (use version_warning in you papersrc '
|
||||
'to bypass this error)')
|
||||
sys.exit()
|
||||
elif repo_version < code_version:
|
||||
ui.print_(
|
||||
@ -47,7 +50,7 @@ def _update_check(config, ui):
|
||||
sys.exit()
|
||||
|
||||
|
||||
def execute(raw_args = sys.argv):
|
||||
def execute(raw_args=sys.argv):
|
||||
# loading config
|
||||
config = configs.Config()
|
||||
config.load()
|
||||
@ -58,17 +61,16 @@ def execute(raw_args = sys.argv):
|
||||
|
||||
_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")
|
||||
subparsers = parser.add_subparsers(title="valid commands", dest="command")
|
||||
|
||||
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:])
|
||||
cmd = args.command
|
||||
|
@ -11,34 +11,14 @@ class PapersPlugin(object):
|
||||
functionality by defining a subclass of PapersPlugin and overriding
|
||||
the abstract methods defined here.
|
||||
"""
|
||||
def __init__(self):
|
||||
"""Perform one-time plugin setup.
|
||||
|
||||
name = None
|
||||
|
||||
def get_commands(self, subparsers):
|
||||
"""Populates the parser with plugins specific command.
|
||||
Returns iterable of pairs (command name, command function to call).
|
||||
"""
|
||||
self.name = self.__module__.split('.')[-1]
|
||||
|
||||
#ui and given again to stay consistent with the core papers cmd.
|
||||
#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):
|
||||
"""This function will be called with argument defined in the parser above
|
||||
This is a basic example
|
||||
"""
|
||||
|
||||
strings = args.strings
|
||||
|
||||
for s in strings:
|
||||
print(s)
|
||||
return []
|
||||
|
||||
@classmethod
|
||||
def get_instance(cls):
|
||||
|
@ -36,9 +36,9 @@ DFT_BIBSTYLE_INFO = 'ieeetr'
|
||||
|
||||
class TexnotePlugin(PapersPlugin):
|
||||
|
||||
def __init__(self):
|
||||
self.name = SECTION
|
||||
name = SECTION
|
||||
|
||||
def __init__(self):
|
||||
self.texcmds = collections.OrderedDict([
|
||||
('remove', self.remove),
|
||||
('edit', self.edit),
|
||||
@ -61,7 +61,7 @@ class TexnotePlugin(PapersPlugin):
|
||||
if not files.check_file(TPL_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')
|
||||
sub = parser.add_subparsers(title='valid texnote commands', dest='texcmd')
|
||||
# remove
|
||||
@ -113,7 +113,7 @@ class TexnotePlugin(PapersPlugin):
|
||||
p = sub.add_parser('extract_note',
|
||||
help='extract core note from its reference')
|
||||
add_references_argument(p, single=True)
|
||||
return parser
|
||||
return [(self.name, self.command)]
|
||||
|
||||
def command(self, args):
|
||||
self._ensure_init()
|
||||
|
Loading…
x
Reference in New Issue
Block a user