diff --git a/papers/papers_cmd.py b/papers/papers_cmd.py index b64ebcb..5a4041e 100644 --- a/papers/papers_cmd.py +++ b/papers/papers_cmd.py @@ -12,7 +12,7 @@ from . import plugins from .__init__ import __version__ -cmds = collections.OrderedDict([ +CORE_CMDS = collections.OrderedDict([ ('init', commands.init_cmd), ('add', commands.add_cmd), ('import', commands.import_cmd), @@ -64,16 +64,19 @@ def execute(raw_args=sys.argv): parser = argparse.ArgumentParser(description="research papers repository") subparsers = parser.add_subparsers(title="valid commands", dest="command") - for cmd_mod in cmds.values(): + cmd_funcs = collections.OrderedDict() + for cmd_name, cmd_mod in CORE_CMDS.items(): cmd_mod.parser(subparsers) + cmd_funcs[cmd_name] = cmd_mod.command # Extend with plugin commands plugins.load_plugins(ui, config.plugins.split()) for p in plugins.get_plugins().values(): - cmds.update(p.get_commands(subparsers)) + cmd_funcs.update(p.get_commands(subparsers)) args = parser.parse_args(raw_args[1:]) + args.prog = parser.prog # Hack: there might be a better way... cmd = args.command del args.command - cmds[cmd].command(args) + cmd_funcs[cmd](args) diff --git a/papers/plugs/alias/__init__.py b/papers/plugs/alias/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/papers/plugs/alias/alias.py b/papers/plugs/alias/alias.py new file mode 100644 index 0000000..078f8ac --- /dev/null +++ b/papers/plugs/alias/alias.py @@ -0,0 +1,70 @@ +import subprocess +import shlex + +from ...plugins import PapersPlugin +from ...configs import config +from ...papers_cmd import execute + + +class Alias(object): + + def __init__(self, name, definition): + self.name = name + self.definition = definition + + def parser(self, parser): + self.parser = parser + p = parser.add_parser(self.name, help='user defined command') + p.add_argument('arguments', nargs='*', + help="arguments to be passed to the user defined command") + + def run(self, args): + raise NotImplementedError + + @classmethod + def create_alias(cls, name, definition): + if len(definition) > 0 and definition[0] == '!': + return ShellAlias(name, definition[1:]) + else: + return CommandAlias(name, definition) + + +class CommandAlias(Alias): + """Default kind of alias. + - definition is used as a papers command + - other arguments are passed to the command + """ + + def run(self, args): + raw_args = ([args.prog] + + shlex.split(self.definition + + ' ' + + ' '.join(args.arguments))) + execute(raw_args) + + +class ShellAlias(Alias): + + def run(self, args): + """Uses a shell function so that arguments can be used in the command + as shell arguments. + """ + subprocess.call( + 'papers_alias_fun () {{\n{}\n}}\npapers_alias_fun {}'.format( + self.definition, ' '.join(args.arguments)), + shell=True) + + +class AliasPlugin(PapersPlugin): + + name = 'alias' + + def __init__(self): + self.aliases = [] + for name, definition in config('alias').items(): + self.aliases.append(Alias.create_alias(name, definition)) + + def get_commands(self, parser): + for a in self.aliases: + a.parser(parser) + return [(a.name, a.run) for a in self.aliases]