diff options
author | Elena ``of Valhalla'' Grandi <valhalla@trueelena.org> | 2016-12-13 18:29:19 +0100 |
---|---|---|
committer | Elena ``of Valhalla'' Grandi <valhalla@trueelena.org> | 2016-12-13 18:29:19 +0100 |
commit | 1c587b319a308a877608531961491f1dbb79317b (patch) | |
tree | 2722df12c41c9653dc426aa30c17c785d2489f2a | |
parent | 8ec0cae371a0b392b81bf34d5ee849c233ebbf65 (diff) |
Indexing of entries
-rw-r--r-- | lesana/__init__.py | 2 | ||||
-rw-r--r-- | lesana/collection.py | 32 | ||||
-rw-r--r-- | tests/data/simple/items/085682ed-6792-499d-a3ab-9aebd683c011.yaml | 2 | ||||
-rw-r--r-- | tests/test_collection.py | 23 |
4 files changed, 50 insertions, 9 deletions
diff --git a/lesana/__init__.py b/lesana/__init__.py index 969ac3f..64fbd80 100644 --- a/lesana/__init__.py +++ b/lesana/__init__.py @@ -1 +1 @@ -from .collection import Collection +from .collection import Collection, Entry diff --git a/lesana/collection.py b/lesana/collection.py index cff94f7..b3c994f 100644 --- a/lesana/collection.py +++ b/lesana/collection.py @@ -1,26 +1,44 @@ import logging import os +import uuid import ruamel.yaml import xapian class Entry(object): - def __init__(self, collection, data={}): + def __init__(self, collection, data={}, fname=None): self.collection = collection self.data = data + self.fname = fname + @property def indexed_fields(self): - return [] + fields = [] + for field in self.collection.settings['fields']: + prefix = field.get('prefix', 'X'+field['name'].upper()) + fields.append({ + 'value': self.data.get(field['name']), + 'prefix': prefix, + 'name': field['name'], + 'free_search': True, + }) + + return fields @property def yaml_data(self): - return "" + return ruamel.yaml.dump(self.data, Dumper=ruamel.yaml.RoundTripDumper) @property def idterm(self): - return "Q" - + uid = self.data.get('uid', None) + if not uid: + if self.fname: + uid, ext = os.path.splitext(os.path.basename(self.fname)) + else: + uid = uuid.uuid4().hex + return "Q"+uid class Collection(object): @@ -67,10 +85,10 @@ class Collection(object): # things # Fields with prefix, for field search - for field in entry.indexed_fields(): + for field in entry.indexed_fields: self.indexer.index_text(field['value'], 1, field['prefix']) # unprefixed fields, for full text search - for field in entry.indexed_fields(): + for field in entry.indexed_fields: if field.get('free_search', False): self.indexer.index_text(field['value']) self.indexer.increase_termpos() 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 a66bb67..16d6917 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 test that spans multiple lines. position: somewhere -id: 085682ed6792499da3ab9aebd683c011 +uid: 085682ed6792499da3ab9aebd683c011 diff --git a/tests/test_collection.py b/tests/test_collection.py index be9a4e5..c54dc32 100644 --- a/tests/test_collection.py +++ b/tests/test_collection.py @@ -3,6 +3,8 @@ import os.path import shutil import unittest +import ruamel.yaml + import lesana @@ -44,3 +46,24 @@ class testCollectionLoading(unittest.TestCase): self.collection = lesana.Collection('tests/data/simple') self.collection.safe = False self.collection.update_cache() + + +class testEntries(unittest.TestCase): + def setUp(self): + self.collection = lesana.Collection('tests/data/simple') + self.basepath = 'tests/data/simple/items' + + def test_simple(self): + fname = '085682ed-6792-499d-a3ab-9aebd683c011.yaml' + with open(os.path.join(self.basepath, fname)) as fp: + data = ruamel.yaml.load(fp) + entry = lesana.Entry(self.collection, data=data, fname=fname) + self.assertEqual(entry.idterm, 'Q'+data['uid']) + self.assertEqual(len(entry.indexed_fields), 3) + fname = '11189ee47ddf4796b718a483b379f976.yaml' + uid = '11189ee47ddf4796b718a483b379f976' + with open(os.path.join(self.basepath, fname)) as fp: + data = ruamel.yaml.load(fp) + entry = lesana.Entry(self.collection, data=data, fname=fname) + self.assertEqual(entry.idterm, 'Q'+uid) + self.assertEqual(len(entry.indexed_fields), 3) |