1 ##############################################################################
2 # Copyright (c) 2015 Huawei Technologies Co.,Ltd and others.
4 # All rights reserved. This program and the accompanying materials
5 # are made available under the terms of the Apache License, Version 2.0
6 # which accompanies this distribution, and is available at
7 # http://www.apache.org/licenses/LICENSE-2.0
8 # yardstick: this file is copied from rally and slightly modified
9 ##############################################################################
10 from __future__ import absolute_import
16 class TaskTemplate(object):
19 def render(cls, task_template, **kwargs):
20 """Render jinja2 task template to Yardstick input task.
22 :param task_template: string that contains template
23 :param kwargs: Dict with template arguments
24 :returns:rendered template str
27 from six.moves import builtins
29 ast = jinja2.Environment().parse(task_template)
30 required_kwargs = jinja2.meta.find_undeclared_variables(ast)
32 missing = set(required_kwargs) - set(kwargs) - set(dir(builtins))
33 real_missing = [mis for mis in missing
34 if is_really_missing(mis, task_template)]
37 multi_msg = ("Please specify next template task arguments:%s")
38 single_msg = ("Please specify template task argument:%s")
39 raise TypeError((len(real_missing) > 1 and multi_msg or single_msg)
40 % ", ".join(real_missing))
41 return jinja2.Template(task_template).render(**kwargs)
44 def is_really_missing(mis, task_template):
45 # Removing variables that have default values from
46 # missing. Construction that won't be properly
47 # check is {% set x = x or 1}
48 if re.search(mis.join([r"{%\s*set\s+", "\s*=\s*", r"[^\w]+"]),
51 # Also check for a default filter which can show up as
53 if re.search(mis + r"\s*\|\s*default\(", task_template):