diff options
| author | Elena ``of Valhalla'' Grandi <valhalla@trueelena.org> | 2019-09-13 14:28:38 +0200 | 
|---|---|---|
| committer | Elena ``of Valhalla'' Grandi <valhalla@trueelena.org> | 2019-09-13 14:28:38 +0200 | 
| commit | 4c291a3710486eab6edd97b5e0619241f5909173 (patch) | |
| tree | 563c389cc90f28c17070f558033190090e36816c | |
| parent | 935047878ba6ad473d2d32b8163e124bc7b2e5c2 (diff) | |
New command to export data from a lesana collection to another one.
| -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): | 
