diff options
-rw-r--r-- | lesana/command.py | 46 | ||||
-rwxr-xr-x | scripts/lesana | 17 |
2 files changed, 55 insertions, 8 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( diff --git a/scripts/lesana b/scripts/lesana index b8b9929..408aa2c 100755 --- a/scripts/lesana +++ b/scripts/lesana @@ -2,7 +2,7 @@ import argparse -from lesana.command import New, Edit, Show, Index, Search, Init, Remove +import lesana.command class Lesana(): """ @@ -10,13 +10,14 @@ class Lesana(): """ commands = ( - ('new', New()), - ('edit', Edit()), - ('show', Show()), - ('index', Index()), - ('search', Search()), - ('init', Init()), - ('rm', Remove()), + ('new', lesana.command.New()), + ('edit', lesana.command.Edit()), + ('show', lesana.command.Show()), + ('index', lesana.command.Index()), + ('search', lesana.command.Search()), + ('export', lesana.command.Export()), + ('init', lesana.command.Init()), + ('rm', lesana.command.Remove()), ) def _main(self): |