Mercurial > piecrust2
annotate piecrust/templating/pystacheengine.py @ 411:e7b865f8f335
bake: Enable multiprocess baking.
Baking is now done by running a worker per CPU, and sending jobs to them.
This changes several things across the codebase:
* Ability to not cache things related to pages other than the 'main' page
(i.e. the page at the bottom of the execution stack).
* Decouple the baking process from the bake records, so only the main process
keeps track (and modifies) the bake record.
* Remove the need for 'batch page getters' and loading a page directly from
the page factories.
There are various smaller changes too included here, including support for
scope performance timers that are saved with the bake record and can be
printed out to the console. Yes I got carried away.
For testing, the in-memory 'mock' file-system doesn't work anymore, since
we're spawning processes, so this is replaced by a 'tmpfs' file-system which
is saved in temporary files on disk and deleted after tests have run.
author | Ludovic Chabant <ludovic@chabant.com> |
---|---|
date | Fri, 12 Jun 2015 17:09:19 -0700 |
parents | 139179dc7abd |
children | f4b7c8f183a4 |
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 |