From 39b2e4f9123ea2614f4007bef83963faa2a9bac1 Mon Sep 17 00:00:00 2001 From: Olivier Mangin Date: Mon, 28 Apr 2014 19:45:06 +0200 Subject: [PATCH] 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). --- pubs/commands/add_cmd.py | 13 ++----------- pubs/commands/attach_cmd.py | 11 +---------- pubs/commands/import_cmd.py | 12 +++--------- pubs/repo.py | 12 ++++++++++++ tests/test_usecase.py | 16 ++++++++++++++++ 5 files changed, 34 insertions(+), 30 deletions(-) diff --git a/pubs/commands/add_cmd.py b/pubs/commands/add_cmd.py index 56765b4..a8461e6 100644 --- a/pubs/commands/add_cmd.py +++ b/pubs/commands/add_cmd.py @@ -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) diff --git a/pubs/commands/attach_cmd.py b/pubs/commands/attach_cmd.py index 0ecf1d8..fc7ba17 100644 --- a/pubs/commands/attach_cmd.py +++ b/pubs/commands/attach_cmd.py @@ -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) diff --git a/pubs/commands/import_cmd.py b/pubs/commands/import_cmd.py index b2e6831..98f909a 100644 --- a/pubs/commands/import_cmd.py +++ b/pubs/commands/import_cmd.py @@ -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: diff --git a/pubs/repo.py b/pubs/repo.py index 7ff8354..4ea1f0f 100644 --- a/pubs/repo.py +++ b/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(): diff --git a/tests/test_usecase.py b/tests/test_usecase.py index e72c0d6..b45e109 100644 --- a/tests/test_usecase.py +++ b/tests/test_usecase.py @@ -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):