diff options
author | Elena ``of Valhalla'' Grandi <valhalla@trueelena.org> | 2019-12-28 18:21:19 +0100 |
---|---|---|
committer | Elena ``of Valhalla'' Grandi <valhalla@trueelena.org> | 2019-12-28 18:21:19 +0100 |
commit | 812c47c4970ebebe19827d94fd467b9964028ae6 (patch) | |
tree | 2194c88c1c2cac03a63aaef2a6beba304c1fb531 | |
parent | a6e69a457226484f2419b792e45428041506e61d (diff) |
Use eid instead of uid
-rw-r--r-- | CHANGELOG.txt | 13 | ||||
-rw-r--r-- | lesana/collection.py | 40 | ||||
-rw-r--r-- | lesana/command.py | 30 | ||||
-rw-r--r-- | lesana/data/settings.yaml | 2 | ||||
-rw-r--r-- | tests/data/complex/settings.yaml | 2 | ||||
-rw-r--r-- | tests/data/simple/items/085682ed-6792-499d-a3ab-9aebd683c011.yaml | 2 | ||||
-rw-r--r-- | tests/test_collection.py | 86 |
7 files changed, 94 insertions, 81 deletions
diff --git a/CHANGELOG.txt b/CHANGELOG.txt new file mode 100644 index 0000000..58df9e3 --- /dev/null +++ b/CHANGELOG.txt @@ -0,0 +1,13 @@ +*********** + CHANGELOG +*********** + +Unreleased +========== + +Library +------- + +* This version changes the name of entry IDs from the nonsensical ``uid`` to + ``eid`` (Entry ID) everywhere in the code, including the property + ``Entry.uid`` and all method names. diff --git a/lesana/collection.py b/lesana/collection.py index 4626a7a..599d328 100644 --- a/lesana/collection.py +++ b/lesana/collection.py @@ -21,14 +21,14 @@ class Entry(object): self.collection = collection self.data = data or self.empty_data() self.fname = fname - self.uid = self.data.get('uid', None) - if not self.uid: + self.eid = self.data.get('eid', None) + if not self.eid: if self.fname: - self.uid, ext = os.path.splitext(os.path.basename(self.fname)) + self.eid, ext = os.path.splitext(os.path.basename(self.fname)) else: - self.uid = uuid.uuid4().hex + self.eid = uuid.uuid4().hex if not self.fname: - self.fname = self.uid + '.yaml' + self.fname = self.eid + '.yaml' def __str__(self): label = self.collection.settings.get('entry_label', None) @@ -36,11 +36,11 @@ class Entry(object): t = jinja2.Template(label) return t.render(**self.get_data()) else: - return self.uid + return self.eid def get_data(self): d = self.data.copy() - d['uid'] = self.uid + d['eid'] = self.eid d['fname'] = self.fname d['short_id'] = self.short_id return d @@ -75,11 +75,11 @@ class Entry(object): @property def idterm(self): - return "Q"+self.uid + return "Q"+self.eid @property def short_id(self): - return self.uid[:8] + return self.eid[:8] def validate(self): errors = [] @@ -396,37 +396,37 @@ class Collection(object): ) return entry - def entry_from_uid(self, uid): + def entry_from_eid(self, eid): cache = self._get_cache() - postlist = cache.postlist('Q'+uid) + postlist = cache.postlist('Q'+eid) for pitem in postlist: return self._doc_to_entry(cache.get_document(pitem.docid)) return None - def entries_from_short_uid(self, suid): + def entries_from_short_eid(self, seid): # 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 + # workaround on repos where the eids are stored in the # filenames. - potential_uids = [ + potential_eids = [ os.path.splitext(f)[0] for f in os.listdir(self.itemdir) - if f.startswith(suid) + if f.startswith(seid) ] - return [self.entry_from_uid(u) for u in potential_uids if u] + return [self.entry_from_eid(u) for u in potential_eids if u] - def remove_entries(self, uids): + def remove_entries(self, eids): cache = xapian.WritableDatabase( os.path.join(self.basedir, '.lesana/xapian'), xapian.DB_CREATE_OR_OPEN ) - for uid in uids: - for entry in self.entries_from_short_uid(uid): + for eid in eids: + for entry in self.entries_from_short_eid(eid): if entry is not None: cache.delete_document(entry.idterm) self.remove_file(entry.fname) else: - logging.warning("No such entry: {}, ignoring".format(uid)) + logging.warning("No such entry: {}, ignoring".format(eid)) cache.commit() cache.close() diff --git a/lesana/command.py b/lesana/command.py index a5a7508..7f4144b 100644 --- a/lesana/command.py +++ b/lesana/command.py @@ -91,7 +91,7 @@ class New(Command): collection.update_cache([filepath]) if self.args.git: collection.git_add_files([filepath]) - saved_entry = collection.entry_from_uid(new_entry.uid) + saved_entry = collection.entry_from_eid(new_entry.eid) print(saved_entry) @@ -105,19 +105,19 @@ class Edit(Command): action="store_false", dest='git' )), - (['uid'], dict( - help='uid of an entry to edit', + (['eid'], dict( + help='eid of an entry to edit', )), ] def main(self): collection = Collection(self.args.collection) - entries = collection.entries_from_short_uid(self.args.uid) + entries = collection.entries_from_short_eid(self.args.eid) if len(entries) > 1: - return "{} is not an unique uid".format(self.args.uid) + return "{} is not an unique eid".format(self.args.eid) if not entries: - return "Could not find an entry with uid starting with: {}".format( - self.args.uid + return "Could not find an entry with eid starting with: {}".format( + self.args.eid ) entry = entries[0] filepath = os.path.join( @@ -128,7 +128,7 @@ class Edit(Command): collection.update_cache([filepath]) if self.args.git: collection.git_add_files([filepath]) - saved_entry = collection.entry_from_uid(entry.uid) + saved_entry = collection.entry_from_eid(entry.eid) print(saved_entry) @@ -140,19 +140,19 @@ class Show(Command): (['--template', '-t'], dict( help='Use the specified template to display results.', )), - (['uid'], dict( - help='uid of an entry to edit', + (['eid'], dict( + help='eid of an entry to edit', )), ] def main(self): collection = Collection(self.args.collection) - entries = collection.entries_from_short_uid(self.args.uid) + entries = collection.entries_from_short_eid(self.args.eid) if len(entries) > 1: - return "{} is not an unique uid".format(self.args.uid) + return "{} is not an unique eid".format(self.args.eid) if not entries: - return "Could not find an entry with uid starting with: {}".format( - self.args.uid + return "Could not find an entry with eid starting with: {}".format( + self.args.eid ) entry = entries[0] if self.args.template: @@ -326,4 +326,4 @@ class Remove(Command): def main(self): collection = Collection(self.args.collection) - collection.remove_entries(uids=self.args.entries) + collection.remove_entries(eids=self.args.entries) diff --git a/lesana/data/settings.yaml b/lesana/data/settings.yaml index 31653d0..5df1d65 100644 --- a/lesana/data/settings.yaml +++ b/lesana/data/settings.yaml @@ -1,6 +1,6 @@ name: 'My Collection' lang: english -entry_label: '{{ uid }}: {{ name }}' +entry_label: '{{ eid }}: {{ name }}' git: true fields: - name: name diff --git a/tests/data/complex/settings.yaml b/tests/data/complex/settings.yaml index 03110f3..51f313f 100644 --- a/tests/data/complex/settings.yaml +++ b/tests/data/complex/settings.yaml @@ -1,6 +1,6 @@ name: "Fully featured lesana collection" lang: 'english' -entry_label: '{{ uid}}: {{ name }} ({{ tags }})' +entry_label: '{{ eid }}: {{ name }} ({{ tags }})' fields: - name: name type: string diff --git a/tests/data/simple/items/085682ed-6792-499d-a3ab-9aebd683c011.yaml b/tests/data/simple/items/085682ed-6792-499d-a3ab-9aebd683c011.yaml index 082128c..0abc78d 100644 --- a/tests/data/simple/items/085682ed-6792-499d-a3ab-9aebd683c011.yaml +++ b/tests/data/simple/items/085682ed-6792-499d-a3ab-9aebd683c011.yaml @@ -3,4 +3,4 @@ description: | This is a long block of text that spans multiple lines. position: somewhere -uid: 085682ed6792499da3ab9aebd683c011 +eid: 085682ed6792499da3ab9aebd683c011 diff --git a/tests/test_collection.py b/tests/test_collection.py index 23186a1..b5266d9 100644 --- a/tests/test_collection.py +++ b/tests/test_collection.py @@ -104,31 +104,31 @@ class testCollection(unittest.TestCase): for m in matches: self.assertIsInstance(m, lesana.Entry) - def test_entry_from_uid(self): + def test_entry_from_eid(self): self.collection = lesana.Collection('tests/data/simple') - entry = self.collection.entry_from_uid( + entry = self.collection.entry_from_eid( '11189ee47ddf4796b718a483b379f976' ) - self.assertEqual(entry.uid, '11189ee47ddf4796b718a483b379f976') + self.assertEqual(entry.eid, '11189ee47ddf4796b718a483b379f976') self.collection.safe = True - entry = self.collection.entry_from_uid( + entry = self.collection.entry_from_eid( '11189ee47ddf4796b718a483b379f976' ) - self.assertEqual(entry.uid, '11189ee47ddf4796b718a483b379f976') + self.assertEqual(entry.eid, '11189ee47ddf4796b718a483b379f976') - def test_entry_from_short_uid(self): + def test_entry_from_short_eid(self): self.collection = lesana.Collection('tests/data/simple') - entries = self.collection.entries_from_short_uid( + entries = self.collection.entries_from_short_eid( '11189ee4' ) self.assertEqual(len(entries), 1) - self.assertEqual(entries[0].uid, '11189ee47ddf4796b718a483b379f976') - entries = self.collection.entries_from_short_uid( + self.assertEqual(entries[0].eid, '11189ee47ddf4796b718a483b379f976') + entries = self.collection.entries_from_short_eid( '11189ee47ddf4796b718a483b379f976' ) self.assertEqual(len(entries), 1) - self.assertEqual(entries[0].uid, '11189ee47ddf4796b718a483b379f976') - entries = self.collection.entries_from_short_uid( + self.assertEqual(entries[0].eid, '11189ee47ddf4796b718a483b379f976') + entries = self.collection.entries_from_short_eid( '12345678' ) self.assertEqual(len(entries), 0) @@ -140,9 +140,9 @@ class testCollection(unittest.TestCase): self.assertEqual(len(cm.output), 1) self.assertIn("non_existing_file", cm.output[0]) - def test_get_entry_missing_uid(self): + def test_get_entry_missing_eid(self): self.collection = lesana.Collection('tests/data/simple') - entry = self.collection.entry_from_uid('this is not an uid') + entry = self.collection.entry_from_eid('this is not an eid') self.assertIsNone(entry) def test_render_collection(self): @@ -170,14 +170,14 @@ class testEntries(unittest.TestCase): with open(os.path.join(self.basepath, fname)) as fp: data = ruamel.yaml.safe_load(fp) entry = lesana.Entry(self.collection, data=data, fname=fname) - self.assertEqual(entry.idterm, 'Q'+data['uid']) + self.assertEqual(entry.idterm, 'Q'+data['eid']) fname = '11189ee47ddf4796b718a483b379f976.yaml' - uid = '11189ee47ddf4796b718a483b379f976' + eid = '11189ee47ddf4796b718a483b379f976' with open(os.path.join(self.basepath, fname)) as fp: data = ruamel.yaml.safe_load(fp) entry = lesana.Entry(self.collection, data=data, fname=fname) - self.assertEqual(entry.idterm, 'Q'+uid) - self.assertEqual(entry.short_id, uid[:8]) + self.assertEqual(entry.idterm, 'Q'+eid) + self.assertEqual(entry.short_id, eid[:8]) def test_write_new(self): new_entry = lesana.Entry(self.collection) @@ -194,71 +194,71 @@ class testEntries(unittest.TestCase): self.assertIsInstance(written['name'], str) def test_entry_representation(self): - uid = '11189ee47ddf4796b718a483b379f976' - entry = self.collection.entry_from_uid(uid) + eid = '11189ee47ddf4796b718a483b379f976' + entry = self.collection.entry_from_eid(eid) self.assertEqual( str(entry), - uid + eid ) - label = '{{ uid }}: {{ name }}' + label = '{{ eid }}: {{ name }}' self.collection.settings['entry_label'] = label self.assertEqual( str(entry), - '{uid}: {name}'.format(uid=uid, name='Another item') + '{eid}: {name}'.format(eid=eid, name='Another item') ) - def test_entry_creation_uid_but_no_filename(self): + def test_entry_creation_eid_but_no_filename(self): fname = '11189ee47ddf4796b718a483b379f976.yaml' with open(os.path.join(self.basepath, fname)) as fp: data = ruamel.yaml.safe_load(fp) - data['uid'] = '11189ee47ddf4796b718a483b379f976' + data['eid'] = '11189ee47ddf4796b718a483b379f976' entry = lesana.Entry(self.collection, data=data) self.assertEqual(entry.fname, fname) - def test_entry_creation_no_uid_no_filename(self): + def test_entry_creation_no_eid_no_filename(self): fname = '11189ee47ddf4796b718a483b379f976.yaml' with open(os.path.join(self.basepath, fname)) as fp: data = ruamel.yaml.safe_load(fp) entry = lesana.Entry(self.collection, data=data) - self.assertIsNotNone(entry.uid) + self.assertIsNotNone(entry.eid) self.assertIsNotNone(entry.fname) - def test_entry_creation_filename_but_no_uid(self): + def test_entry_creation_filename_but_no_eid(self): fname = '11189ee47ddf4796b718a483b379f976.yaml' - uid = '11189ee47ddf4796b718a483b379f976' + eid = '11189ee47ddf4796b718a483b379f976' with open(os.path.join(self.basepath, fname)) as fp: data = ruamel.yaml.safe_load(fp) entry = lesana.Entry(self.collection, data=data, fname=fname) - self.assertEqual(entry.uid, uid) + self.assertEqual(entry.eid, eid) - def test_entry_str_filename_and_uid(self): + def test_entry_str_filename_and_eid(self): fname = '11189ee47ddf4796b718a483b379f976.yaml' with open(os.path.join(self.basepath, fname)) as fp: data = ruamel.yaml.safe_load(fp) - data['uid'] = '11189ee47ddf4796b718a483b379f976' + data['eid'] = '11189ee47ddf4796b718a483b379f976' entry = lesana.Entry(self.collection, data=data) - self.assertEqual(str(entry), data['uid']) - self.collection.settings['entry_label'] = '{{ uid }}: {{ name }}' - self.assertEqual(str(entry), data['uid'] + ': Another item') + self.assertEqual(str(entry), data['eid']) + self.collection.settings['entry_label'] = '{{ eid }}: {{ name }}' + self.assertEqual(str(entry), data['eid'] + ': Another item') - def test_entry_str_filename_no_uid(self): + def test_entry_str_filename_no_eid(self): fname = '11189ee47ddf4796b718a483b379f976.yaml' with open(os.path.join(self.basepath, fname)) as fp: data = ruamel.yaml.safe_load(fp) entry = lesana.Entry(self.collection, data=data) - uid = entry.uid - self.assertEqual(str(entry), uid) - self.collection.settings['entry_label'] = '{{ uid }}: {{ name }}' - self.assertEqual(str(entry), uid + ': Another item') + eid = entry.eid + self.assertEqual(str(entry), eid) + self.collection.settings['entry_label'] = '{{ eid }}: {{ name }}' + self.assertEqual(str(entry), eid + ': Another item') def test_render_entry(self): fname = '11189ee47ddf4796b718a483b379f976.yaml' with open(os.path.join(self.basepath, fname)) as fp: data = ruamel.yaml.safe_load(fp) entry = lesana.Entry(self.collection, data=data) - uid = entry.uid + eid = entry.eid res = entry.render('tests/data/simple/templates/trivial_template.txt') - self.assertIn(uid, res) + self.assertIn(eid, res) def test_empty_data(self): entry = lesana.Entry(self.collection) @@ -298,7 +298,7 @@ class testComplexCollection(unittest.TestCase): self.assertIsInstance(m, lesana.Entry) def test_boolean_field(self): - entry = self.collection.entry_from_uid( + entry = self.collection.entry_from_eid( '73097121f1874a6ea2f927db7dc4f11e' ) self.assertIsInstance(entry.data['exists'], bool) @@ -412,7 +412,7 @@ class testCollectionCreation(unittest.TestCase): mset = collection._enquire.get_mset(0, 10) self.assertEqual(mset.get_matches_estimated(), 0) - def test_partial_uid_deletion(self): + def test_partial_eid_deletion(self): tmpdir = tempfile.mkdtemp() shutil.copy('tests/data/simple/settings.yaml', tmpdir) shutil.copytree( |