From b740568ddfe6194af7c2c5d7123b3cc2a07fa7f7 Mon Sep 17 00:00:00 2001 From: Joshua Harlow Date: Fri, 7 Mar 2014 19:33:41 -0800 Subject: Add some basic template tests --- cloudinit/templater.py | 27 ++++++++++++++------------- 1 file changed, 14 insertions(+), 13 deletions(-) (limited to 'cloudinit') diff --git a/cloudinit/templater.py b/cloudinit/templater.py index 915b188e..9922c633 100644 --- a/cloudinit/templater.py +++ b/cloudinit/templater.py @@ -44,21 +44,21 @@ TYPE_MATCHER = re.compile(r"##\s*template:(.*)", re.I) def detect_template(text): - try: + if text.find("\n") != -1: ident, rest = text.split("\n", 1) - except ValueError: + else: + ident = text + rest = '' + 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: - 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) - + return (template_type, rest) + def render_from_file(fn, params): return render_string(util.load_file(fn), params) @@ -74,7 +74,8 @@ def render_string(content, params): params = {} try: renderer, content = detect_template(content) - except ValueError: + except ValueError as e: renderer = DEF_RENDERER + LOG.warn("%s, using renderer %s", e, renderer) LOG.debug("Rendering %s using renderer '%s'", content, renderer) return RENDERERS[renderer](content, params) -- cgit v1.2.3