Merge "Allow for default() filters for jinja variables"
This commit is contained in:
commit
1c761cfbb4
23
rally/api.py
23
rally/api.py
@ -112,6 +112,19 @@ class Task(object):
|
|||||||
:returns: rendered template str
|
:returns: rendered template str
|
||||||
"""
|
"""
|
||||||
|
|
||||||
|
def is_really_missing(mis, task_template):
|
||||||
|
# NOTE(boris-42): Removing variables that have default values from
|
||||||
|
# missing. Construction that won't be properly
|
||||||
|
# checked is {% set x = x or 1}
|
||||||
|
if re.search(mis.join(["{%\s*set\s+", "\s*=\s*", "[^\w]+"]),
|
||||||
|
task_template):
|
||||||
|
return False
|
||||||
|
# NOTE(jlk): Also check for a default filter which can show up as
|
||||||
|
# a missing variable
|
||||||
|
if re.search(mis + "\s*\|\s*default\(", task_template):
|
||||||
|
return False
|
||||||
|
return True
|
||||||
|
|
||||||
# NOTE(boris-42): We have to import builtins to get the full list of
|
# NOTE(boris-42): We have to import builtins to get the full list of
|
||||||
# builtin functions (e.g. range()). Unfortunately,
|
# builtin functions (e.g. range()). Unfortunately,
|
||||||
# __builtins__ doesn't return them (when it is not
|
# __builtins__ doesn't return them (when it is not
|
||||||
@ -122,14 +135,8 @@ class Task(object):
|
|||||||
required_kwargs = jinja2.meta.find_undeclared_variables(ast)
|
required_kwargs = jinja2.meta.find_undeclared_variables(ast)
|
||||||
|
|
||||||
missing = set(required_kwargs) - set(kwargs) - set(dir(builtins))
|
missing = set(required_kwargs) - set(kwargs) - set(dir(builtins))
|
||||||
# NOTE(boris-42): Removing variables that have default values from
|
real_missing = [mis for mis in missing
|
||||||
# missing. Construction that won't be properly checked
|
if is_really_missing(mis, task_template)]
|
||||||
# is {% set x = x or 1}
|
|
||||||
real_missing = []
|
|
||||||
for mis in missing:
|
|
||||||
if not re.search(mis.join(["{%\s*set\s+", "\s*=\s*", "[^\w]+"]),
|
|
||||||
task_template):
|
|
||||||
real_missing.append(mis)
|
|
||||||
|
|
||||||
if real_missing:
|
if real_missing:
|
||||||
multi_msg = _("Please specify next template task arguments: %s")
|
multi_msg = _("Please specify next template task arguments: %s")
|
||||||
|
@ -87,6 +87,13 @@ class TaskAPITestCase(test.TestCase):
|
|||||||
self.assertEqual(
|
self.assertEqual(
|
||||||
"5 = 5", api.Task.render_template(template, a=2, b=3, c=5))
|
"5 = 5", api.Task.render_template(template, a=2, b=3, c=5))
|
||||||
|
|
||||||
|
def test_render_template_default_filter(self):
|
||||||
|
template = "{{ c | default(3) }}"
|
||||||
|
|
||||||
|
self.assertEqual("3", api.Task.render_template(template))
|
||||||
|
|
||||||
|
self.assertEqual("5", api.Task.render_template(template, c=5))
|
||||||
|
|
||||||
def test_render_template_builtin(self):
|
def test_render_template_builtin(self):
|
||||||
template = "{% for i in range(4) %}{{i}}{% endfor %}"
|
template = "{% for i in range(4) %}{{i}}{% endfor %}"
|
||||||
self.assertEqual("0123", api.Task.render_template(template))
|
self.assertEqual("0123", api.Task.render_template(template))
|
||||||
|
Loading…
Reference in New Issue
Block a user