diff options
-rw-r--r-- | TODO.rst | 1 | ||||
-rw-r--r-- | lesana/collection.py | 12 | ||||
-rw-r--r-- | lesana/command.py | 11 | ||||
-rw-r--r-- | tests/test_collection.py | 17 |
4 files changed, 37 insertions, 4 deletions
@@ -1,6 +1,5 @@ The following features are already planned. -* Allow to identify entries with just the beginning of the uid. * Write some infrastructure to make it easy to write import scripts from other formats (and write an import script from tellico to lesana). diff --git a/lesana/collection.py b/lesana/collection.py index 5877a7d..dc005bb 100644 --- a/lesana/collection.py +++ b/lesana/collection.py @@ -368,6 +368,18 @@ class Collection(object): return self._doc_to_entry(cache.get_document(pitem.docid)) return None + def entries_from_short_uid(self, suid): + # It would be better to search for partial UIDs inside xapian, + # but I still can't find a way to do it, so this is a workable + # workaround on repos where the uids are stored in the + # filenames. + potential_uids = [ + os.path.splitext(f)[0] + for f in os.listdir(self.itemdir) + if f.startswith(suid) + ] + return [self.entry_from_uid(u) for u in potential_uids if u] + def remove_entries(self, uids): cache = xapian.WritableDatabase( os.path.join(self.basedir, '.lesana/xapian'), diff --git a/lesana/command.py b/lesana/command.py index 2915d30..2df70c1 100644 --- a/lesana/command.py +++ b/lesana/command.py @@ -111,9 +111,14 @@ class Edit(guacamole.Command): def invoked(self, ctx): collection = Collection(ctx.args.collection) - entry = collection.entry_from_uid(ctx.args.uid) - if entry is None: - return "No such entry: {}".format(ctx.args.uid) + entries = collection.entries_from_short_uid(ctx.args.uid) + if len(entries) > 1: + return "{} is not an unique uid".format(ctx.args.uid) + if not entries: + return "Could not find an entry with uid starting with: {}".format( + ctx.args.uid + ) + entry = entries[0] filepath = os.path.join( collection.itemdir, entry.fname diff --git a/tests/test_collection.py b/tests/test_collection.py index 2b1864a..cc28a27 100644 --- a/tests/test_collection.py +++ b/tests/test_collection.py @@ -116,6 +116,23 @@ class testCollection(unittest.TestCase): ) self.assertEqual(entry.uid, '11189ee47ddf4796b718a483b379f976') + def test_entry_from_short_uid(self): + self.collection = lesana.Collection('tests/data/simple') + entries = self.collection.entries_from_short_uid( + '11189ee4' + ) + self.assertEqual(len(entries), 1) + self.assertEqual(entries[0].uid, '11189ee47ddf4796b718a483b379f976') + entries = self.collection.entries_from_short_uid( + '11189ee47ddf4796b718a483b379f976' + ) + self.assertEqual(len(entries), 1) + self.assertEqual(entries[0].uid, '11189ee47ddf4796b718a483b379f976') + entries = self.collection.entries_from_short_uid( + '12345678' + ) + self.assertEqual(len(entries), 0) + def test_index_missing_file(self): self.collection = lesana.Collection('tests/data/simple') with self.assertLogs(level=logging.WARNING) as cm: |