fix #5 and achieve method parity between databroker and datacache

main
Fabien Benureau 11 years ago
parent ed16f0bb5b
commit 071730a49b

@ -82,7 +82,7 @@ def command(args):
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: else:
rp.databroker.exists(citekey, both=False) rp.databroker.exists(citekey, meta_check=False)
p = paper.Paper(bibdata, citekey=citekey) p = paper.Paper(bibdata, citekey=citekey)

@ -39,13 +39,22 @@ class DataBroker(object):
def remove(self, citekey): def remove(self, citekey):
self.filebroker.remove(citekey) self.filebroker.remove(citekey)
def exists(self, citekey, both = True): def exists(self, citekey, meta_check=False):
return self.filebroker.exists(citekey, both=both) """ Checks wether the bibtex of a citekey exists.
:param meta_check: if True, will return if both the bibtex and the meta file exists.
"""
return self.filebroker.exists(citekey, meta_check=meta_check)
def citekeys(self):
listings = self.listing(filestats=False)
return set(listings['bibfiles'])
def listing(self, filestats=True): def listing(self, filestats=True):
return self.filebroker.listing(filestats=filestats) return self.filebroker.listing(filestats=filestats)
def verify(self, bibdata_raw): def verify(self, bibdata_raw):
"""Will return None if bibdata_raw can't be decoded"""
try: try:
return self.endecoder.decode_bibdata(bibdata_raw) return self.endecoder.decode_bibdata(bibdata_raw)
except ValueError: except ValueError:

@ -7,7 +7,7 @@ class DataCache(object):
Has two roles : Has two roles :
1. Provides a buffer between the commands and the hard drive. 1. Provides a buffer between the commands and the hard drive.
Until a command request a hard drive ressource, it does not touch it. Until a command request a hard drive ressource, it does not touch it.
2. Keeps a up-to-date, pickled version of the repository, to speed up things 2. Keeps an up-to-date, pickled version of the repository, to speed up things
when they are a lot of files. Update are also done only when required. when they are a lot of files. Update are also done only when required.
Changes are detected using data modification timestamps. Changes are detected using data modification timestamps.
@ -46,18 +46,16 @@ class DataCache(object):
def remove(self, citekey): def remove(self, citekey):
self.databroker.remove(citekey) self.databroker.remove(citekey)
def exists(self, citekey, both=True): def exists(self, citekey, meta_check=False):
return self.databroker.exists(citekey, both=both) return self.databroker.exists(citekey, meta_check=meta_check)
def citekeys(self): def citekeys(self):
listings = self.listing(filestats=False) return self.databroker.citekeys()
return set(listings['metafiles']).intersection(listings['bibfiles'])
def listing(self, filestats=True): def listing(self, filestats=True):
return self.databroker.listing(filestats=filestats) return self.databroker.listing(filestats=filestats)
def verify(self, bibdata_raw): def verify(self, bibdata_raw):
"""Will return None if bibdata_raw can't be decoded"""
return self.databroker.verify(bibdata_raw) return self.databroker.verify(bibdata_raw)
# docbroker # docbroker

@ -71,13 +71,16 @@ class FileBroker(object):
if check_file(bibfilepath): if check_file(bibfilepath):
os.remove(system_path(bibfilepath)) os.remove(system_path(bibfilepath))
def exists(self, citekey, both=True): def exists(self, citekey, meta_check=False):
meta_exists = check_file(os.path.join(self.metadir, citekey + '.yaml'), fail=False) """ Checks wether the bibtex of a citekey exists.
bib_exists = check_file(os.path.join(self.bibdir, citekey + '.bib'), fail=False)
if both: :param meta_check: if True, will return if both the bibtex and the meta file exists.
return meta_exists and bib_exists """
else: does_exists = check_file(os.path.join(self.bibdir, citekey + '.bib'), fail=False)
return meta_exists or bib_exists if meta_check:
meta_exists = check_file(os.path.join(self.metadir, citekey + '.yaml'), fail=False)
does_exists = does_exists and meta_exists
return does_exists
def listing(self, filestats=True): def listing(self, filestats=True):
metafiles = [] metafiles = []

@ -50,7 +50,8 @@ 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, both = True): if self.databroker.exists(citekey, meta_check=True):
#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))
@ -64,7 +65,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, both=False) if (not overwrite) and (self.databroker.exists(paper.citekey, meta_check=False)
or (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:

@ -25,11 +25,12 @@ class TestDataBroker(unittest.TestCase):
db = db_class('tmp', create=True) db = db_class('tmp', create=True)
db.push_metadata('citekey1', page99_metadata) db.push_metadata('citekey1', page99_metadata)
self.assertTrue(db.exists('citekey1', both=False)) self.assertFalse(db.exists('citekey1', meta_check=True))
self.assertFalse(db.exists('citekey1', both=True)) self.assertFalse(db.exists('citekey1', meta_check=False))
db.push_bibdata('citekey1', page99_bibdata) db.push_bibdata('citekey1', page99_bibdata)
self.assertTrue(db.exists('citekey1', both=True)) self.assertTrue(db.exists('citekey1', meta_check=False))
self.assertTrue(db.exists('citekey1', meta_check=True))
self.assertEqual(db.pull_metadata('citekey1'), page99_metadata) self.assertEqual(db.pull_metadata('citekey1'), page99_metadata)
pulled = db.pull_bibdata('citekey1')['Page99'] pulled = db.pull_bibdata('citekey1')['Page99']

Loading…
Cancel
Save