Commit 964d0f7c authored by Saurabh's avatar Saurabh

jinja2: fix display of source in traceback

parent e9b7c1b2
2.7 (2015-05-18)
================
* jinja2: fix display of source in traceback when there is an error in the
root template (or in instance parameters).
2.6 (2014-11-26) 2.6 (2014-11-26)
================ ================
......
from setuptools import setup, find_packages from setuptools import setup, find_packages
import os import os
version = '2.6' version = '2.7'
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',
......
...@@ -134,14 +134,13 @@ LOADER_TYPE_DICT = { ...@@ -134,14 +134,13 @@ LOADER_TYPE_DICT = {
class Recipe(object): class Recipe(object):
mode = 0777 # BBB: 0666 may have been a better default value mode = 0777 # BBB: 0666 may have been a better default value
loader = None
umask = None umask = None
def __init__(self, buildout, name, options): def __init__(self, buildout, name, options):
template = options['template'] template = options['template']
if template.startswith('inline:'): if template.startswith('inline:'):
template = template[7:].lstrip('\r\n') source = template[7:].lstrip('\r\n')
self.get_template = lambda: template template = '<inline>'
else: else:
template = zc.buildout.download.Download( template = zc.buildout.download.Download(
buildout['buildout'], buildout['buildout'],
...@@ -150,7 +149,7 @@ class Recipe(object): ...@@ -150,7 +149,7 @@ class Recipe(object):
template, template,
md5sum=options.get('md5sum'), md5sum=options.get('md5sum'),
)[0] )[0]
self.get_template = lambda: open(template).read() source = 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 = {}
...@@ -167,10 +166,12 @@ class Recipe(object): ...@@ -167,10 +166,12 @@ class Recipe(object):
import_delimiter, import_delimiter,
) )
if import_dict: if import_dict:
self.loader = RelaxedPrefixLoader(import_dict, loader = RelaxedPrefixLoader(import_dict,
delimiter=import_delimiter) delimiter=import_delimiter)
else:
loader = None
self.rendered = options['rendered'] self.rendered = options['rendered']
self.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()
if _buildout_safe_dumps is not None: if _buildout_safe_dumps is not None:
...@@ -191,6 +192,14 @@ class Recipe(object): ...@@ -191,6 +192,14 @@ class Recipe(object):
umask_value = options.get('umask') umask_value = options.get('umask')
if umask_value: if umask_value:
self.umask = int(umask_value, 8) self.umask = int(umask_value, 8)
env = Environment(
extensions=extension_list,
undefined=StrictUndefined,
loader=loader)
# For overriding from_string method of jinja2
self.template = env.template_class.from_code(env,
env.compile(source, filename=template),
env.make_globals(None), None)
def install(self): def install(self):
# Unlink any existing file, so umask is always applied. # Unlink any existing file, so umask is always applied.
...@@ -208,17 +217,7 @@ class Recipe(object): ...@@ -208,17 +217,7 @@ class Recipe(object):
with os.fdopen(os.open(self.rendered, with os.fdopen(os.open(self.rendered,
os.O_CREAT | os.O_EXCL | os.O_WRONLY, os.O_CREAT | os.O_EXCL | os.O_WRONLY,
self.mode), 'w') as out: self.mode), 'w') as out:
out.write( out.write(self.template.render(**self.context))
Environment(
extensions=self.extension_list,
undefined=StrictUndefined,
loader=self.loader,
).from_string(
self.get_template(),
).render(
**self.context
)
)
return self.rendered return self.rendered
update = install update = install
......
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