Adds citekey argument to selectively import entries.
Also removes unused fatal argument from many_from_path and moves printing of warning to the command.
This commit is contained in:
parent
68e2a23a3c
commit
08811b842f
@ -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:
|
||||
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))
|
||||
|
@ -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,17 +244,13 @@ 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 = []
|
||||
papers = {}
|
||||
for b in bib_data:
|
||||
for k in b.entries:
|
||||
try:
|
||||
papers.append(Paper(bibentry=b.entries[k], citekey=k))
|
||||
papers[k] = Paper(bibentry=b.entries[k], citekey=k)
|
||||
except ValueError, e:
|
||||
print('Warning, skipping paper ({}).'.format(e))
|
||||
papers[k] = e
|
||||
return papers
|
||||
|
||||
|
||||
|
@ -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',
|
||||
|
Loading…
x
Reference in New Issue
Block a user