Adds alias plugin.
This commit is contained in:
parent
6e68bd5251
commit
68e2a23a3c
@ -12,7 +12,7 @@ from . import plugins
|
|||||||
from .__init__ import __version__
|
from .__init__ import __version__
|
||||||
|
|
||||||
|
|
||||||
cmds = collections.OrderedDict([
|
CORE_CMDS = collections.OrderedDict([
|
||||||
('init', commands.init_cmd),
|
('init', commands.init_cmd),
|
||||||
('add', commands.add_cmd),
|
('add', commands.add_cmd),
|
||||||
('import', commands.import_cmd),
|
('import', commands.import_cmd),
|
||||||
@ -64,16 +64,19 @@ def execute(raw_args=sys.argv):
|
|||||||
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():
|
cmd_funcs = collections.OrderedDict()
|
||||||
|
for cmd_name, cmd_mod in CORE_CMDS.items():
|
||||||
cmd_mod.parser(subparsers)
|
cmd_mod.parser(subparsers)
|
||||||
|
cmd_funcs[cmd_name] = cmd_mod.command
|
||||||
|
|
||||||
# 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(p.get_commands(subparsers))
|
cmd_funcs.update(p.get_commands(subparsers))
|
||||||
|
|
||||||
args = parser.parse_args(raw_args[1:])
|
args = parser.parse_args(raw_args[1:])
|
||||||
|
args.prog = parser.prog # Hack: there might be a better way...
|
||||||
cmd = args.command
|
cmd = args.command
|
||||||
del args.command
|
del args.command
|
||||||
|
|
||||||
cmds[cmd].command(args)
|
cmd_funcs[cmd](args)
|
||||||
|
0
papers/plugs/alias/__init__.py
Normal file
0
papers/plugs/alias/__init__.py
Normal file
70
papers/plugs/alias/alias.py
Normal file
70
papers/plugs/alias/alias.py
Normal file
@ -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]
|
Loading…
x
Reference in New Issue
Block a user