diff options
author | Elena ``of Valhalla'' Grandi <valhalla@trueelena.org> | 2016-12-22 16:57:10 +0100 |
---|---|---|
committer | Elena ``of Valhalla'' Grandi <valhalla@trueelena.org> | 2016-12-22 16:57:10 +0100 |
commit | 6f9eaac83999fc8f6ba513b3705f6717b0f6fed8 (patch) | |
tree | bb51986baab300ee9df842120ab9377fe0e3fe13 | |
parent | 826f690632664f070d863abdbe31e978e99d3325 (diff) |
Add itemdir to collections, to be able to customize it
-rw-r--r-- | lesana/collection.py | 23 | ||||
-rw-r--r-- | lesana/command.py | 3 |
2 files changed, 14 insertions, 12 deletions
diff --git a/lesana/collection.py b/lesana/collection.py index c15f517..2e63ee7 100644 --- a/lesana/collection.py +++ b/lesana/collection.py @@ -59,8 +59,9 @@ class Collection(object): """ """ - def __init__(self, directory=None): + def __init__(self, directory=None, itemdir='items'): self.basedir = directory or os.getcwd() + self.itemdir = os.path.join(self.basedir, itemdir) self.cache = None try: with open(os.path.join(self.basedir, 'settings.yaml')) as fp: @@ -87,7 +88,7 @@ class Collection(object): self.safe = True def _index_file(self, fname): - with open(os.path.join(self.basedir, 'items', fname)) as fp: + with open(os.path.join(self.itemdir, fname)) as fp: if self.safe: data = ruamel.yaml.safe_load(fp) else: @@ -119,10 +120,12 @@ class Collection(object): self.cache.replace_document(entry.idterm, doc) - def update_cache(self, files=None): + def update_cache(self, fnames=None): """ Update the xapian db with the data in files. + ``fnames`` is a list of *basenames* of files in ``self.itemdir``. + If no files have been passed, add everything. Return the number of files that have been added to the cache. @@ -133,16 +136,17 @@ class Collection(object): ) self.indexer = xapian.TermGenerator() self.indexer.set_stemmer(self.stemmer) - if not files: + if not fnames: try: - files = os.listdir(os.path.join(self.basedir, 'items')) + fnames = os.listdir(self.itemdir) except FileNotFoundError: logging.warning( - "No such file or directory: %s, not updating cache", - os.path.join(self.basedir, 'items')) + "No such file or directory: %s, not updating cache".format( + self.itemdir) + ) return 0 updated = 0 - for fname in files: + for fname in fnames: try: self._index_file(fname) except IOError as e: @@ -159,8 +163,7 @@ class Collection(object): raise NotImplementedError for e in entries: complete_name = os.path.join( - self.basedir, - 'items', + self.itemdir, e.fname ) with open(complete_name, 'w') as fp: diff --git a/lesana/command.py b/lesana/command.py index 2beb88b..25a422a 100644 --- a/lesana/command.py +++ b/lesana/command.py @@ -19,8 +19,7 @@ class New(gadona.Command): new_entry = Entry(collection) collection.save_entries([new_entry]) filepath = os.path.join( - collection.basedir, - 'items', + collection.itemdir, new_entry.fname ) try: |