Refactors filebroker to remove file duplication.

main
Olivier Mangin 8 years ago
parent 983d1892e0
commit 38fc68adbc

@ -8,6 +8,10 @@ from .content import (check_file, check_directory, read_text_file, write_file,
from . import content from . import content
META_EXT = '.yaml'
BIB_EXT = '.bib'
def filter_filename(filename, ext): def filter_filename(filename, ext):
""" Return the filename without the extension if the extension matches ext. """ Return the filename without the extension if the extension matches ext.
Otherwise return None Otherwise return None
@ -47,6 +51,12 @@ class FileBroker(object):
if not check_directory(self.bibdir, fail=False): if not check_directory(self.bibdir, fail=False):
os.mkdir(system_path(self.bibdir)) os.mkdir(system_path(self.bibdir))
def bib_path(self, citekey):
return os.path.join(self.bibdir, citekey + BIB_EXT)
def meta_path(self, citekey):
return os.path.join(self.metadir, citekey + META_EXT)
def pull_cachefile(self, filename): def pull_cachefile(self, filename):
filepath = os.path.join(self.cachedir, filename) filepath = os.path.join(self.cachedir, filename)
return content.read_binary_file(filepath) return content.read_binary_file(filepath)
@ -57,35 +67,31 @@ class FileBroker(object):
def mtime_metafile(self, citekey): def mtime_metafile(self, citekey):
try: try:
filepath = os.path.join(self.metadir, citekey + '.yaml') filepath = self.meta_path(citekey)
return os.path.getmtime(filepath) return os.path.getmtime(filepath)
except OSError: except OSError:
raise IOError("'{}' not found.".format(filepath)) raise IOError("'{}' not found.".format(filepath))
def mtime_bibfile(self, citekey): def mtime_bibfile(self, citekey):
try: try:
filepath = os.path.join(self.bibdir, citekey + '.bib') filepath = self.bib_path(citekey)
return os.path.getmtime(filepath) return os.path.getmtime(filepath)
except OSError: except OSError:
raise IOError("'{}' not found.".format(filepath)) raise IOError("'{}' not found.".format(filepath))
def pull_metafile(self, citekey): def pull_metafile(self, citekey):
filepath = os.path.join(self.metadir, citekey + '.yaml') return read_text_file(self.meta_path(citekey))
return read_text_file(filepath)
def pull_bibfile(self, citekey): def pull_bibfile(self, citekey):
filepath = os.path.join(self.bibdir, citekey + '.bib') return read_text_file(self.bib_path(citekey))
return read_text_file(filepath)
def push_metafile(self, citekey, metadata): def push_metafile(self, citekey, metadata):
"""Put content to disk. Will gladly override anything standing in its way.""" """Put content to disk. Will gladly override anything standing in its way."""
filepath = os.path.join(self.metadir, citekey + '.yaml') write_file(self.meta_path(citekey), metadata)
write_file(filepath, metadata)
def push_bibfile(self, citekey, bibdata): def push_bibfile(self, citekey, bibdata):
"""Put content to disk. Will gladly override anything standing in its way.""" """Put content to disk. Will gladly override anything standing in its way."""
filepath = os.path.join(self.bibdir, citekey + '.bib') write_file(self.bib_path(citekey), bibdata)
write_file(filepath, bibdata)
def push(self, citekey, metadata, bibdata): def push(self, citekey, metadata, bibdata):
"""Put content to disk. Will gladly override anything standing in its way.""" """Put content to disk. Will gladly override anything standing in its way."""
@ -93,10 +99,10 @@ class FileBroker(object):
self.push_bibfile(citekey, bibdata) self.push_bibfile(citekey, bibdata)
def remove(self, citekey): def remove(self, citekey):
metafilepath = os.path.join(self.metadir, citekey + '.yaml') metafilepath = self.meta_path(citekey)
if check_file(metafilepath): if check_file(metafilepath):
os.remove(system_path(metafilepath)) os.remove(system_path(metafilepath))
bibfilepath = os.path.join(self.bibdir, citekey + '.bib') bibfilepath = self.bib_path(citekey)
if check_file(bibfilepath): if check_file(bibfilepath):
os.remove(system_path(bibfilepath)) os.remove(system_path(bibfilepath))
@ -105,16 +111,16 @@ class FileBroker(object):
:param meta_check: if True, will return if both the bibtex and the meta file exists. :param meta_check: if True, will return if both the bibtex and the meta file exists.
""" """
does_exists = check_file(os.path.join(self.bibdir, citekey + '.bib'), fail=False) does_exists = check_file(self.bib_path(citekey), fail=False)
if meta_check: if meta_check:
meta_exists = check_file(os.path.join(self.metadir, citekey + '.yaml'), fail=False) meta_exists = check_file(self.meta_path(citekey), fail=False)
does_exists = does_exists and meta_exists does_exists = does_exists and meta_exists
return does_exists return does_exists
def listing(self, filestats=True): def listing(self, filestats=True):
metafiles = [] metafiles = []
for filename in os.listdir(system_path(self.metadir)): for filename in os.listdir(system_path(self.metadir)):
citekey = filter_filename(filename, '.yaml') citekey = filter_filename(filename, META_EXT)
if citekey is not None: if citekey is not None:
if filestats: if filestats:
stats = os.stat(system_path(os.path.join(self.metadir, filename))) stats = os.stat(system_path(os.path.join(self.metadir, filename)))
@ -124,7 +130,7 @@ class FileBroker(object):
bibfiles = [] bibfiles = []
for filename in os.listdir(system_path(self.bibdir)): for filename in os.listdir(system_path(self.bibdir)):
citekey = filter_filename(filename, '.bib') citekey = filter_filename(filename, BIB_EXT)
if citekey is not None: if citekey is not None:
if filestats: if filestats:
stats = os.stat(system_path(os.path.join(self.bibdir, filename))) stats = os.stat(system_path(os.path.join(self.bibdir, filename)))

Loading…
Cancel
Save