Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 5 additions & 37 deletions cloudinit/templater.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,6 @@
import re
import sys

try:
from Cheetah.Template import Template as CTemplate

CHEETAH_AVAILABLE = True
except (ImportError, AttributeError):
CHEETAH_AVAILABLE = False

try:
from jinja2 import DebugUndefined as JUndefined
from jinja2 import Template as JTemplate
Expand Down Expand Up @@ -96,9 +89,6 @@ def replacer(match):


def detect_template(text):
def cheetah_render(content, params):
return CTemplate(content, searchList=[params]).respond()

def jinja_render(content, params):
# keep_trailing_newline is in jinja2 2.7+, not 2.6
add = "\n" if content.endswith("\n") else ""
Expand All @@ -119,14 +109,10 @@ def jinja_render(content, params):
rest = ""
type_match = TYPE_MATCHER.match(ident)
if not type_match:
if CHEETAH_AVAILABLE:
LOG.debug("Using Cheetah as the renderer for unknown template.")
return ("cheetah", cheetah_render, text)
else:
return ("basic", basic_render, text)
return ("basic", basic_render, text)
else:
template_type = type_match.group(1).lower().strip()
if template_type not in ("jinja", "cheetah", "basic"):
if template_type not in ("jinja", "basic"):
raise ValueError(
"Unknown template rendering type '%s' requested"
% template_type
Expand All @@ -139,27 +125,14 @@ def jinja_render(content, params):
return ("basic", basic_render, rest)
elif template_type == "jinja" and JINJA_AVAILABLE:
return ("jinja", jinja_render, rest)
if template_type == "cheetah" and not CHEETAH_AVAILABLE:
LOG.warning(
"Cheetah not available as the selected renderer for"
" desired template, reverting to the basic renderer."
)
return ("basic", basic_render, rest)
elif template_type == "cheetah" and CHEETAH_AVAILABLE:
return ("cheetah", cheetah_render, rest)
# Only thing left over is the basic renderer (it is always available).
return ("basic", basic_render, rest)


def render_from_file(fn, params):
if not params:
params = {}
# jinja in python2 uses unicode internally. All py2 str will be decoded.
# If it is given a str that has non-ascii then it will raise a
# UnicodeDecodeError. So we explicitly convert to unicode type here.
template_type, renderer, content = detect_template(
util.load_file(fn, decode=False).decode("utf-8")
)
template_type, renderer, content = detect_template(util.load_file(fn))
LOG.debug("Rendering content of '%s' using renderer %s", fn, template_type)
return renderer(content, params)

Expand All @@ -170,15 +143,13 @@ def render_to_file(fn, outfn, params, mode=0o644):


def render_string_to_file(content, outfn, params, mode=0o644):
"""Render string (or py2 unicode) to file.
Warning: py2 str with non-ascii chars will cause UnicodeDecodeError."""
"""Render string"""
contents = render_string(content, params)
util.write_file(outfn, contents, mode=mode)


def render_string(content, params):
"""Render string (or py2 unicode).
Warning: py2 str with non-ascii chars will cause UnicodeDecodeError."""
"""Render string"""
if not params:
params = {}
_template_type, renderer, content = detect_template(content)
Expand All @@ -196,6 +167,3 @@ def render_cloudcfg(variant, template, output):
sys.stdout.write(contents)
else:
write_file(output, contents, omode="w")


# vi: ts=4 expandtab
12 changes: 0 additions & 12 deletions tests/unittests/__init__.py
Original file line number Diff line number Diff line change
@@ -1,12 +0,0 @@
# This file is part of cloud-init. See LICENSE file for license information.

try:
# For test cases, avoid the following UserWarning to stderr:
# You don't have the C version of NameMapper installed ...
from Cheetah import NameMapper as _nm

_nm.C_VERSION = True
except ImportError:
pass

# vi: ts=4 expandtab
30 changes: 0 additions & 30 deletions tests/unittests/test_templating.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,6 @@
from cloudinit.util import load_file, write_file
from tests.unittests import helpers as test_helpers

try:
import Cheetah

HAS_CHEETAH = True
c = Cheetah # make pyflakes and pylint happy, as Cheetah is not used here
except ImportError:
HAS_CHEETAH = False


class TestTemplates(test_helpers.CiTestCase):

Expand Down Expand Up @@ -52,28 +44,6 @@ def test_render_basic(self):
out_data = templater.basic_render(in_data, {"b": 2})
self.assertEqual(expected_data.strip(), out_data)

@test_helpers.skipIf(not HAS_CHEETAH, "cheetah renderer not available")
def test_detection(self):
blob = "## template:cheetah"

(template_type, _renderer, contents) = templater.detect_template(blob)
self.assertIn("cheetah", template_type)
self.assertEqual("", contents.strip())

blob = "blahblah $blah"
(template_type, _renderer, _contents) = templater.detect_template(blob)
self.assertIn("cheetah", template_type)
self.assertEqual(blob, contents)

blob = "##template:something-new"
self.assertRaises(ValueError, templater.detect_template, blob)

def test_render_cheetah(self):
blob = """## template:cheetah
$a,$b"""
c = templater.render_string(blob, {"a": 1, "b": 2})
self.assertEqual("1,2", c)

def test_render_jinja(self):
blob = """## template:jinja
{{a}},{{b}}"""
Expand Down