summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorElena ``of Valhalla'' Grandi <valhalla@trueelena.org>2018-12-16 19:14:34 +0100
committerElena ``of Valhalla'' Grandi <valhalla@trueelena.org>2018-12-16 19:14:34 +0100
commite1842f4a9c6e06b1163ef1ae0d8c1b68c434a0a9 (patch)
tree7aeb886c4bf389f2465145eac71cd7fd5f00e235
parente7279a50431bc86286acd1f167b3c1e364a9f33e (diff)
Allow to identify entries with just the beginning of the uid.
-rw-r--r--TODO.rst1
-rw-r--r--lesana/collection.py12
-rw-r--r--lesana/command.py11
-rw-r--r--tests/test_collection.py17
4 files changed, 37 insertions, 4 deletions
diff --git a/TODO.rst b/TODO.rst
index 4452ab5..236e671 100644
--- a/TODO.rst
+++ b/TODO.rst
@@ -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: