From 5cc3f892a0c770976f48cba3ff10bd028f09d057 Mon Sep 17 00:00:00 2001 From: 73 Date: Mon, 14 Dec 2015 16:31:30 +0100 Subject: [PATCH] All commands that consume a citekey as an argument will now complete a prefix. --- pubs/commands/conf_cmd.py | 4 ++-- pubs/commands/doc_cmd.py | 4 ++-- pubs/commands/export_cmd.py | 30 +++++++++++++++--------------- pubs/commands/import_cmd.py | 8 ++++---- pubs/commands/init_cmd.py | 2 +- pubs/commands/note_cmd.py | 14 +++++++------- pubs/commands/tag_cmd.py | 11 +++++++++-- pubs/uis.py | 2 +- pubs/utils.py | 3 ++- 9 files changed, 43 insertions(+), 35 deletions(-) diff --git a/pubs/commands/conf_cmd.py b/pubs/commands/conf_cmd.py index d09d61c..2fe1574 100644 --- a/pubs/commands/conf_cmd.py +++ b/pubs/commands/conf_cmd.py @@ -22,8 +22,8 @@ def command(conf, args): config.check_conf(new_conf) ui.message('The configuration file was updated.') break - except AssertionError: # TODO better error message - ui.error('Error reading the modified configuration file.') + except AssertionError as e: # TODO better error message + ui.error('Error reading the modified configuration file [' + e.message + '].') options = ['edit_again', 'abort'] choice = options[ui.input_choice( options, ['e', 'a'], diff --git a/pubs/commands/doc_cmd.py b/pubs/commands/doc_cmd.py index f221d8c..c031a55 100644 --- a/pubs/commands/doc_cmd.py +++ b/pubs/commands/doc_cmd.py @@ -115,7 +115,7 @@ def command(conf, args): if not path.endswith('/'): path += '/' else: - ui.error('{} is not a directory.'.format( + ui.error('{} is not a directory. Quit.'.format( color.dye_err(args.path[0], 'filepath'))) ui.exit(1) @@ -140,7 +140,7 @@ def command(conf, args): paper = rp.pull_paper(citekey) if paper.docpath is None: - ui.error('No document associated with the entry {}.'.format( + ui.error('No document associated with the entry {}. Quit.'.format( color.dye_err(citekey, 'citekey'))) ui.exit() diff --git a/pubs/commands/export_cmd.py b/pubs/commands/export_cmd.py index a55a791..be559b7 100644 --- a/pubs/commands/export_cmd.py +++ b/pubs/commands/export_cmd.py @@ -3,7 +3,7 @@ from __future__ import print_function from .. import repo from ..uis import get_ui from .. import endecoder - +from ..utils import resolve_citekey_list def parser(subparsers): parser = subparsers.add_parser('export', help='export bibliography') @@ -19,20 +19,20 @@ def command(conf, args): # :param bib_format (only 'bibtex' now) ui = get_ui() - rp = repo.Repository(conf) try: - papers = [rp.pull_paper(c) for c in args.citekeys] - except repo.InvalidReference as v: - ui.error(v) - ui.exit(1) - - if len(papers) == 0: - papers = rp.all_papers() - bib = {} - for p in papers: - bib[p.citekey] = p.bibdata - exporter = endecoder.EnDecoder() - bibdata_raw = exporter.encode_bibdata(bib) - ui.message(bibdata_raw) + papers = [] + if len(args.citekeys) < 1: + papers = rp.all_papers() + else: + for key in resolve_citekey_list(repo=rp, citekeys=args.citekeys, ui=ui, exit_on_fail=True): + papers.append(rp.pull_paper(key)) + bib = {} + for p in papers: + bib[p.citekey] = p.bibdata + exporter = endecoder.EnDecoder() + bibdata_raw = exporter.encode_bibdata(bib) + ui.message(bibdata_raw) + except Exception as e: + ui.error(e.message) \ No newline at end of file diff --git a/pubs/commands/import_cmd.py b/pubs/commands/import_cmd.py index ceda2bd..e9ffa94 100644 --- a/pubs/commands/import_cmd.py +++ b/pubs/commands/import_cmd.py @@ -77,17 +77,17 @@ def command(conf, args): try: p = papers[k] if isinstance(p, Exception): - ui.error('could not load entry for citekey {}.'.format(k)) + ui.error('Could not load entry for citekey {}.'.format(k)) else: rp.push_paper(p) - ui.message('{} imported'.format(color.dye_out(p.citekey, 'citekey'))) + ui.info('{} imported.'.format(color.dye_out(p.citekey, 'citekey'))) docfile = bibstruct.extract_docfile(p.bibdata) if docfile is None: - ui.warning("no file for {}.".format(p.citekey)) + ui.warning("No file for {}.".format(p.citekey)) else: rp.push_doc(p.citekey, docfile, copy=copy) #FIXME should move the file if configured to do so. except KeyError: - ui.error('no entry found for citekey {}.'.format(k)) + ui.error('No entry found for citekey {}.'.format(k)) except IOError as e: ui.error(e.message) diff --git a/pubs/commands/init_cmd.py b/pubs/commands/init_cmd.py index 048e713..e20dd3e 100644 --- a/pubs/commands/init_cmd.py +++ b/pubs/commands/init_cmd.py @@ -33,7 +33,7 @@ def command(conf, args): pubsdir = system_path(pubsdir) if check_directory(pubsdir, fail=False) and len(os.listdir(pubsdir)) > 0: - ui.error('directory {} is not empty.'.format( + ui.error('Directory {} is not empty.'.format( color.dye_err(pubsdir, 'filepath'))) ui.exit() diff --git a/pubs/commands/note_cmd.py b/pubs/commands/note_cmd.py index 77be6d2..f68ef35 100644 --- a/pubs/commands/note_cmd.py +++ b/pubs/commands/note_cmd.py @@ -1,6 +1,7 @@ from .. import repo from .. import content from ..uis import get_ui +from ..utils import resolve_citekey def parser(subparsers): @@ -16,11 +17,10 @@ def command(conf, args): """ ui = get_ui() - rp = repo.Repository(conf) - if not rp.databroker.exists(args.citekey): - ui.error("citekey {} not found".format(args.citekey)) - ui.exit(1) - - notepath = rp.databroker.real_notepath(args.citekey) - content.edit_file(conf['main']['edit_cmd'], notepath, temporary=False) + citekey = resolve_citekey(rp, args.citekey, ui=ui, exit_on_fail=True) + try: + notepath = rp.databroker.real_notepath(citekey) + content.edit_file(conf['main']['edit_cmd'], notepath, temporary=False) + except Exception as e: + ui.error(e.message) \ No newline at end of file diff --git a/pubs/commands/tag_cmd.py b/pubs/commands/tag_cmd.py index 2cd8339..1f05cb3 100644 --- a/pubs/commands/tag_cmd.py +++ b/pubs/commands/tag_cmd.py @@ -23,6 +23,7 @@ from ..repo import Repository from ..uis import get_ui from .. import pretty from .. import color +from ..utils import resolve_citekey def parser(subparsers): @@ -81,7 +82,12 @@ def command(conf, args): if citekeyOrTag is None: ui.message(color.dye_out(' '.join(sorted(rp.get_tags())), 'tag')) else: - if rp.databroker.exists(citekeyOrTag): + not_citekey = False + try: + citekeyOrTag = resolve_citekey(repo=rp, citekey=citekeyOrTag, ui=ui, exit_on_fail=True) + except SystemExit: + not_citekey = True + if not not_citekey: p = rp.pull_paper(citekeyOrTag) if tags is None: ui.message(color.dye_out(' '.join(sorted(p.tags)), 'tag')) @@ -93,9 +99,10 @@ def command(conf, args): p.remove_tag(tag) rp.push_paper(p, overwrite=True) elif tags is not None: - ui.error(ui.error('no entry found for citekey {}.'.format(citekeyOrTag))) + ui.error(ui.error('No entry found for citekey {}.'.format(citekeyOrTag))) ui.exit() else: + ui.info('Assuming {} to be a tag.'.format(color.dye_out(citekeyOrTag))) # case where we want to find papers with specific tags included, excluded = _tag_groups(_parse_tag_seq(citekeyOrTag)) papers_list = [] diff --git a/pubs/uis.py b/pubs/uis.py index 07f45c9..bc503ac 100644 --- a/pubs/uis.py +++ b/pubs/uis.py @@ -112,7 +112,7 @@ class InputUI(PrintUI): option_str = '/'.join(["{}{}".format(color.dye_out(c, 'bold'), s[1:]) for c, s in zip(displayed_chars, options)]) - self.message('{} {}: '.format(question, option_str), end='') + self.message('{}: {} {}: '.format(color.dye_err('prompt', 'warning'), question, option_str), end='') while True: answer = self.input() if answer is None or answer == '': diff --git a/pubs/utils.py b/pubs/utils.py index 76c582c..381f1b5 100644 --- a/pubs/utils.py +++ b/pubs/utils.py @@ -5,6 +5,7 @@ from . import pretty def resolve_citekey(repo, citekey, ui=None, exit_on_fail=True): """Check that a citekey exists, or autocompletes it if not ambiguous.""" + """ :returns found citekey """ # FIXME. Make me optionally non ui interactive/exiting citekeys = repo.citekeys_from_prefix(citekey) if len(citekeys) == 0: @@ -15,7 +16,7 @@ def resolve_citekey(repo, citekey, ui=None, exit_on_fail=True): elif len(citekeys) == 1: if citekeys[0] != citekey: if ui is not None: - ui.info("Provided citekey '{}' has been autocompleted into '{}'".format(color.dye_out(citekey, 'citekey'), color.dye_out(citekeys[0], 'citekey'))) + ui.info("Provided citekey '{}' has been autocompleted into [{}].".format(color.dye_out(citekey, 'citekey'), color.dye_out(citekeys[0], 'citekey'))) citekey = citekeys[0] elif citekey not in citekeys: if ui is not None: