diff --git a/pubs/commands/add_cmd.py b/pubs/commands/add_cmd.py index a8461e6..1dc5907 100644 --- a/pubs/commands/add_cmd.py +++ b/pubs/commands/add_cmd.py @@ -81,8 +81,9 @@ def command(args): if citekey is None: base_key = bibstruct.extract_citekey(bibdata) citekey = rp.unique_citekey(base_key) - else: - rp.databroker.exists(citekey, meta_check=False) + elif citekey in rp: + ui.error('citekey already exist {}.'.format(citekey)) + ui.exit(1) p = paper.Paper(bibdata, citekey=citekey) diff --git a/pubs/repo.py b/pubs/repo.py index 4ea1f0f..2873f51 100644 --- a/pubs/repo.py +++ b/pubs/repo.py @@ -36,9 +36,10 @@ class Repository(object): def __contains__(self, citekey): """ Allows to use 'if citekey in repo' pattern - Warning: costly the first time. + The convention is that the paper is in the repository + if and only if a bibfile is in the repository. """ - return citekey in self.citekeys + return self.databroker.exists(citekey) def __len__(self): """Warning: costly the first time.""" @@ -51,8 +52,7 @@ class Repository(object): def pull_paper(self, citekey): """Load a paper by its citekey from disk, if necessary.""" - if self.databroker.exists(citekey, meta_check=True): - #TODO meta_check=False and default meta generation + if citekey in self: return Paper(self.databroker.pull_bibdata(citekey), citekey=citekey, metadata=self.databroker.pull_metadata(citekey)) @@ -66,8 +66,7 @@ class Repository(object): if True, mimick the behavior of updating a paper """ bibstruct.check_citekey(paper.citekey) - if (not overwrite) and (self.databroker.exists(paper.citekey, meta_check=False) - or (paper.citekey in self)): + if (not overwrite) and (paper.citekey in self): raise CiteKeyCollision('citekey {} already in use'.format(paper.citekey)) if not paper.added: paper.added = datetime.now() diff --git a/tests/test_usecase.py b/tests/test_usecase.py index b45e109..e5ce87a 100644 --- a/tests/test_usecase.py +++ b/tests/test_usecase.py @@ -151,6 +151,14 @@ class TestAdd(DataCommandTestCase): self.execute_cmds(cmds) self.assertEqual(set(self.fs['os'].listdir('/not_default/doc')), {'Page99.pdf'}) + def test_add_citekey(self): + cmds = ['pubs init', + 'pubs add -k CustomCitekey /data/pagerank.bib', + ] + self.execute_cmds(cmds) + bib_dir = self.fs['os'].path.join(self.default_pubs_dir, 'bib') + self.assertEqual(set(self.fs['os'].listdir(bib_dir)), {'CustomCitekey.bib'}) + def test_add_doc_nocopy_does_not_copy(self): cmds = ['pubs init', 'pubs add /data/pagerank.bib -C -d /data/pagerank.pdf', @@ -160,6 +168,14 @@ class TestAdd(DataCommandTestCase): self.fs['os'].path.join(self.default_pubs_dir, 'doc')), []) + def test_add_twice_fails(self): + cmds = ['pubs init', + 'pubs add /data/pagerank.bib', + 'pubs add -k Page99 /data/turing1950.bib', + ] + with self.assertRaises(SystemExit): + self.execute_cmds(cmds) + class TestList(DataCommandTestCase):