diff options
author | Joshua Harlow <harlowja@yahoo-inc.com> | 2014-03-05 15:05:59 -0800 |
---|---|---|
committer | Joshua Harlow <harlowja@yahoo-inc.com> | 2014-03-05 15:05:59 -0800 |
commit | cc79c859115ceb520877c0afaece4c28d6031499 (patch) | |
tree | 1deb6b7613faa9d150519222556e9a0c5991bf3f /cloudinit | |
parent | 3f3450660f5f7118ac0583de5d137120c1ae4abd (diff) | |
download | vyos-cloud-init-cc79c859115ceb520877c0afaece4c28d6031499.tar.gz vyos-cloud-init-cc79c859115ceb520877c0afaece4c28d6031499.zip |
Switch to jinja & adjust tpls
Diffstat (limited to 'cloudinit')
-rw-r--r-- | cloudinit/templater.py | 48 |
1 files changed, 30 insertions, 18 deletions
diff --git a/cloudinit/templater.py b/cloudinit/templater.py index a7527964..915b188e 100644 --- a/cloudinit/templater.py +++ b/cloudinit/templater.py @@ -23,33 +23,41 @@ import re from Cheetah.Template import Template as CTemplate -from mako.template import Template as MTemplate + +import jinja2 +from jinja2 import Template as JTemplate from cloudinit import log as logging from cloudinit import util LOG = logging.getLogger(__name__) -DEF_RENDERER = (lambda content, params: - CTemplate(content, searchList=[params]).respond()) +DEF_RENDERER = 'cheetah' RENDERERS = { - 'mako': lambda content, params: MTemplate(content).render(**params), - 'cheetah': DEF_RENDERER, + 'jinja': (lambda content, params: + JTemplate(content, + undefined=jinja2.StrictUndefined, + trim_blocks=True).render(**params)), + 'cheetah': (lambda content, params: + CTemplate(content, searchList=[params]).respond()), } TYPE_MATCHER = re.compile(r"##\s*template:(.*)", re.I) def detect_template(text): - lines = text.splitlines() - if not lines: - return DEF_RENDERER - line = lines[0] - type_match = TYPE_MATCHER.match(line) - if not type_match: - return DEF_RENDERER - template_type = type_match.group(1).lower().strip() - if template_type not in RENDERERS: - LOG.warn("Unknown template type requested: %s", template_type) - return RENDERERS.get(template_type, DEF_RENDERER) + try: + ident, rest = text.split("\n", 1) + except ValueError: + return (DEF_RENDERER, text) + else: + type_match = TYPE_MATCHER.match(ident) + if not type_match: + return (DEF_RENDERER, text) + template_type = type_match.group(1).lower().strip() + if template_type not in RENDERERS: + raise ValueError("Unknown template type '%s' requested" + % template_type) + else: + return (template_type, rest) def render_from_file(fn, params): @@ -64,5 +72,9 @@ def render_to_file(fn, outfn, params, mode=0644): def render_string(content, params): if not params: params = {} - renderer = detect_template(content) - return renderer(content, params) + try: + renderer, content = detect_template(content) + except ValueError: + renderer = DEF_RENDERER + LOG.debug("Rendering %s using renderer '%s'", content, renderer) + return RENDERERS[renderer](content, params) |