Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 6 additions & 4 deletions chk/infrastructure/symbol_table.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,11 +94,11 @@ def replace_value(doc: dict | list, var_s: dict) -> dict | list:
:param var_s:
:return:
"""
env = JinjaTemplate.build_env()

for key, val in list(doc.items() if isinstance(doc, dict) else enumerate(doc)):
if isinstance(val, str):
str_tpl = JinjaTemplate.make(val)
doc[key] = str_tpl.render(var_s)
doc[key] = JinjaTemplate.render(env, val, var_s)
elif isinstance(val, (dict, list)):
doc[key] = replace_value(doc[key], var_s)
return doc
Expand Down Expand Up @@ -174,8 +174,10 @@ def handle_composite(
replaced_values: dict = replace_callback(
composite_values, variable_doc.data
)
for key, val in replaced_values.items():
variable_doc[key] = val

cls.handle_absolute(variable_doc, replaced_values)
cls.handle_composite(variable_doc, replaced_values)


@classmethod
def handle_execute_context(
Expand Down
34 changes: 34 additions & 0 deletions chk/infrastructure/templating.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

from jinja2 import TemplateError
from jinja2.environment import Template
from jinja2.meta import find_undeclared_variables
from jinja2.nativetypes import NativeEnvironment

from chk.infrastructure.logging import error
Expand All @@ -15,6 +16,22 @@
class JinjaTemplate:
"""JinjaTemplate is wrapper class for JinjaNativeTemplate"""

@staticmethod
def build_env() -> NativeEnvironment:
"""Build a native env"""

env = NativeEnvironment(
variable_start_string="<%",
variable_end_string="%>",
block_start_string="<@",
block_end_string="@>",
comment_start_string="<#",
comment_end_string="#>",
)

env.filters["fromjson"] = filter_fromjson
return env

@staticmethod
def make(template: str) -> Template:
"""Create a NativeEnvironment with default settings"""
Expand All @@ -37,6 +54,23 @@ def make(template: str) -> Template:

return env.from_string(template)

@staticmethod
def render(env: NativeEnvironment, template: str, data: dict) -> typing.Any:
"""Create a NativeEnvironment with default settings"""

if not template or not isinstance(template, str):
e_msg = f"Template error: {type(template)} {template}"
error(e_msg)
raise ValueError(e_msg)

# handle undefined vars
undeclared_vars = find_undeclared_variables(env.parse(template))

if all([_var in data for _var in undeclared_vars]):
return env.from_string(template).render(data)
else:
return template


def is_template_str(tpl: str) -> bool:
"""Check given string is templated string or not"""
Expand Down
10 changes: 7 additions & 3 deletions chk/modules/workflow/services.py
Original file line number Diff line number Diff line change
Expand Up @@ -113,9 +113,13 @@ def _prepare_dump_data(self) -> dict:
response_task_dump["validate_asserts_count_fail"] = (
item.others["count_fail"] if "count_fail" in item.others else ""
)
response_task_dump["validate_asserts_err_messages"] = [
f" >>> {msg}" for msg in item.others["exceptions"]
]

response_task_dump["validate_asserts_err_messages"] = []

if "exceptions" in item.others:
response_task_dump["validate_asserts_err_messages"] = [
f" >>> {msg}" for msg in item.others["exceptions"]
]

r_dump["tasks"].append(response_task_dump)
return r_dump
Expand Down
12 changes: 6 additions & 6 deletions poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 6 additions & 6 deletions requirements-dev.txt
Original file line number Diff line number Diff line change
Expand Up @@ -411,9 +411,9 @@ pygments==2.19.1 ; python_version >= "3.13" and python_version < "4.0" \
pytest-cov==6.0.0 ; python_version >= "3.13" and python_version < "4.0" \
--hash=sha256:eee6f1b9e61008bd34975a4d5bab25801eb31898b032dd55addc93e96fcaaa35 \
--hash=sha256:fde0b595ca248bb8e2d76f020b465f3b107c9632e6a1d1705f17834c89dcadc0
pytest==8.3.4 ; python_version >= "3.13" and python_version < "4.0" \
--hash=sha256:50e16d954148559c9a74109af1eaf0c945ba2d8f30f0a3d3335edde19788b6f6 \
--hash=sha256:965370d062bce11e73868e0335abac31b4d3de0e82f4007408d242b4f8610761
pytest==8.3.5 ; python_version >= "3.13" and python_version < "4.0" \
--hash=sha256:c69214aa47deac29fad6c2a4f590b9c4a9fdb16a403176fe154b79c0b4d4d820 \
--hash=sha256:f4efe70cc14e511565ac476b57c279e12a855b11f48f212af1080ef2263d3845
python-dotenv==1.0.1 ; python_version >= "3.13" and python_version < "4.0" \
--hash=sha256:e324ee90a023d808f1959c46bcbc04446a10ced277783dc6ee09987c37ec10ca \
--hash=sha256:f7b63ef50f1b690dddf550d03497b66d609393b40b564ed0d674909a68ebf16a
Expand Down Expand Up @@ -496,9 +496,9 @@ ruff==0.8.6 ; python_version >= "3.13" and python_version < "4.0" \
--hash=sha256:e169ea1b9eae61c99b257dc83b9ee6c76f89042752cb2d83486a7d6e48e8f764 \
--hash=sha256:e88b8f6d901477c41559ba540beeb5a671e14cd29ebd5683903572f4b40a9807 \
--hash=sha256:f1d70bef3d16fdc897ee290d7d20da3cbe4e26349f62e8a0274e7a3f4ce7a905
setuptools==75.8.0 ; python_version >= "3.13" and python_version < "4.0" \
--hash=sha256:c5afc8f407c626b8313a86e10311dd3f661c6cd9c09d4bf8c15c0e11f9f2b0e6 \
--hash=sha256:e3982f444617239225d675215d51f6ba05f845d4eec313da4418fdbb56fb27e3
setuptools==75.8.2 ; python_version >= "3.13" and python_version < "4.0" \
--hash=sha256:4880473a969e5f23f2a2be3646b2dfd84af9028716d398e46192f84bc36900d2 \
--hash=sha256:558e47c15f1811c1fa7adbd0096669bf76c1d3f433f58324df69f3f5ecac4e8f
shiv==1.0.8 ; python_version >= "3.13" and python_version < "4.0" \
--hash=sha256:2a68d69e98ce81cb5b8fdafbfc1e27efa93e6d89ca14bfae33482e4176f561d6 \
--hash=sha256:a60e4b05a2d2f8b820d567b1d89ee59af731759771c32c282d03c4ceae6aba24
Expand Down
7 changes: 5 additions & 2 deletions tests/infrastructure/symbol_table_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,9 @@ def test_handle_composite_pass():
},
VariableConfigNode.VARIABLES: {
"var_1": "bar",
"var_6": "<% var_5 | trim %> cent",
"var_2": 2,
"var_7": "<% var_6 %> <% var_3 | upper %>",
"var_3": "ajax_<%var_1%>",
"var_5": " <% var_2 %>",
},
Expand All @@ -104,13 +106,14 @@ def test_handle_composite_pass():
variable_doc, file_ctx.document[VariableConfigNode.VARIABLES]
)

assert len(variable_doc.data) == 4
assert len(variable_doc.data) == 6
assert variable_doc.data == {
"var_1": "bar",
"var_2": 2,
"var_3": "ajax_bar",
"var_5": " 2",
}
"var_6": "2 cent",
"var_7": "2 cent AJAX_BAR", }

@staticmethod
def test_handle_execute_context_pass():
Expand Down
Loading