aboutsummaryrefslogtreecommitdiff
path: root/lesana
diff options
context:
space:
mode:
Diffstat (limited to 'lesana')
-rw-r--r--lesana/command.py46
1 files changed, 46 insertions, 0 deletions
diff --git a/lesana/command.py b/lesana/command.py
index 6fb0f5f..15be857 100644
--- a/lesana/command.py
+++ b/lesana/command.py
@@ -3,6 +3,8 @@ import os
import subprocess
import sys
+import ruamel.yaml
+
from . import Collection, Entry, TemplatingError
@@ -246,6 +248,50 @@ class Search(Command):
))
+class Export(Command):
+ arguments = [
+ (['--collection', '-c'], dict(
+ help='The collection to work on (default .)'
+ )),
+ (['--query', '-q'], dict(
+ help='Xapian query to search in the collection',
+ )),
+ (['destination'], dict(
+ help='The collection to export entries to'
+ )),
+ (['template'], dict(
+ help='Template to convert entries',
+ )),
+ ]
+
+ def main(self):
+ collection = Collection(self.args.collection)
+ destination = Collection(self.args.destination)
+ if not self.args.query:
+ results = collection.get_all_documents()
+ else:
+ collection.start_search(' '.join(self.args.query))
+ results = collection.get_all_search_results()
+ for entry in results:
+ try:
+ template = collection.get_template(self.args.template)
+ rendered = template.render(entry=entry)
+ data = ruamel.yaml.load(
+ template.render(entry=entry),
+ ruamel.yaml.RoundTripLoader
+ )
+ except TemplatingError as e:
+ logging.error("{}".format(e))
+ sys.exit(1)
+ try:
+ data = ruamel.yaml.load(rendered, ruamel.yaml.RoundTripLoader)
+ except ruamel.yaml.YAMLError as e:
+ logging.error("{}".format(e))
+ sys.exit(1)
+ e = Entry(destination, data=data)
+ destination.save_entries([e])
+
+
class Init(Command):
arguments = [
(['--collection', '-c'], dict(