From 08811b842f4091f67d26350510f58f3c6427a468 Mon Sep 17 00:00:00 2001 From: Olivier Mangin Date: Tue, 16 Jul 2013 12:46:02 +0200 Subject: [PATCH] Adds citekey argument to selectively import entries. Also removes unused fatal argument from many_from_path and moves printing of warning to the command. --- papers/commands/import_cmd.py | 23 +++++++++++++++++------ papers/paper.py | 29 ++++++++++++++++------------- tests/test_usecase.py | 12 +++++++++++- 3 files changed, 44 insertions(+), 20 deletions(-) diff --git a/papers/commands/import_cmd.py b/papers/commands/import_cmd.py index 5f36471..c917949 100644 --- a/papers/commands/import_cmd.py +++ b/papers/commands/import_cmd.py @@ -4,6 +4,7 @@ from .helpers import add_paper_with_docfile, extract_doc_path_from_bibdata from ..configs import config from ..uis import get_ui + def parser(subparsers): parser = subparsers.add_parser('import', help='import paper(s) to the repository') @@ -13,6 +14,8 @@ def parser(subparsers): help="copy document files into library directory (default)") parser.add_argument('-C', '--nocopy', action='store_false', dest='copy', help="don't copy document files (opposite of -c)") + parser.add_argument('keys', nargs='*', + help="one or several keys to import from the file") return parser @@ -29,9 +32,17 @@ def command(args): copy = config().import_copy rp = repo.Repository(config()) # Extract papers from bib - papers = Paper.many_from_path(bibpath, fatal=False) - for p in papers: - doc_file = extract_doc_path_from_bibdata(p) - if doc_file is None: - ui.warning("No file for %s." % p.citekey) - add_paper_with_docfile(rp, p, docfile=doc_file, copy=copy) + papers = Paper.many_from_path(bibpath) + keys = args.keys or papers.keys() + for k in keys: + try: + p = papers[k] + if isinstance(p, Exception): + ui.error('Could not load entry for citekey {}.'.format(k)) + else: + doc_file = extract_doc_path_from_bibdata(p) + if doc_file is None: + ui.warning("No file for %s." % p.citekey) + add_paper_with_docfile(rp, p, docfile=doc_file, copy=copy) + except KeyError: + ui.error('No entry found for citekey {}.'.format(k)) diff --git a/papers/paper.py b/papers/paper.py index d1018f3..28b41c3 100644 --- a/papers/paper.py +++ b/papers/paper.py @@ -227,8 +227,15 @@ class Paper(object): return BASE_META.copy() @classmethod - def many_from_path(cls, bibpath, fatal=True): + def many_from_path(cls, bibpath): """Extract list of papers found in bibliographic files in path. + + The behavior is to: + - ignore wrong entries, + - overwrite duplicated entries. + :returns: dictionary of (key, paper | exception) + if loading of entry failed, the excpetion is returned in the + dictionary in place of the paper """ bibpath = files.clean_path(bibpath) if os.path.isdir(bibpath): @@ -237,18 +244,14 @@ class Paper(object): else: all_files = [bibpath] bib_data = [files.load_externalbibfile(f) for f in all_files] - if fatal: - return [Paper(bibentry=b.entries[k], citekey=k) - for b in bib_data for k in b.entries] - else: - papers = [] - for b in bib_data: - for k in b.entries: - try: - papers.append(Paper(bibentry=b.entries[k], citekey=k)) - except ValueError, e: - print('Warning, skipping paper ({}).'.format(e)) - return papers + papers = {} + for b in bib_data: + for k in b.entries: + try: + papers[k] = Paper(bibentry=b.entries[k], citekey=k) + except ValueError, e: + papers[k] = e + return papers # tags diff --git a/tests/test_usecase.py b/tests/test_usecase.py index 02cb382..623854a 100644 --- a/tests/test_usecase.py +++ b/tests/test_usecase.py @@ -102,13 +102,13 @@ def redirect(f): return newf - # Test helpers # automating input real_input = input + class FakeInput(): """ Replace the input() command, and mock user input during tests @@ -253,6 +253,7 @@ class TestList(DataCommandTestCase): ] self.execute_cmds(cmds) + class TestUsecase(DataCommandTestCase): def test_first(self): @@ -361,6 +362,15 @@ class TestUsecase(DataCommandTestCase): outs = self.execute_cmds(cmds) self.assertEqual(4 + 1, len(outs[-1].split('\n'))) + def test_import_one(self): + cmds = ['papers init', + 'papers import data/ Page99', + 'papers list' + ] + + outs = self.execute_cmds(cmds) + self.assertEqual(1 + 1, len(outs[-1].split('\n'))) + def test_open(self): cmds = ['papers init', 'papers add -b data/pagerank.bib',