Commit 90772890 authored by Julien Muchembled's avatar Julien Muchembled

jinja2: add support for inline templates

parent 8b177917
2.4.3-dev (unreleased) 2.4.3 (2013-08-02)
====================== ======================
* No changes yet. * jinja2: add support for inline templates.
2.4.2 (2012-08-21) 2.4.2 (2012-08-21)
================== ==================
......
from setuptools import setup, find_packages from setuptools import setup, find_packages
import os import os
version = '2.4.3-dev' version = '2.4.3'
name = 'slapos.recipe.template' name = 'slapos.recipe.template'
long_description = open("README.txt").read() + "\n" + \ long_description = open("README.txt").read() + "\n" + \
open(os.path.join('slapos', 'recipe', open(os.path.join('slapos', 'recipe',
......
...@@ -65,6 +65,9 @@ Mandatory: ...@@ -65,6 +65,9 @@ Mandatory:
``template`` ``template``
Template url/path, as accepted by zc.buildout.download.Download.__call__ . Template url/path, as accepted by zc.buildout.download.Download.__call__ .
For very short template, it can make sense to put it directly into
buildout.cfg: the value is the template itself, prefixed by the string
"inline:" + an optional newline.
``rendered`` ``rendered``
Where rendered template should be stored. Where rendered template should be stored.
...@@ -390,14 +393,13 @@ Section dependency ...@@ -390,14 +393,13 @@ Section dependency
You can use other part of buildout in the template. This way this parts You can use other part of buildout in the template. This way this parts
will be installed as dependency:: will be installed as dependency::
>>> write('foo.in', '{{bar}}')
>>> write('buildout.cfg', ''' >>> write('buildout.cfg', '''
... [buildout] ... [buildout]
... parts = template ... parts = template
... ...
... [template] ... [template]
... recipe = slapos.recipe.template:jinja2 ... recipe = slapos.recipe.template:jinja2
... template = foo.in ... template = inline:{{bar}}
... rendered = foo ... rendered = foo
... context = key bar dependency:foobar ... context = key bar dependency:foobar
... ...
...@@ -443,7 +445,6 @@ Let's create a sample recipe modifying its option dict:: ...@@ -443,7 +445,6 @@ Let's create a sample recipe modifying its option dict::
Let's just use ``buildout.cfg`` using this egg:: Let's just use ``buildout.cfg`` using this egg::
>>> write('foo.in', '{{bar}}')
>>> write('buildout.cfg', >>> write('buildout.cfg',
... ''' ... '''
... [buildout] ... [buildout]
...@@ -452,7 +453,8 @@ Let's just use ``buildout.cfg`` using this egg:: ...@@ -452,7 +453,8 @@ Let's just use ``buildout.cfg`` using this egg::
... ...
... [template] ... [template]
... recipe = slapos.recipe.template:jinja2 ... recipe = slapos.recipe.template:jinja2
... template = foo.in ... template = inline:
... {{bar}}
... rendered = foo ... rendered = foo
... context = key bar sample:data ... context = key bar sample:data
... ...
......
...@@ -133,13 +133,19 @@ class Recipe(object): ...@@ -133,13 +133,19 @@ class Recipe(object):
umask = None umask = None
def __init__(self, buildout, name, options): def __init__(self, buildout, name, options):
self.template = zc.buildout.download.Download( template = options['template']
if template.startswith('inline:'):
template = template[7:].lstrip('\r\n')
self.get_template = lambda: template
else:
template = zc.buildout.download.Download(
buildout['buildout'], buildout['buildout'],
hash_name=True, hash_name=True,
)( )(
options['template'], template,
md5sum=options.get('md5sum'), md5sum=options.get('md5sum'),
)[0] )[0]
self.get_template = lambda: open(template).read()
import_delimiter = options.get('import-delimiter', import_delimiter = options.get('import-delimiter',
DEFAULT_IMPORT_DELIMITER) DEFAULT_IMPORT_DELIMITER)
import_dict = {} import_dict = {}
...@@ -203,7 +209,7 @@ class Recipe(object): ...@@ -203,7 +209,7 @@ class Recipe(object):
undefined=StrictUndefined, undefined=StrictUndefined,
loader=self.loader, loader=self.loader,
).from_string( ).from_string(
open(self.template).read(), self.get_template(),
).render( ).render(
**self.context **self.context
) )
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment