From 6f9eaac83999fc8f6ba513b3705f6717b0f6fed8 Mon Sep 17 00:00:00 2001 From: Elena ``of Valhalla'' Grandi Date: Thu, 22 Dec 2016 16:57:10 +0100 Subject: Add itemdir to collections, to be able to customize it --- lesana/collection.py | 23 +++++++++++++---------- 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: -- cgit v1.2.3