aboutsummaryrefslogtreecommitdiff
path: root/lesana/command.py
diff options
context:
space:
mode:
Diffstat (limited to 'lesana/command.py')
-rw-r--r--lesana/command.py61
1 files changed, 47 insertions, 14 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(