YAML is a data serialization language, not a data processing language. Templating capabilities are beyond its scope (although there exist a few optional features in that direction, which are not applicable here).
Postprocessing with other templating languages has already been proposed. I'd like to add the suggestion to use pystache because it is a minimal language which might be more appropriate here than using Jinja.
Furthermore, I'd suggest to put the value spec into a map. And since you already use {{
for something else in the string, I'd suggest to tell pystache to use another means of escaping. Example:
tests:
- is-gt: {val: 2}
info: "Test Succeeded!!, val is greater than `val`, it is <{{post['val']}}>"
err: "Test Failed!!, val is not greater than `val`, it is <{{post['val']}}>"
You can now parse it like this:
import yaml, pystache
raw = yaml.safe_load("""
tests:
- is-gt: {val: 2}
info: "Test Succeeded!!, val is greater than `val`, it is <{{post['val']}}>"
err: "Test Failed!!, val is not greater than `val`, it is <{{post['val']}}>"
""")
def dynString(inputs, template):
return pystache.render("{{=` `=}}" + template, inputs)
tests = []
for rawtest in raw["tests"]:
test = {}
inputs = rawtest["is-gt"]
for msg in ["info", "err"]:
test[msg] = dynString(inputs, rawtest[msg])
tests.append(test)
print(tests)
Which outputs:
[{'info': "Test Succeeded!!, val is greater than 2, it is <{{post['val']}}>",
'err': "Test Failed!!, val is not greater than 2, it is <{{post['val']}}>"}]