Commit 7fa2695a authored by Julien Muchembled's avatar Julien Muchembled

jinja2: rendered -> output, template -> url/inline

parent 70da5e82
...@@ -5,15 +5,17 @@ ...@@ -5,15 +5,17 @@
Similar to the default recipe but the template syntax is Jinja2 instead of Similar to the default recipe but the template syntax is Jinja2 instead of
buildout. Other significant differences are: buildout. Other significant differences are:
- For historical reasons, the generated file is specified with ``rendered``
instead of ``output``.
- For historical reasons, the template is specified with ``template`` only
and an inline template is prefixed with ``inline:`` + an optional newline.
- Rendering, and download if requested, is done during the install phase. - Rendering, and download if requested, is done during the install phase.
- Dependencies are explicit (see ``context`` option) instead of deduced from - Dependencies are explicit (see ``context`` option) instead of deduced from
the template. the template.
- Some extra features (options detailed below). - Some extra features (options detailed below).
For backward compatibility, the following old options are still supported:
- The generated file can be specified with ``rendered`` instead of ``output``.
- The template can be specified with ``template`` instead of ``url``/``inline``.
An inline template is prefixed with ``inline:`` + an optional newline.
Example demonstrating some types:: Example demonstrating some types::
>>> write('buildout.cfg', >>> write('buildout.cfg',
......
...@@ -71,24 +71,31 @@ class Recipe(object): ...@@ -71,24 +71,31 @@ class Recipe(object):
self.mode = int(mode, 8) if mode else None self.mode = int(mode, 8) if mode else None
self._init(name, options) self._init(name, options)
def _init(self, name, options): def _template(self, options):
self.output = options['output'] inline = options.get('inline')
rendered = options.get('inline')
try: try:
url = options['url'] url = options['url']
except KeyError: except KeyError:
if rendered is None: if inline is None:
raise raise
if self.md5sum: if self.md5sum:
raise UserError("options 'inline' & 'md5sum' conflict") raise UserError("options 'inline' & 'md5sum' conflict")
self.md5sum = True # tell update() to do nothing self.md5sum = True # tell update() to do nothing
self.rendered = rendered return True, inline
else: else:
if rendered: if inline:
raise UserError("options 'inline' & 'url' conflict") raise UserError("options 'inline' & 'url' conflict")
return False, url
def _init(self, name, options):
self.output = options['output']
inline, template = self._template(options)
if inline:
self.rendered = template
else:
options_sub = options._sub options_sub = options._sub
self.rendered = '$'.join(options_sub(s, None) self.rendered = '$'.join(options_sub(s, None)
for s in self._read(url).split('$$')) for s in self._read(template).split('$$'))
def _read(self, url, *args): def _read(self, url, *args):
path, is_temp = zc.buildout.download.Download( path, is_temp = zc.buildout.download.Download(
......
...@@ -165,8 +165,17 @@ class Recipe(Recipe): ...@@ -165,8 +165,17 @@ class Recipe(Recipe):
delimiter=import_delimiter) delimiter=import_delimiter)
else: else:
loader = None loader = None
self.output = options['rendered'] try:
self.template = options['template'] self.output = options['output']
except KeyError: # BBB
self.output = options['rendered']
template = options['template']
if template.startswith('inline:'):
self.template = True, template[7:].lstrip('\r\n')
else:
self.template = False, template
else:
self.template = self._template(options)
extension_list = [x for x in (y.strip() extension_list = [x for x in (y.strip()
for y in options.get('extensions', '').split()) if x] for y in options.get('extensions', '').split()) if x]
self.context = context = DEFAULT_CONTEXT.copy() self.context = context = DEFAULT_CONTEXT.copy()
...@@ -187,11 +196,10 @@ class Recipe(Recipe): ...@@ -187,11 +196,10 @@ class Recipe(Recipe):
loader=loader) loader=loader)
def _render(self): def _render(self):
template = self.template
env = self.env env = self.env
if template.startswith('inline:'): inline, template = self.template
source = template[7:].lstrip('\r\n') if inline:
compiled_source = env.compile(source, filename='<inline>') compiled_source = env.compile(template, filename='<inline>')
else: else:
try: try:
compiled_source = compiled_source_cache[template] compiled_source = compiled_source_cache[template]
......
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