aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorElena ``of Valhalla'' Grandi <valhalla@trueelena.org>2016-12-22 21:50:44 +0100
committerElena ``of Valhalla'' Grandi <valhalla@trueelena.org>2016-12-22 21:50:44 +0100
commit0038a648a74236e33144cb8c3bce42a0873ec44f (patch)
tree0061cacde856ea0761bd6883e7b31475a1db1a3d
parent5004001be342f46dd694d539a9a4b8bfbddfd69a (diff)
new command: edit
-rw-r--r--lesana/collection.py46
-rw-r--r--lesana/command.py29
-rwxr-xr-xscripts/lesana3
-rw-r--r--tests/test_collection.py7
4 files changed, 69 insertions, 16 deletions
diff --git a/lesana/collection.py b/lesana/collection.py
index 3af1cd4..d5c42e8 100644
--- a/lesana/collection.py
+++ b/lesana/collection.py
@@ -104,7 +104,7 @@ class Collection(object):
pass
doc.set_data(entry.yaml_data)
doc.add_boolean_term(entry.idterm)
- doc.add_value(0, entry.fname)
+ doc.add_value(0, entry.fname.encode('utf-8'))
cache.replace_document(entry.idterm, doc)
@@ -170,7 +170,7 @@ class Collection(object):
with open(complete_name, 'w') as fp:
fp.write(e.yaml_data)
- def search(self, querystring, offset=0, pagesize=12):
+ def _get_cache(self):
try:
cache = xapian.Database(
os.path.join(self.basedir, '.lesana/xapian'),
@@ -181,6 +181,10 @@ class Collection(object):
cache = xapian.Database(
os.path.join(self.basedir, '.lesana/xapian'),
)
+ return cache
+
+ def search(self, querystring, offset=0, pagesize=12):
+ cache = self._get_cache()
queryparser = xapian.QueryParser()
queryparser.set_stemmer(self.stemmer)
@@ -193,17 +197,29 @@ class Collection(object):
enquire.set_query(query)
for match in enquire.get_mset(offset, pagesize):
- fname = match.document.get_value(0)
- if self.safe:
- data = ruamel.yaml.safe_load(match.document.get_data())
- else:
- data = ruamel.yaml.load(
- match.document.get_data(),
- ruamel.yaml.RoundTripLoader
- )
- entry = Entry(
- self,
- data=data,
- fname=fname,
+ yield self._match_to_entry(match)
+
+ def _match_to_entry(self, match):
+ fname = match.document.get_value(0).decode('utf-8')
+ print(type(fname))
+ if self.safe:
+ data = ruamel.yaml.safe_load(match.document.get_data())
+ else:
+ data = ruamel.yaml.load(
+ match.document.get_data(),
+ ruamel.yaml.RoundTripLoader
)
- yield entry
+ entry = Entry(
+ self,
+ data=data,
+ fname=fname,
+ )
+ return entry
+
+ def entry_from_uid(self, uid):
+ cache = self._get_cache()
+ query = xapian.Query('Q'+uid)
+ enquire = xapian.Enquire(cache)
+ enquire.set_query(query)
+ # FIXME: if more items are returned, something is wrong?
+ return self._match_to_entry(enquire.get_mset(0, 1)[0])
diff --git a/lesana/command.py b/lesana/command.py
index 6f3fefe..23b9b6b 100644
--- a/lesana/command.py
+++ b/lesana/command.py
@@ -33,6 +33,35 @@ class New(gadona.Command):
print(new_entry.fname)
+class Edit(gadona.Command):
+ name = 'edit'
+ arguments = [
+ (['--collection', '-c'], dict(
+ help='The collection to work on (default .)'
+ )),
+ (['uid'], dict(
+ help='uid of an entry to edit',
+ )),
+ ]
+
+ def main(self):
+ collection = Collection(self.settings.collection)
+ entry = collection.entry_from_uid(self.settings.uid)
+ print(collection.itemdir,entry.fname)
+ filepath = os.path.join(
+ collection.itemdir,
+ entry.fname
+ )
+ try:
+ subprocess.call(['sensible-editor', filepath])
+ except FileNotFoundError as e:
+ logging.warning(
+ "Could not open new file with editor: {}".format(str(e))
+ )
+ else:
+ collection.update_cache([filepath])
+
+
class Index(gadona.Command):
name = 'index'
arguments = [
diff --git a/scripts/lesana b/scripts/lesana
index c909888..a452c64 100755
--- a/scripts/lesana
+++ b/scripts/lesana
@@ -1,13 +1,14 @@
#!/usr/bin/env python3
import gadona
-from lesana.command import New, Index, Search
+from lesana.command import New, Edit, Index, Search
if __name__ == '__main__':
app = gadona.App()
app.description = "Manage collections"
app.commands = [
New(),
+ Edit(),
Index(),
Search(),
]
diff --git a/tests/test_collection.py b/tests/test_collection.py
index ccd535f..9522ab8 100644
--- a/tests/test_collection.py
+++ b/tests/test_collection.py
@@ -56,6 +56,13 @@ class testCollectionLoading(unittest.TestCase):
for m in matches:
self.assertIsInstance(m, lesana.Entry)
+ def test_entry_from_uid(self):
+ self.collection = lesana.Collection('tests/data/simple')
+ entry = self.collection.entry_from_uid(
+ '11189ee47ddf4796b718a483b379f976'
+ )
+ self.assertEqual(entry.uid, '11189ee47ddf4796b718a483b379f976')
+
class testEntries(unittest.TestCase):