X-Git-Url: https://gerrit.opnfv.org/gerrit/gitweb?a=blobdiff_plain;f=yardstick%2Fcommon%2Ftask_template.py;h=f6c1286095786863772966ebcb684def98172f48;hb=bdf55c2a6f000e0ca6462a9df4a0d411c3dd029e;hp=2739323bdcac9bac7bd60e4de5c111d723448440;hpb=c2d65daf876dca1c9bade1e7fa91653251c0c01b;p=yardstick.git diff --git a/yardstick/common/task_template.py b/yardstick/common/task_template.py index 2739323bd..f6c128609 100755 --- a/yardstick/common/task_template.py +++ b/yardstick/common/task_template.py @@ -7,12 +7,30 @@ # http://www.apache.org/licenses/LICENSE-2.0 # yardstick: this file is copied from rally and slightly modified ############################################################################## +from __future__ import absolute_import import re import jinja2 import jinja2.meta +import yaml + + +def finalize_for_yaml(elem): + """Render Jinja2 output specifically for YAML files""" + # Jinaj2 by default converts None to 'None', we can't allow this + # we could convert to empty string '', or we can convert to null, aka ~ + if elem is None: + return '~' + # convert data structures to inline YAML + # match builtin types because we shouldn't be trying to render complex types + if isinstance(elem, (dict, list)): + # remove newlines because we are injecting back into YAML + # use block style for single line + return yaml.safe_dump(elem, default_flow_style=True).replace('\n', '') + return elem class TaskTemplate(object): + @classmethod def render(cls, task_template, **kwargs): """Render jinja2 task template to Yardstick input task. @@ -36,18 +54,18 @@ class TaskTemplate(object): single_msg = ("Please specify template task argument:%s") raise TypeError((len(real_missing) > 1 and multi_msg or single_msg) % ", ".join(real_missing)) - return jinja2.Template(task_template).render(**kwargs) + return jinja2.Template(task_template, finalize=finalize_for_yaml).render(**kwargs) def is_really_missing(mis, task_template): # Removing variables that have default values from # missing. Construction that won't be properly # check is {% set x = x or 1} - if re.search(mis.join(["{%\s*set\s+", "\s*=\s*", "[^\w]+"]), + if re.search(mis.join([r"{%\s*set\s+", "\s*=\s*", r"[^\w]+"]), task_template): return False # Also check for a default filter which can show up as # a missing variable - if re.search(mis + "\s*\|\s*default\(", task_template): + if re.search(mis + r"\s*\|\s*default\(", task_template): return False return True