Fails to add with existing citekey.
Also adds add usecase test and improves repository __contains__.
This commit is contained in:
parent
39b2e4f912
commit
89bf370902
@ -81,8 +81,9 @@ def command(args):
|
|||||||
if citekey is None:
|
if citekey is None:
|
||||||
base_key = bibstruct.extract_citekey(bibdata)
|
base_key = bibstruct.extract_citekey(bibdata)
|
||||||
citekey = rp.unique_citekey(base_key)
|
citekey = rp.unique_citekey(base_key)
|
||||||
else:
|
elif citekey in rp:
|
||||||
rp.databroker.exists(citekey, meta_check=False)
|
ui.error('citekey already exist {}.'.format(citekey))
|
||||||
|
ui.exit(1)
|
||||||
|
|
||||||
p = paper.Paper(bibdata, citekey=citekey)
|
p = paper.Paper(bibdata, citekey=citekey)
|
||||||
|
|
||||||
|
11
pubs/repo.py
11
pubs/repo.py
@ -36,9 +36,10 @@ class Repository(object):
|
|||||||
def __contains__(self, citekey):
|
def __contains__(self, citekey):
|
||||||
""" Allows to use 'if citekey in repo' pattern
|
""" 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):
|
def __len__(self):
|
||||||
"""Warning: costly the first time."""
|
"""Warning: costly the first time."""
|
||||||
@ -51,8 +52,7 @@ class Repository(object):
|
|||||||
|
|
||||||
def pull_paper(self, citekey):
|
def pull_paper(self, citekey):
|
||||||
"""Load a paper by its citekey from disk, if necessary."""
|
"""Load a paper by its citekey from disk, if necessary."""
|
||||||
if self.databroker.exists(citekey, meta_check=True):
|
if citekey in self:
|
||||||
#TODO meta_check=False and default meta generation
|
|
||||||
return Paper(self.databroker.pull_bibdata(citekey),
|
return Paper(self.databroker.pull_bibdata(citekey),
|
||||||
citekey=citekey,
|
citekey=citekey,
|
||||||
metadata=self.databroker.pull_metadata(citekey))
|
metadata=self.databroker.pull_metadata(citekey))
|
||||||
@ -66,8 +66,7 @@ class Repository(object):
|
|||||||
if True, mimick the behavior of updating a paper
|
if True, mimick the behavior of updating a paper
|
||||||
"""
|
"""
|
||||||
bibstruct.check_citekey(paper.citekey)
|
bibstruct.check_citekey(paper.citekey)
|
||||||
if (not overwrite) and (self.databroker.exists(paper.citekey, meta_check=False)
|
if (not overwrite) and (paper.citekey in self):
|
||||||
or (paper.citekey in self)):
|
|
||||||
raise CiteKeyCollision('citekey {} already in use'.format(paper.citekey))
|
raise CiteKeyCollision('citekey {} already in use'.format(paper.citekey))
|
||||||
if not paper.added:
|
if not paper.added:
|
||||||
paper.added = datetime.now()
|
paper.added = datetime.now()
|
||||||
|
@ -151,6 +151,14 @@ class TestAdd(DataCommandTestCase):
|
|||||||
self.execute_cmds(cmds)
|
self.execute_cmds(cmds)
|
||||||
self.assertEqual(set(self.fs['os'].listdir('/not_default/doc')), {'Page99.pdf'})
|
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):
|
def test_add_doc_nocopy_does_not_copy(self):
|
||||||
cmds = ['pubs init',
|
cmds = ['pubs init',
|
||||||
'pubs add /data/pagerank.bib -C -d /data/pagerank.pdf',
|
'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')),
|
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):
|
class TestList(DataCommandTestCase):
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user