From ed2bbb44984eff1945931fd6a00908e5e0d9d363 Mon Sep 17 00:00:00 2001 From: Olivier Mangin Date: Wed, 13 Jan 2016 17:31:27 -0500 Subject: [PATCH] Removes generic handling of errors from commands. The default behavior for commands is now to only catch exceptions that must be handled specifically. This includes outputting a context dependant message, cleaning up, etc. All other exceptions will be handled by the ui. --- pubs/commands/add_cmd.py | 37 +++++++++++++---------------------- pubs/commands/doc_cmd.py | 39 +++++++++++++------------------------ pubs/commands/import_cmd.py | 27 +++++++++++-------------- pubs/commands/note_cmd.py | 7 ++----- pubs/commands/remove_cmd.py | 4 ++++ pubs/commands/rename_cmd.py | 9 +++------ pubs/repo.py | 6 +++--- 7 files changed, 51 insertions(+), 78 deletions(-) diff --git a/pubs/commands/add_cmd.py b/pubs/commands/add_cmd.py index bbb9c70..1e787e7 100644 --- a/pubs/commands/add_cmd.py +++ b/pubs/commands/add_cmd.py @@ -104,11 +104,7 @@ def command(conf, args): ui.error('citekey already exist {}.'.format(citekey)) ui.exit(1) - try: - p = paper.Paper.from_bibentry(bibentry, citekey=citekey) - except Exception as e: - ui.error(e.args[0]) - ui.exit(1) + p = paper.Paper.from_bibentry(bibentry, citekey=citekey) # tags @@ -132,21 +128,16 @@ def command(conf, args): if move is None: move = conf['main']['doc_add'] == 'move' - try: - rp.push_paper(p) - ui.message('added to pubs:\n{}'.format(pretty.paper_oneliner(p))) - if docfile is not None: - rp.push_doc(p.citekey, docfile, copy=copy or args.move) - if copy: - if move: - content.remove_file(docfile) - - if copy: - if move: - ui.message('{} was moved to the pubs repository.'.format(docfile)) - else: - ui.message('{} was copied to the pubs repository.'.format(docfile)) - - except ValueError as v: - ui.error(v.message) - ui.exit(1) + rp.push_paper(p) + ui.message('added to pubs:\n{}'.format(pretty.paper_oneliner(p))) + if docfile is not None: + rp.push_doc(p.citekey, docfile, copy=copy or args.move) + if copy: + if move: + content.remove_file(docfile) + + if copy: + if move: + ui.message('{} was moved to the pubs repository.'.format(docfile)) + else: + ui.message('{} was copied to the pubs repository.'.format(docfile)) diff --git a/pubs/commands/doc_cmd.py b/pubs/commands/doc_cmd.py index 2710f7c..5cdd84d 100644 --- a/pubs/commands/doc_cmd.py +++ b/pubs/commands/doc_cmd.py @@ -65,26 +65,19 @@ def command(conf, args): if not ui.input_yn(question=msg, default='n'): ui.exit(0) else: - try: - rp.remove_doc(paper.citekey) - except (ValueError, IOError) as v: - ui.error(v.message) - ui.exit(1) + rp.remove_doc(paper.citekey) - try: - document = args.document[0] - if args.link: - rp.push_doc(paper.citekey, document, copy=False) - else: - rp.push_doc(paper.citekey, document, copy=True) - if not args.link and args.move: - content.remove_file(document) - - ui.message('{} added to {}'.format(color.dye_out(document, 'filepath'), - color.dye_out(paper.citekey, 'citekey'))) - except (ValueError, IOError) as v: - ui.error(v.message) - ui.exit(1) + document = args.document[0] + if args.link: + rp.push_doc(paper.citekey, document, copy=False) + else: + rp.push_doc(paper.citekey, document, copy=True) + if not args.link and args.move: + content.remove_file(document) + + ui.message('{} added to {}'.format( + color.dye_out(document, 'filepath'), + color.dye_out(paper.citekey, 'citekey'))) elif args.action == 'remove': @@ -103,11 +96,7 @@ def command(conf, args): if not ui.input_yn(question=msg, default='n'): continue - try: - rp.remove_doc(paper.citekey) - except (ValueError, IOError) as v: - ui.error(v.message) - ui.exit(1) + rp.remove_doc(paper.citekey) elif args.action == 'export': @@ -133,7 +122,7 @@ def command(conf, args): dest_path = path + os.path.basename(real_doc_path) content.copy_content(real_doc_path, dest_path) except (ValueError, IOError) as e: - ui.error(e.message) + ui.error(str(e)) elif args.action == 'open': with_command = args.cmd diff --git a/pubs/commands/import_cmd.py b/pubs/commands/import_cmd.py index 128acd8..9592332 100644 --- a/pubs/commands/import_cmd.py +++ b/pubs/commands/import_cmd.py @@ -74,20 +74,15 @@ def command(conf, args): papers = many_from_path(bibpath) keys = args.keys or papers.keys() for k in keys: - try: - p = papers[k] - if isinstance(p, Exception): - ui.error(u'Could not load entry for citekey {}.'.format(k)) + p = papers[k] + if isinstance(p, Exception): + ui.error(u'Could not load entry for citekey {}.'.format(k)) + else: + rp.push_paper(p) + ui.info(u'{} 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)) else: - rp.push_paper(p) - ui.info(u'{} 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)) - else: - rp.push_doc(p.citekey, docfile, copy=copy) - #FIXME should move the file if configured to do so. - except KeyError: - ui.error(u'No entry found for citekey {}.'.format(k)) - except IOError as e: - ui.error(e.message) + rp.push_doc(p.citekey, docfile, copy=copy) + #FIXME should move the file if configured to do so. diff --git a/pubs/commands/note_cmd.py b/pubs/commands/note_cmd.py index f68ef35..895e701 100644 --- a/pubs/commands/note_cmd.py +++ b/pubs/commands/note_cmd.py @@ -19,8 +19,5 @@ def command(conf, args): ui = get_ui() rp = repo.Repository(conf) 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 + notepath = rp.databroker.real_notepath(citekey) + content.edit_file(conf['main']['edit_cmd'], notepath, temporary=False) diff --git a/pubs/commands/remove_cmd.py b/pubs/commands/remove_cmd.py index d18f185..e673623 100644 --- a/pubs/commands/remove_cmd.py +++ b/pubs/commands/remove_cmd.py @@ -26,13 +26,17 @@ def command(conf, args): .format(', '.join([color.dye_out(c, 'citekey') for c in args.citekeys]))) sure = ui.input_yn(question=are_you_sure, default='n') if force or sure: + failed = False # Whether something failed for c in keys: try: rp.remove_paper(c) except Exception as e: ui.error(e.message) + failed = True ui.message('The publication(s) [{}] were removed'.format( ', '.join([color.dye_out(c, 'citekey') for c in keys]))) + if failed: + ui.exit() # Exit with nonzero error code # FIXME: print should check that removal proceeded well. else: ui.message('The publication(s) [{}] were {} removed'.format( diff --git a/pubs/commands/rename_cmd.py b/pubs/commands/rename_cmd.py index d832980..e87daec 100644 --- a/pubs/commands/rename_cmd.py +++ b/pubs/commands/rename_cmd.py @@ -21,9 +21,6 @@ def command(conf, args): rp = repo.Repository(conf) # TODO: here should be a test whether the new citekey is valid - try: - key = resolve_citekey(repo=rp, citekey=args.citekey, ui=ui, exit_on_fail=True) - paper = rp.pull_paper(key) - rp.rename_paper(paper, args.new_citekey) - except Exception as e: - ui.error(e.message) \ No newline at end of file + key = resolve_citekey(repo=rp, citekey=args.citekey, ui=ui, exit_on_fail=True) + paper = rp.pull_paper(key) + rp.rename_paper(paper, args.new_citekey) diff --git a/pubs/repo.py b/pubs/repo.py index e3df89e..5ee69f6 100644 --- a/pubs/repo.py +++ b/pubs/repo.py @@ -24,14 +24,14 @@ class CiteKeyError(Exception): return self.message or self.default_msg.format(self.citekey) -class CiteKeyCollision(Exception): +class CiteKeyCollision(CiteKeyError): default_message = "Citekey already in use: {}." -class CiteKeyNotFound(Exception): +class CiteKeyNotFound(CiteKeyError): - default_message = "Could not find citekey: {}." + default_message = "No entry found for citekey: {}." class Repository(object):