From 02c11aaaeabab2f00dfbf5136ef003df63d3f1c7 Mon Sep 17 00:00:00 2001 From: Olivier Mangin Date: Fri, 12 May 2017 18:33:33 -0400 Subject: [PATCH] Adds completion for citekeys. --- pubs/commands/add_cmd.py | 2 +- pubs/commands/conf_cmd.py | 2 +- pubs/commands/doc_cmd.py | 43 ++++++++++++++++++++++------------ pubs/commands/edit_cmd.py | 18 ++++++++------ pubs/commands/export_cmd.py | 7 ++++-- pubs/commands/import_cmd.py | 2 +- pubs/commands/init_cmd.py | 3 ++- pubs/commands/list_cmd.py | 2 +- pubs/commands/note_cmd.py | 10 ++++---- pubs/commands/remove_cmd.py | 7 ++++-- pubs/commands/rename_cmd.py | 18 +++++++------- pubs/commands/tag_cmd.py | 5 ++-- pubs/commands/websearch_cmd.py | 3 ++- pubs/completion.py | 38 ++++++++++++++++++++++++++++++ pubs/pubs_cmd.py | 11 ++++----- readme.md | 2 +- 16 files changed, 118 insertions(+), 55 deletions(-) create mode 100644 pubs/completion.py diff --git a/pubs/commands/add_cmd.py b/pubs/commands/add_cmd.py index 95a07a2..8c5db77 100644 --- a/pubs/commands/add_cmd.py +++ b/pubs/commands/add_cmd.py @@ -9,7 +9,7 @@ from .. import color from .. import pretty -def parser(subparsers): +def parser(subparsers, conf): parser = subparsers.add_parser('add', help='add a paper to the repository') parser.add_argument('bibfile', nargs='?', default=None, help='bibtex file') diff --git a/pubs/commands/conf_cmd.py b/pubs/commands/conf_cmd.py index 75eed5a..eceaf8d 100644 --- a/pubs/commands/conf_cmd.py +++ b/pubs/commands/conf_cmd.py @@ -3,7 +3,7 @@ from .. import config from .. import content -def parser(subparsers): +def parser(subparsers, conf): parser = subparsers.add_parser('conf', help='open the configuration in an editor') return parser diff --git a/pubs/commands/doc_cmd.py b/pubs/commands/doc_cmd.py index 96bcf0f..c8f34eb 100644 --- a/pubs/commands/doc_cmd.py +++ b/pubs/commands/doc_cmd.py @@ -6,6 +6,8 @@ from .. import color from ..uis import get_ui from .. import content from ..utils import resolve_citekey, resolve_citekey_list +from ..completion import CiteKeyCompletion + # doc --+- add $file $key [[-L|--link] | [-M|--move]] [-f|--force] # +- remove $key [$key [...]] [-f|--force] @@ -13,35 +15,46 @@ from ..utils import resolve_citekey, resolve_citekey_list # +- open $key [-w|--with $cmd] # supplements attach, open -def parser(subparsers): - doc_parser = subparsers.add_parser('doc', help='manage the document relating to a publication') - doc_subparsers = doc_parser.add_subparsers(title='document actions', help='actions to interact with the documents', - dest='action') +def parser(subparsers, conf): + doc_parser = subparsers.add_parser( + 'doc', + help='manage the document relating to a publication') + doc_subparsers = doc_parser.add_subparsers( + title='document actions', dest='action', + help='actions to interact with the documents') doc_subparsers.required = True add_parser = doc_subparsers.add_parser('add', help='add a document to a publication') add_parser.add_argument('-f', '--force', action='store_true', dest='force', default=False, - help='force overwriting an already assigned document') + help='force overwriting an already assigned document') add_parser.add_argument('document', nargs=1, help='document file to assign') - add_parser.add_argument('citekey', nargs=1, help='citekey of the publication') + add_parser.add_argument('citekey', nargs=1, help='citekey of the publication' + ).completer = CiteKeyCompletion(conf) add_exclusives = add_parser.add_mutually_exclusive_group() - add_exclusives.add_argument('-L', '--link', action='store_false', dest='link', default=False, - help='do not copy document files, just create a link') - add_exclusives.add_argument('-M', '--move', action='store_true', dest='move', default=False, - help='move document instead of of copying (ignored if --link)') + add_exclusives.add_argument( + '-L', '--link', action='store_false', dest='link', default=False, + help='do not copy document files, just create a link') + add_exclusives.add_argument( + '-M', '--move', action='store_true', dest='move', default=False, + help='move document instead of of copying (ignored if --link)') remove_parser = doc_subparsers.add_parser('remove', help='remove assigned documents from publications') - remove_parser.add_argument('citekeys', nargs='+', help='citekeys of the publications') - remove_parser.add_argument('-f', '--force', action='store_true', dest='force', default=False, - help='force removing assigned documents') + remove_parser.add_argument('citekeys', nargs='+', help='citekeys of the publications' + ).completer = CiteKeyCompletion(conf) + remove_parser.add_argument('-f', '--force', action='store_true', dest='force', + default=False, + help='force removing assigned documents') # favor key+ path over: key export_parser = doc_subparsers.add_parser('export', help='export assigned documents to given path') - export_parser.add_argument('citekeys', nargs='+', help='citekeys of the documents to export') + export_parser.add_argument('citekeys', nargs='+', + help='citekeys of the documents to export' + ).completer = CiteKeyCompletion(conf) export_parser.add_argument('path', nargs=1, help='directory to export the files to') open_parser = doc_subparsers.add_parser('open', help='open an assigned document') - open_parser.add_argument('citekey', nargs=1, help='citekey of the document to open') + open_parser.add_argument('citekey', nargs=1, help='citekey of the document to open' + ).completer = CiteKeyCompletion(conf) open_parser.add_argument('-w', '--with', dest='cmd', help='command to open the file with') return doc_parser diff --git a/pubs/commands/edit_cmd.py b/pubs/commands/edit_cmd.py index 60f04d6..0af4e5e 100644 --- a/pubs/commands/edit_cmd.py +++ b/pubs/commands/edit_cmd.py @@ -4,15 +4,19 @@ from .. import repo from ..uis import get_ui from ..endecoder import EnDecoder from ..utils import resolve_citekey +from ..completion import CiteKeyCompletion -def parser(subparsers): - parser = subparsers.add_parser('edit', - help='open the paper bibliographic file in an editor') - parser.add_argument('-m', '--meta', action='store_true', default=False, - help='edit metadata') - parser.add_argument('citekey', - help='citekey of the paper') +def parser(subparsers, conf): + parser = subparsers.add_parser( + 'edit', + help='open the paper bibliographic file in an editor') + parser.add_argument( + '-m', '--meta', action='store_true', default=False, + help='edit metadata') + parser.add_argument( + 'citekey', + help='citekey of the paper').completer = CiteKeyCompletion(conf) return parser diff --git a/pubs/commands/export_cmd.py b/pubs/commands/export_cmd.py index 54e48f8..1224a03 100644 --- a/pubs/commands/export_cmd.py +++ b/pubs/commands/export_cmd.py @@ -4,12 +4,15 @@ from .. import repo from ..uis import get_ui from .. import endecoder from ..utils import resolve_citekey_list +from ..completion import CiteKeyCompletion -def parser(subparsers): + +def parser(subparsers, conf): parser = subparsers.add_parser('export', help='export bibliography') # parser.add_argument('-f', '--bib-format', default='bibtex', # help='export format') - parser.add_argument('citekeys', nargs='*', help='one or several citekeys') + parser.add_argument('citekeys', nargs='*', help='one or several citekeys' + ).completer = CiteKeyCompletion(conf) return parser diff --git a/pubs/commands/import_cmd.py b/pubs/commands/import_cmd.py index 990fc8c..03935b4 100644 --- a/pubs/commands/import_cmd.py +++ b/pubs/commands/import_cmd.py @@ -11,7 +11,7 @@ from ..uis import get_ui from ..content import system_path, read_text_file -def parser(subparsers): +def parser(subparsers, conf): parser = subparsers.add_parser('import', help='import paper(s) to the repository') parser.add_argument('bibpath', diff --git a/pubs/commands/init_cmd.py b/pubs/commands/init_cmd.py index f4f2a4c..322800e 100644 --- a/pubs/commands/init_cmd.py +++ b/pubs/commands/init_cmd.py @@ -9,7 +9,8 @@ from ..repo import Repository from ..content import system_path, check_directory from .. import config -def parser(subparsers): + +def parser(subparsers, conf): parser = subparsers.add_parser('init', help="initialize the pubs directory") parser.add_argument('-p', '--pubsdir', default=None, diff --git a/pubs/commands/list_cmd.py b/pubs/commands/list_cmd.py index 6901f08..5884cdc 100644 --- a/pubs/commands/list_cmd.py +++ b/pubs/commands/list_cmd.py @@ -10,7 +10,7 @@ class InvalidQuery(ValueError): pass -def parser(subparsers): +def parser(subparsers, conf): parser = subparsers.add_parser('list', help="list papers") parser.add_argument('-k', '--citekeys-only', action='store_true', default=False, dest='citekeys', diff --git a/pubs/commands/note_cmd.py b/pubs/commands/note_cmd.py index 8e1fab1..696f99d 100644 --- a/pubs/commands/note_cmd.py +++ b/pubs/commands/note_cmd.py @@ -2,19 +2,19 @@ from .. import repo from .. import content from ..uis import get_ui from ..utils import resolve_citekey +from ..completion import CiteKeyCompletion -def parser(subparsers): +def parser(subparsers, conf): parser = subparsers.add_parser('note', - help='edit the note attached to a paper') + help='edit the note attached to a paper') parser.add_argument('citekey', - help='citekey of the paper') + help='citekey of the paper' + ).completer = CiteKeyCompletion(conf) return parser def command(conf, args): - """ - """ ui = get_ui() rp = repo.Repository(conf) diff --git a/pubs/commands/remove_cmd.py b/pubs/commands/remove_cmd.py index 83785e2..cbeb9e9 100644 --- a/pubs/commands/remove_cmd.py +++ b/pubs/commands/remove_cmd.py @@ -3,13 +3,16 @@ from .. import color from ..uis import get_ui from ..utils import resolve_citekey_list from ..p3 import ustr +from ..completion import CiteKeyCompletion -def parser(subparsers): + +def parser(subparsers, conf): parser = subparsers.add_parser('remove', help='removes a publication') parser.add_argument('-f', '--force', action='store_true', default=None, help="does not prompt for confirmation.") parser.add_argument('citekeys', nargs='+', - help="one or several citekeys") + help="one or several citekeys" + ).completer = CiteKeyCompletion(conf) return parser diff --git a/pubs/commands/rename_cmd.py b/pubs/commands/rename_cmd.py index dd4b903..ae08f51 100644 --- a/pubs/commands/rename_cmd.py +++ b/pubs/commands/rename_cmd.py @@ -2,13 +2,15 @@ from ..uis import get_ui from .. import color from .. import repo from ..utils import resolve_citekey +from ..completion import CiteKeyCompletion -def parser(subparsers): - parser = subparsers.add_parser('rename', help='rename the citekey of a repository') - parser.add_argument('citekey', - help='current citekey') - parser.add_argument('new_citekey', - help='new citekey') + +def parser(subparsers, conf): + parser = subparsers.add_parser('rename', + help='rename the citekey of a repository') + parser.add_argument('citekey', help='current citekey' + ).completer = CiteKeyCompletion(conf) + parser.add_argument('new_citekey', help='new citekey') return parser @@ -26,7 +28,7 @@ def command(conf, args): paper = rp.pull_paper(key) rp.rename_paper(paper, args.new_citekey) ui.message("The '{}' citekey has been renamed into '{}'".format( - color.dye_out(args.citekey, 'citekey'), - color.dye_out(args.new_citekey, 'citekey'))) + color.dye_out(args.citekey, 'citekey'), + color.dye_out(args.new_citekey, 'citekey'))) rp.close() diff --git a/pubs/commands/tag_cmd.py b/pubs/commands/tag_cmd.py index 5ce91a7..c37eac4 100644 --- a/pubs/commands/tag_cmd.py +++ b/pubs/commands/tag_cmd.py @@ -24,12 +24,13 @@ from ..uis import get_ui from .. import pretty from .. import color from ..utils import resolve_citekey +from ..completion import CiteKeyCompletion -def parser(subparsers): +def parser(subparsers, conf): parser = subparsers.add_parser('tag', help="add, remove and show tags") parser.add_argument('citekeyOrTag', nargs='?', default=None, - help='citekey or tag.') + help='citekey or tag.').completer = CiteKeyCompletion(conf) parser.add_argument('tags', nargs='?', default=None, help='If the previous argument was a citekey, then ' 'a list of tags separated by a +.') diff --git a/pubs/commands/websearch_cmd.py b/pubs/commands/websearch_cmd.py index ee7ee20..7599f0d 100644 --- a/pubs/commands/websearch_cmd.py +++ b/pubs/commands/websearch_cmd.py @@ -3,7 +3,8 @@ import urllib from ..uis import get_ui -def parser(subparsers): + +def parser(subparsers, conf): parser = subparsers.add_parser('websearch', help="launch a search on Google Scholar") parser.add_argument("search_string", nargs = '*', diff --git a/pubs/completion.py b/pubs/completion.py new file mode 100644 index 0000000..3752cb3 --- /dev/null +++ b/pubs/completion.py @@ -0,0 +1,38 @@ +from . import repo +try: + import argcomplete +except ModuleNotFoundError: + + class FakeModule: + + @staticmethod + def _fun(**kwargs): + pass + + def __getattr__(self, _): + return self._fun + + argcomplete = FakeModule() + + +def autocomplete(parser): + argcomplete.autocomplete(parser) + + +class BaseCompleter(object): + + def __init__(self, conf): + self.conf = conf + + def __call__(self, **kwargs): + try: + return self._complete(**kwargs) + except Exception as e: + argcomplete.warn(e) + + +class CiteKeyCompletion(BaseCompleter): + + def _complete(self, **kwargs): + rp = repo.Repository(self.conf) + return rp.citekeys diff --git a/pubs/pubs_cmd.py b/pubs/pubs_cmd.py index 976be3e..b1c28c8 100644 --- a/pubs/pubs_cmd.py +++ b/pubs/pubs_cmd.py @@ -7,10 +7,7 @@ from . import commands from . import update from . import plugins from .__init__ import __version__ -try: - import argcomplete -except ModuleNotFoundError: - argcomplete = None +from .completion import autocomplete CORE_CMDS = collections.OrderedDict([ @@ -77,7 +74,7 @@ def execute(raw_args=sys.argv): # Populate the parser with core commands for cmd_name, cmd_mod in CORE_CMDS.items(): - cmd_parser = cmd_mod.parser(subparsers) + cmd_parser = cmd_mod.parser(subparsers, conf) cmd_parser.set_defaults(func=cmd_mod.command) # Extend with plugin commands @@ -85,8 +82,8 @@ def execute(raw_args=sys.argv): for p in plugins.get_plugins().values(): p.update_parser(subparsers) - if argcomplete is not None: - argcomplete.autocomplete(parser) + # Eventually autocomplete + autocomplete(parser) # Parse and run appropriate command args = parser.parse_args(remaining_args) args.prog = "pubs" # FIXME? diff --git a/readme.md b/readme.md index 960b6f3..9be385f 100644 --- a/readme.md +++ b/readme.md @@ -26,7 +26,7 @@ Alternatively Arch Linux users can also use the [pubs-git](https://aur.archlinux ## Getting started -Create your library (by default, goes to '~/.pubs/'). +Create your library (by default, goes to `~/.pubs/`). pubs init