diff options
| author | Elena ``of Valhalla'' Grandi <valhalla@trueelena.org> | 2019-09-10 12:06:20 +0200 | 
|---|---|---|
| committer | Elena ``of Valhalla'' Grandi <valhalla@trueelena.org> | 2019-09-10 12:06:20 +0200 | 
| commit | 04c28f10c56b36e0754b375e3e1209d472bb9584 (patch) | |
| tree | 874956a26dca011b331f1c2fcc78061ac0913fe6 | |
| parent | f44e855f0462b105f54a0dea826e6fdcd60925db (diff) | |
New command: Show
| -rw-r--r-- | lesana/command.py | 61 | ||||
| -rwxr-xr-x | scripts/lesana | 3 | 
2 files changed, 49 insertions, 15 deletions
| diff --git a/lesana/command.py b/lesana/command.py index dbad2d2..16d38f3 100644 --- a/lesana/command.py +++ b/lesana/command.py @@ -65,6 +65,21 @@ class Command():          self.args = args          self.main() +    def get_template_renderer(self, template_fname): +        env = jinja2.Environment( +            loader=jinja2.FileSystemLoader( +                searchpath='.', +                followlinks=True, +                ), +            # TODO: add autoescaping settings +            ) +        try: +            template = env.get_template(template_fname) +        except jinja2.exceptions.TemplateNotFound as e: +            logging.error("Could not find template: {}".format(e)) +            sys.exit(1) +        return template +  class New(Command):      arguments = [ @@ -129,6 +144,36 @@ class Edit(Command):          print(entry) +class Show(Command): +    arguments = [ +        (['--collection', '-c'], dict( +            help='The collection to work on (default .)' +            )), +        (['--template', '-t'], dict( +            help='Template to use when displaying results', +            )), +        (['uid'], dict( +            help='uid of an entry to edit', +            )), +        ] + +    def main(self): +        collection = Collection(self.args.collection) +        entries = collection.entries_from_short_uid(self.args.uid) +        if len(entries) > 1: +            return "{} is not an unique uid".format(self.args.uid) +        if not entries: +            return "Could not find an entry with uid starting with: {}".format( +                self.args.uid +                ) +        entry = entries[0] +        if self.args.template: +            template = self.get_template_renderer(self.args.template) +            print(template.render(entry=entry)) +        else: +            print(entry.yaml_data) + +  class Index(Command):      arguments = [          (['--collection', '-c'], dict( @@ -201,20 +246,8 @@ class Search(Command):                      offset,                      pagesize)          if self.args.template: -            env = jinja2.Environment( -                loader=jinja2.FileSystemLoader( -                    searchpath='.', -                    followlinks=True, -                    ), -                # TODO: add autoescaping settings -                ) -            try: -                template = env.get_template(self.args.template) -            except jinja2.exceptions.TemplateNotFound as e: -                logging.error("Could not find template: {}".format(e)) -                sys.exit(1) -            else: -                print(template.render(entries=results)) +            template = self.get_template_renderer(self.args.template) +            print(template.render(entries=results))          else:              for entry in results:                  print("{entry}".format( diff --git a/scripts/lesana b/scripts/lesana index 713f8a7..b8b9929 100755 --- a/scripts/lesana +++ b/scripts/lesana @@ -2,7 +2,7 @@  import argparse -from lesana.command import New, Edit, Index, Search, Init, Remove +from lesana.command import New, Edit, Show, Index, Search, Init, Remove  class Lesana():      """ @@ -12,6 +12,7 @@ class Lesana():      commands = (          ('new', New()),          ('edit', Edit()), +        ('show', Show()),          ('index', Index()),          ('search', Search()),          ('init', Init()), | 
