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 ..configs import config
|
||||||
from ..uis import get_ui
|
from ..uis import get_ui
|
||||||
|
|
||||||
|
|
||||||
def parser(subparsers):
|
def parser(subparsers):
|
||||||
parser = subparsers.add_parser('import',
|
parser = subparsers.add_parser('import',
|
||||||
help='import paper(s) to the repository')
|
help='import paper(s) to the repository')
|
||||||
@ -13,6 +14,8 @@ def parser(subparsers):
|
|||||||
help="copy document files into library directory (default)")
|
help="copy document files into library directory (default)")
|
||||||
parser.add_argument('-C', '--nocopy', action='store_false', dest='copy',
|
parser.add_argument('-C', '--nocopy', action='store_false', dest='copy',
|
||||||
help="don't copy document files (opposite of -c)")
|
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
|
return parser
|
||||||
|
|
||||||
|
|
||||||
@ -29,9 +32,17 @@ def command(args):
|
|||||||
copy = config().import_copy
|
copy = config().import_copy
|
||||||
rp = repo.Repository(config())
|
rp = repo.Repository(config())
|
||||||
# Extract papers from bib
|
# Extract papers from bib
|
||||||
papers = Paper.many_from_path(bibpath, fatal=False)
|
papers = Paper.many_from_path(bibpath)
|
||||||
for p in papers:
|
keys = args.keys or papers.keys()
|
||||||
doc_file = extract_doc_path_from_bibdata(p)
|
for k in keys:
|
||||||
if doc_file is None:
|
try:
|
||||||
ui.warning("No file for %s." % p.citekey)
|
p = papers[k]
|
||||||
add_paper_with_docfile(rp, p, docfile=doc_file, copy=copy)
|
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()
|
return BASE_META.copy()
|
||||||
|
|
||||||
@classmethod
|
@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.
|
"""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)
|
bibpath = files.clean_path(bibpath)
|
||||||
if os.path.isdir(bibpath):
|
if os.path.isdir(bibpath):
|
||||||
@ -237,18 +244,14 @@ class Paper(object):
|
|||||||
else:
|
else:
|
||||||
all_files = [bibpath]
|
all_files = [bibpath]
|
||||||
bib_data = [files.load_externalbibfile(f) for f in all_files]
|
bib_data = [files.load_externalbibfile(f) for f in all_files]
|
||||||
if fatal:
|
papers = {}
|
||||||
return [Paper(bibentry=b.entries[k], citekey=k)
|
for b in bib_data:
|
||||||
for b in bib_data for k in b.entries]
|
for k in b.entries:
|
||||||
else:
|
try:
|
||||||
papers = []
|
papers[k] = Paper(bibentry=b.entries[k], citekey=k)
|
||||||
for b in bib_data:
|
except ValueError, e:
|
||||||
for k in b.entries:
|
papers[k] = e
|
||||||
try:
|
return papers
|
||||||
papers.append(Paper(bibentry=b.entries[k], citekey=k))
|
|
||||||
except ValueError, e:
|
|
||||||
print('Warning, skipping paper ({}).'.format(e))
|
|
||||||
return papers
|
|
||||||
|
|
||||||
|
|
||||||
# tags
|
# tags
|
||||||
|
@ -102,13 +102,13 @@ def redirect(f):
|
|||||||
return newf
|
return newf
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
# Test helpers
|
# Test helpers
|
||||||
|
|
||||||
# automating input
|
# automating input
|
||||||
|
|
||||||
real_input = input
|
real_input = input
|
||||||
|
|
||||||
|
|
||||||
class FakeInput():
|
class FakeInput():
|
||||||
""" Replace the input() command, and mock user input during tests
|
""" Replace the input() command, and mock user input during tests
|
||||||
|
|
||||||
@ -253,6 +253,7 @@ class TestList(DataCommandTestCase):
|
|||||||
]
|
]
|
||||||
self.execute_cmds(cmds)
|
self.execute_cmds(cmds)
|
||||||
|
|
||||||
|
|
||||||
class TestUsecase(DataCommandTestCase):
|
class TestUsecase(DataCommandTestCase):
|
||||||
|
|
||||||
def test_first(self):
|
def test_first(self):
|
||||||
@ -361,6 +362,15 @@ class TestUsecase(DataCommandTestCase):
|
|||||||
outs = self.execute_cmds(cmds)
|
outs = self.execute_cmds(cmds)
|
||||||
self.assertEqual(4 + 1, len(outs[-1].split('\n')))
|
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):
|
def test_open(self):
|
||||||
cmds = ['papers init',
|
cmds = ['papers init',
|
||||||
'papers add -b data/pagerank.bib',
|
'papers add -b data/pagerank.bib',
|
||||||
|
Loading…
x
Reference in New Issue
Block a user