Merge "Yardstick Preliminary Documentation"
[yardstick.git] / yardstick / common / task_template.py
1 ##############################################################################
2 # Copyright (c) 2015 Huawei Technologies Co.,Ltd and others.
3 #
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
11 import re
12 import jinja2
13 import jinja2.meta
14
15
16 class TaskTemplate(object):
17
18     @classmethod
19     def render(cls, task_template, **kwargs):
20         """Render jinja2 task template to Yardstick input task.
21
22         :param task_template: string that contains template
23         :param kwargs: Dict with template arguments
24         :returns:rendered template str
25         """
26
27         from six.moves import builtins
28
29         ast = jinja2.Environment().parse(task_template)
30         required_kwargs = jinja2.meta.find_undeclared_variables(ast)
31
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)]
35
36         if real_missing:
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)
42
43
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]+"]),
49                  task_template):
50         return False
51     # Also check for a default filter which can show up as
52     # a missing variable
53     if re.search(mis + r"\s*\|\s*default\(", task_template):
54         return False
55     return True