summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorElena ``of Valhalla'' Grandi <valhalla@trueelena.org>2019-09-11 11:19:20 +0200
committerElena ``of Valhalla'' Grandi <valhalla@trueelena.org>2019-09-11 11:19:20 +0200
commitc0a33617f9d304933322496c1b7b36f0d3882c1b (patch)
tree0353e2e4bd6144390fcb5ee202484df52da131fa
parent04c28f10c56b36e0754b375e3e1209d472bb9584 (diff)
Move some templating code to the collection instead of commands.
-rw-r--r--lesana/collection.py27
-rw-r--r--lesana/command.py33
-rw-r--r--tests/data/simple/templates/collection_template.txt4
-rw-r--r--tests/data/simple/templates/trivial_template.txt1
-rw-r--r--tests/test_collection.py17
5 files changed, 61 insertions, 21 deletions
diff --git a/lesana/collection.py b/lesana/collection.py
index 966741c..766617c 100644
--- a/lesana/collection.py
+++ b/lesana/collection.py
@@ -105,6 +105,13 @@ class Entry(object):
})
return valid, errors
+ def render(self, template, searchpath='.'):
+ jtemplate = self.collection.get_template(template, searchpath)
+ try:
+ return jtemplate.render(entry=self)
+ except jinja2.exceptions.TemplateSyntaxError as e:
+ raise TemplatingError('Template Syntax Error: '+str(e))
+
class Collection(object):
"""
@@ -403,6 +410,20 @@ class Collection(object):
cache.commit()
cache.close()
+ def get_template(self, template_fname, searchpath='.'):
+ env = jinja2.Environment(
+ loader=jinja2.FileSystemLoader(
+ searchpath=searchpath,
+ followlinks=True,
+ ),
+ # TODO: add autoescaping settings
+ )
+ try:
+ template = env.get_template(template_fname)
+ except jinja2.exceptions.TemplateNotFound as e:
+ raise TemplatingError('Could not find template' + str(e))
+ return template
+
@classmethod
def init(
cls,
@@ -472,3 +493,9 @@ class Collection(object):
coll = cls(c_dir)
os.makedirs(os.path.join(coll.basedir, coll.itemdir), exist_ok=True)
return coll
+
+
+class TemplatingError(Exception):
+ """
+ Raised when there are errors rendering a jinja template
+ """
diff --git a/lesana/command.py b/lesana/command.py
index 16d38f3..6fb0f5f 100644
--- a/lesana/command.py
+++ b/lesana/command.py
@@ -3,8 +3,7 @@ import os
import subprocess
import sys
-import jinja2
-from . import Collection, Entry
+from . import Collection, Entry, TemplatingError
def edit_file_in_external_editor(filepath):
@@ -65,21 +64,6 @@ 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 = [
@@ -168,8 +152,11 @@ class Show(Command):
)
entry = entries[0]
if self.args.template:
- template = self.get_template_renderer(self.args.template)
- print(template.render(entry=entry))
+ try:
+ print(entry.render(self.args.template))
+ except TemplatingError as e:
+ logging.error("{}".format(e))
+ sys.exit(1)
else:
print(entry.yaml_data)
@@ -246,8 +233,12 @@ class Search(Command):
offset,
pagesize)
if self.args.template:
- template = self.get_template_renderer(self.args.template)
- print(template.render(entries=results))
+ try:
+ template = collection.get_template(self.args.template)
+ print(template.render(entries=results))
+ except TemplatingError as e:
+ logging.error("{}".format(e))
+ sys.exit(1)
else:
for entry in results:
print("{entry}".format(
diff --git a/tests/data/simple/templates/collection_template.txt b/tests/data/simple/templates/collection_template.txt
new file mode 100644
index 0000000..8873e1e
--- /dev/null
+++ b/tests/data/simple/templates/collection_template.txt
@@ -0,0 +1,4 @@
+{% for entry in entries %}
+{{ entry.short_id }}: {{ entry.data.name }}
+ {{ entry.data.description }}
+{% endfor %}
diff --git a/tests/data/simple/templates/trivial_template.txt b/tests/data/simple/templates/trivial_template.txt
new file mode 100644
index 0000000..df31f0c
--- /dev/null
+++ b/tests/data/simple/templates/trivial_template.txt
@@ -0,0 +1 @@
+{{ entry }}
diff --git a/tests/test_collection.py b/tests/test_collection.py
index d1d6455..09ff96c 100644
--- a/tests/test_collection.py
+++ b/tests/test_collection.py
@@ -145,6 +145,14 @@ class testCollection(unittest.TestCase):
entry = self.collection.entry_from_uid('this is not an uid')
self.assertIsNone(entry)
+ def test_render_collection(self):
+ self.collection = lesana.Collection('tests/data/simple')
+ template = self.collection.get_template(
+ 'tests/data/simple/templates/collection_template.txt'
+ )
+ res = template.render(entries=self.collection.get_all_documents())
+ self.assertIn('11189ee4: Another item', res)
+
class testEntries(unittest.TestCase):
def setUp(self):
@@ -243,6 +251,15 @@ class testEntries(unittest.TestCase):
self.collection.settings['entry_label'] = '{{ uid }}: {{ name }}'
self.assertEqual(str(entry), uid + ': Another item')
+ def test_render_entry(self):
+ fname = '11189ee47ddf4796b718a483b379f976.yaml'
+ with open(os.path.join(self.basepath, fname)) as fp:
+ data = ruamel.yaml.safe_load(fp)
+ entry = lesana.Entry(self.collection, data=data)
+ uid = entry.uid
+ res = entry.render('tests/data/simple/templates/trivial_template.txt')
+ self.assertIn(uid, res)
+
class testComplexCollection(unittest.TestCase):
@classmethod