From 7ce9d50c190d3fb51753b8b41a9fce5e3ee5368a Mon Sep 17 00:00:00 2001 From: "Maksim Bushmanov (19531436)" Date: Fri, 26 Nov 2021 18:18:02 +0300 Subject: [PATCH 1/2] Feat(core): Implement file templates to UnifiedTemplate --- core/unified_template/unified_template.py | 11 ++++++++++- smart_kit/configs/__init__.py | 2 ++ 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/core/unified_template/unified_template.py b/core/unified_template/unified_template.py index 1832ba03..e5c843fc 100644 --- a/core/unified_template/unified_template.py +++ b/core/unified_template/unified_template.py @@ -32,7 +32,16 @@ def __init__(self, input): elif isinstance(input, dict): if input.get("type") != UNIFIED_TEMPLATE_TYPE_NAME: raise Exception("template must be string or dict with type='{}'".format(UNIFIED_TEMPLATE_TYPE_NAME)) - self.template = jinja2.Template(input["template"], extensions=input.get("extensions", ())) + if input.get("file"): + from smart_kit.configs import get_app_config + app_config = get_app_config() + template_loader = FileSystemLoader(app_config.JINJA2_TEMPLATES_PATH) + file_name = input["file"] + self.jinja2_environment = Environment(loader=template_loader, trim_blocks=True, lstrip_blocks=True) + self.template = self.jinja2_environment.get_template(file_name) + log("UnifiedTemplateLoader: File, file_name: %(file_name)s", params={"file_name": file_name}) + else: + self.template = jinja2.Template(input["template"], extensions=input.get("extensions", ())) self.loader = UnifiedTemplate.loaders[input.get("loader", "str")] self.support_templates = {k: UnifiedTemplate(t) for k, t in input.get("support_templates", dict()).items()} else: diff --git a/smart_kit/configs/__init__.py b/smart_kit/configs/__init__.py index eda9ca44..82116fb0 100644 --- a/smart_kit/configs/__init__.py +++ b/smart_kit/configs/__init__.py @@ -40,6 +40,8 @@ def get_app_config(environment_variable=ENVIRONMENT_VARIABLE): set_default(app_config, "SECRET_PATH", os.path.join(static_path, "./configs")) references_path = os.path.join(static_path, "./references") set_default(app_config, "REFERENCES_PATH", references_path) + jinja2_templates_path = os.path.join(references_path, "./templates") + set_default(app_config, "JINJA2_TEMPLATES_PATH", jinja2_templates_path) set_default(app_config, "LOCAL_TESTING", CLInterface) set_default(app_config, "TEST_CASE", TestCase) From c50c4e0f515d1f40540e2f0530e582989f154e50 Mon Sep 17 00:00:00 2001 From: "Maksim Bushmanov (19531436)" Date: Mon, 6 Dec 2021 13:51:12 +0300 Subject: [PATCH 2/2] Fix(core): fix FileSystemLoader and Environment calls --- core/unified_template/unified_template.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/core/unified_template/unified_template.py b/core/unified_template/unified_template.py index e5c843fc..fb353392 100644 --- a/core/unified_template/unified_template.py +++ b/core/unified_template/unified_template.py @@ -35,9 +35,11 @@ def __init__(self, input): if input.get("file"): from smart_kit.configs import get_app_config app_config = get_app_config() - template_loader = FileSystemLoader(app_config.JINJA2_TEMPLATES_PATH) + template_loader = jinja2.FileSystemLoader(app_config.JINJA2_TEMPLATES_PATH) file_name = input["file"] - self.jinja2_environment = Environment(loader=template_loader, trim_blocks=True, lstrip_blocks=True) + self.jinja2_environment = jinja2.Environment( + loader=template_loader, trim_blocks=True, lstrip_blocks=True + ) self.template = self.jinja2_environment.get_template(file_name) log("UnifiedTemplateLoader: File, file_name: %(file_name)s", params={"file_name": file_name}) else: