Factorizes code for adding document.
- Document are added through repository method. - Correct handling of the copy option. - Does not check if document exists yet. - Does not bypass copy=False if an url is given. Should it? - Currently the document is actually pushed twice (not optimal).
This commit is contained in:
parent
53a0f0a86e
commit
39b2e4f912
@ -100,22 +100,13 @@ def command(args):
|
||||
ui.warning(('Skipping document file from bib file '
|
||||
'{}, using {} instead.').format(bib_docfile, docfile))
|
||||
|
||||
docfile = docfile
|
||||
|
||||
if docfile is not None:
|
||||
copy_doc = args.copy
|
||||
if copy_doc is None:
|
||||
copy_doc = config().import_copy
|
||||
if copy_doc:
|
||||
docfile = rp.databroker.add_doc(citekey, docfile)
|
||||
else:
|
||||
raise NotImplementedError
|
||||
|
||||
# create the paper
|
||||
|
||||
try:
|
||||
p.docpath = docfile
|
||||
rp.push_paper(p)
|
||||
if docfile is not None:
|
||||
rp.push_doc(p.citekey, docfile, copy=args.copy)
|
||||
except ValueError as v:
|
||||
ui.error(v.message)
|
||||
ui.exit(1)
|
||||
|
@ -27,18 +27,9 @@ def command(args):
|
||||
rp = repo.Repository(config())
|
||||
paper = rp.pull_paper(args.citekey)
|
||||
|
||||
copy = args.copy
|
||||
if copy is None:
|
||||
copy = config().import_copy
|
||||
|
||||
try:
|
||||
document = args.document
|
||||
if copy:
|
||||
document = rp.databroker.add_doc(paper.citekey, document)
|
||||
else:
|
||||
pass # TODO warn if file does not exists
|
||||
paper.docpath = document
|
||||
rp.push_paper(paper, overwrite=True, event=False)
|
||||
document = rp.push_doc(paper.citekey, document, copy=args.copy)
|
||||
except ValueError as v:
|
||||
ui.error(v.message)
|
||||
ui.exit(1)
|
||||
|
@ -84,19 +84,13 @@ def command(args):
|
||||
if isinstance(p, Exception):
|
||||
ui.error('could not load entry for citekey {}.'.format(k))
|
||||
else:
|
||||
rp.push_paper(p)
|
||||
ui.print_('{} imported'.format(color.dye(p.citekey, color.cyan)))
|
||||
docfile = bibstruct.extract_docfile(p.bibdata)
|
||||
if docfile is None:
|
||||
ui.warning("no file for {}.".format(p.citekey))
|
||||
else:
|
||||
copy_doc = args.copy
|
||||
if copy_doc is None:
|
||||
copy_doc = config().import_copy
|
||||
if copy_doc:
|
||||
docfile = rp.databroker.add_doc(p.citekey, docfile)
|
||||
|
||||
p.docpath = docfile
|
||||
rp.push_paper(p)
|
||||
ui.print_('{} imported'.format(color.dye(p.citekey, color.cyan)))
|
||||
rp.push_doc(p.citekey, docfile, copy=args.copy)
|
||||
except KeyError:
|
||||
ui.error('no entry found for citekey {}.'.format(k))
|
||||
except IOError as e:
|
||||
|
12
pubs/repo.py
12
pubs/repo.py
@ -5,6 +5,7 @@ from . import bibstruct
|
||||
from . import events
|
||||
from .datacache import DataCache
|
||||
from .paper import Paper
|
||||
from .content import system_path
|
||||
|
||||
|
||||
def _base27(n):
|
||||
@ -124,6 +125,17 @@ class Repository(object):
|
||||
# send event
|
||||
events.RenameEvent(paper, old_citekey).send()
|
||||
|
||||
def push_doc(self, citekey, docfile, copy=None):
|
||||
p = self.pull_paper(citekey)
|
||||
if copy is None:
|
||||
copy = self.config.import_copy
|
||||
if copy:
|
||||
docfile = self.databroker.add_doc(citekey, docfile)
|
||||
else:
|
||||
docfile = system_path(docfile)
|
||||
p.docpath = docfile
|
||||
self.push_paper(p, overwrite=True, event=False)
|
||||
|
||||
def unique_citekey(self, base_key):
|
||||
"""Create a unique citekey for a given basekey."""
|
||||
for n in itertools.count():
|
||||
|
@ -57,6 +57,7 @@ class CommandTestCase(unittest.TestCase):
|
||||
|
||||
def setUp(self):
|
||||
self.fs = fake_env.create_fake_fs([content, filebroker, configs, init_cmd, import_cmd])
|
||||
self.default_pubs_dir = self.fs['os'].path.expanduser('~/.pubs')
|
||||
|
||||
def execute_cmds(self, cmds, fs=None, capture_output=CAPTURE_OUTPUT):
|
||||
""" Execute a list of commands, and capture their output
|
||||
@ -136,6 +137,12 @@ class TestAdd(DataCommandTestCase):
|
||||
'pubs add /data/pagerank.bib -d /data/pagerank.pdf',
|
||||
]
|
||||
self.execute_cmds(cmds)
|
||||
bib_dir = self.fs['os'].path.join(self.default_pubs_dir, 'bib')
|
||||
meta_dir = self.fs['os'].path.join(self.default_pubs_dir, 'meta')
|
||||
doc_dir = self.fs['os'].path.join(self.default_pubs_dir, 'doc')
|
||||
self.assertEqual(set(self.fs['os'].listdir(bib_dir)), {'Page99.bib'})
|
||||
self.assertEqual(set(self.fs['os'].listdir(meta_dir)), {'Page99.yaml'})
|
||||
self.assertEqual(set(self.fs['os'].listdir(doc_dir)), {'Page99.pdf'})
|
||||
|
||||
def test_add2(self):
|
||||
cmds = ['pubs init -p /not_default',
|
||||
@ -144,6 +151,15 @@ class TestAdd(DataCommandTestCase):
|
||||
self.execute_cmds(cmds)
|
||||
self.assertEqual(set(self.fs['os'].listdir('/not_default/doc')), {'Page99.pdf'})
|
||||
|
||||
def test_add_doc_nocopy_does_not_copy(self):
|
||||
cmds = ['pubs init',
|
||||
'pubs add /data/pagerank.bib -C -d /data/pagerank.pdf',
|
||||
]
|
||||
self.execute_cmds(cmds)
|
||||
self.assertEqual(self.fs['os'].listdir(
|
||||
self.fs['os'].path.join(self.default_pubs_dir, 'doc')),
|
||||
[])
|
||||
|
||||
|
||||
class TestList(DataCommandTestCase):
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user