diff --git a/pubs/commands/edit_cmd.py b/pubs/commands/edit_cmd.py index 30ab5e8..d563dc6 100644 --- a/pubs/commands/edit_cmd.py +++ b/pubs/commands/edit_cmd.py @@ -3,7 +3,7 @@ from .. import repo from ..configs import config from ..uis import get_ui from ..endecoder import EnDecoder - +from ..utils import resolve_citekey def parser(subparsers): parser = subparsers.add_parser('edit', @@ -19,14 +19,10 @@ def command(args): ui = get_ui() meta = args.meta - citekey = args.citekey rp = repo.Repository(config()) - try: - paper = rp.pull_paper(citekey) - except repo.InvalidReference as v: - ui.error(v) - ui.exit(1) + citekey = resolve_citekey(rp, args.citekey, ui=ui, exit_on_fail=True) + paper = rp.pull_paper(citekey) coder = EnDecoder() if meta: diff --git a/pubs/commands/open_cmd.py b/pubs/commands/open_cmd.py index ccbc040..5a1f589 100644 --- a/pubs/commands/open_cmd.py +++ b/pubs/commands/open_cmd.py @@ -5,7 +5,7 @@ from ..configs import config from ..uis import get_ui from .. import color from ..content import system_path - +from ..utils import resolve_citekey def parser(subparsers): parser = subparsers.add_parser('open', @@ -21,10 +21,11 @@ def command(args): ui = get_ui() with_command = args.with_command - citekey = args.citekey rp = repo.Repository(config()) + citekey = resolve_citekey(rp, args.citekey, ui=ui, exit_on_fail=True) paper = rp.pull_paper(citekey) + if with_command is None: with_command = config().open_cmd diff --git a/pubs/repo.py b/pubs/repo.py index 2873f51..5953c3b 100644 --- a/pubs/repo.py +++ b/pubs/repo.py @@ -50,6 +50,11 @@ class Repository(object): for key in self.citekeys: yield self.pull_paper(key) + def citekeys_from_prefix(self, prefix): + """Return all citekey beginning with prefix.""" + return tuple(citekey for citekey in self.citekeys + if citekey.startswith(prefix)) + def pull_paper(self, citekey): """Load a paper by its citekey from disk, if necessary.""" if citekey in self: diff --git a/pubs/uis.py b/pubs/uis.py index a01ad52..441dfb0 100644 --- a/pubs/uis.py +++ b/pubs/uis.py @@ -89,7 +89,7 @@ class UI: if default is not None: return default else: - try: + try: # FIXME options handling !!! return option_chars.index(answer.lower()) except ValueError: pass diff --git a/pubs/utils.py b/pubs/utils.py new file mode 100644 index 0000000..7a9f934 --- /dev/null +++ b/pubs/utils.py @@ -0,0 +1,24 @@ +# Function here may belong somewhere else. In the mean time... + +def resolve_citekey(repo, citekey, ui=None, exit_on_fail=True): + """Check that a citekey exists, or autocompletes it if not ambiguous.""" + # FIXME. Make me optionally non ui interactive/exiting + citekeys = repo.citekeys_from_prefix(citekey) + if len(citekeys) == 0: + if ui is not None: + ui.error("No citekey named or beginning with '{}".format(citekey)) + if exit_on_fail: + ui.exit() + elif len(citekeys) == 1: + if citekeys[0] != citekey: + if ui is not None: + ui.print_("Provided citekey '{}' has been autocompleted into '{}'".format(citekey, citekeys[0])) + citekey = citekeys[0] + elif citekey not in citekeys: + if ui is not None: + ui.error("Be more specific. Provided citekey '{}' matches multiples citekeys: {}".format(citekey, ', '.join(citekeys))) + if exit_on_fail: + ui.exit() + return citekey + +