annotate piecrust/templating/pystacheengine.py @ 380:f33712c4cfab

routing: Fix bugs with matching URLs with correct route but missing metadata. When matching a route like `/foo/%slug%` against an URL like `/foo`, the route will (correctly) return a match, but it will be completely missing the `slug` metadata, resulting in problems elsewhere. This change makes it so that any missing route metadata will be filled in with an empty string. And because this means generated URLs may differ from the incoming URL when using trailing slashes (`/foo/` _vs._ `/foo`), we make the assert in the chef server handle those discrepancies.
author Ludovic Chabant <ludovic@chabant.com>
date Sun, 10 May 2015 00:34:21 -0700
parents 139179dc7abd
children f4b7c8f183a4
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
185
139179dc7abd render: Add support for a Mustache template engine.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1 import logging
139179dc7abd render: Add support for a Mustache template engine.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
2 import pystache
139179dc7abd render: Add support for a Mustache template engine.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
3 from piecrust.templating.base import (
139179dc7abd render: Add support for a Mustache template engine.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
4 TemplateEngine, TemplateNotFoundError, TemplatingError)
139179dc7abd render: Add support for a Mustache template engine.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
5
139179dc7abd render: Add support for a Mustache template engine.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
6
139179dc7abd render: Add support for a Mustache template engine.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
7 logger = logging.getLogger(__name__)
139179dc7abd render: Add support for a Mustache template engine.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
8
139179dc7abd render: Add support for a Mustache template engine.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
9
139179dc7abd render: Add support for a Mustache template engine.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
10 class PystacheTemplateEngine(TemplateEngine):
139179dc7abd render: Add support for a Mustache template engine.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
11 ENGINE_NAMES = ['mustache']
139179dc7abd render: Add support for a Mustache template engine.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
12 EXTENSIONS = ['mustache']
139179dc7abd render: Add support for a Mustache template engine.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
13
139179dc7abd render: Add support for a Mustache template engine.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
14 def __init__(self):
139179dc7abd render: Add support for a Mustache template engine.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
15 self.renderer = None
139179dc7abd render: Add support for a Mustache template engine.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
16
139179dc7abd render: Add support for a Mustache template engine.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
17 def renderString(self, txt, data, filename=None):
139179dc7abd render: Add support for a Mustache template engine.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
18 self._ensureLoaded()
139179dc7abd render: Add support for a Mustache template engine.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
19 try:
139179dc7abd render: Add support for a Mustache template engine.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
20 return self.renderer.render(txt, data)
139179dc7abd render: Add support for a Mustache template engine.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
21 except pystache.TemplateNotFoundError as ex:
139179dc7abd render: Add support for a Mustache template engine.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
22 raise TemplateNotFoundError() from ex
139179dc7abd render: Add support for a Mustache template engine.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
23 except pystache.PystacheError as ex:
139179dc7abd render: Add support for a Mustache template engine.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
24 raise TemplatingError(str(ex), filename) from ex
139179dc7abd render: Add support for a Mustache template engine.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
25
139179dc7abd render: Add support for a Mustache template engine.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
26 def renderFile(self, paths, data):
139179dc7abd render: Add support for a Mustache template engine.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
27 self._ensureLoaded()
139179dc7abd render: Add support for a Mustache template engine.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
28 tpl = None
139179dc7abd render: Add support for a Mustache template engine.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
29 logger.debug("Looking for template: %s" % paths)
139179dc7abd render: Add support for a Mustache template engine.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
30 for p in paths:
139179dc7abd render: Add support for a Mustache template engine.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
31 if not p.endswith('.mustache'):
139179dc7abd render: Add support for a Mustache template engine.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
32 raise TemplatingError(
139179dc7abd render: Add support for a Mustache template engine.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
33 "The Mustache template engine only accepts template "
139179dc7abd render: Add support for a Mustache template engine.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
34 "filenames with a `.mustache` extension. Got: %s" %
139179dc7abd render: Add support for a Mustache template engine.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
35 p)
139179dc7abd render: Add support for a Mustache template engine.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
36 name = p[:-9] # strip `.mustache`
139179dc7abd render: Add support for a Mustache template engine.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
37 try:
139179dc7abd render: Add support for a Mustache template engine.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
38 tpl = self.renderer.load_template(name)
139179dc7abd render: Add support for a Mustache template engine.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
39 except Exception as ex:
139179dc7abd render: Add support for a Mustache template engine.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
40 print(p, ex)
139179dc7abd render: Add support for a Mustache template engine.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
41 pass
139179dc7abd render: Add support for a Mustache template engine.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
42
139179dc7abd render: Add support for a Mustache template engine.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
43 if tpl is None:
139179dc7abd render: Add support for a Mustache template engine.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
44 raise TemplateNotFoundError()
139179dc7abd render: Add support for a Mustache template engine.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
45
139179dc7abd render: Add support for a Mustache template engine.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
46 try:
139179dc7abd render: Add support for a Mustache template engine.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
47 return self.renderer.render(tpl, data)
139179dc7abd render: Add support for a Mustache template engine.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
48 except pystache.PystacheError as ex:
139179dc7abd render: Add support for a Mustache template engine.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
49 raise TemplatingError(str(ex)) from ex
139179dc7abd render: Add support for a Mustache template engine.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
50
139179dc7abd render: Add support for a Mustache template engine.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
51 def _ensureLoaded(self):
139179dc7abd render: Add support for a Mustache template engine.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
52 if self.renderer:
139179dc7abd render: Add support for a Mustache template engine.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
53 return
139179dc7abd render: Add support for a Mustache template engine.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
54
139179dc7abd render: Add support for a Mustache template engine.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
55 self.renderer = pystache.Renderer(
139179dc7abd render: Add support for a Mustache template engine.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
56 search_dirs=self.app.templates_dirs)
139179dc7abd render: Add support for a Mustache template engine.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
57