From c26b74f4722f1f4613346ee9c03f15f950d69d15 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Thomas=20G=C3=BCnther?= Date: Mon, 23 Dec 2024 18:30:05 +0100 Subject: [PATCH] Simplified template file handling for java, javascript and python MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Thomas Günther --- cimgen/languages/java/lang_pack.py | 21 ++++++++--------- cimgen/languages/javascript/lang_pack.py | 29 +++++++++++------------- cimgen/languages/python/lang_pack.py | 22 ++++++++---------- 3 files changed, 33 insertions(+), 39 deletions(-) diff --git a/cimgen/languages/java/lang_pack.py b/cimgen/languages/java/lang_pack.py index 3c5684e..a0f7566 100644 --- a/cimgen/languages/java/lang_pack.py +++ b/cimgen/languages/java/lang_pack.py @@ -26,10 +26,10 @@ def setup(output_path: str, cgmes_profile_details: list[dict], cim_namespace: st # There is a template set for the large number of classes that are floats. They # have unit, multiplier and value attributes in the schema, but only appear in # the file as a float string. -template_files = [{"filename": "java_class.mustache", "ext": ".java"}] -float_template_files = [{"filename": "java_float.mustache", "ext": ".java"}] -enum_template_files = [{"filename": "java_enum.mustache", "ext": ".java"}] -string_template_files = [{"filename": "java_string.mustache", "ext": ".java"}] +class_template_file = {"filename": "java_class.mustache", "ext": ".java"} +float_template_file = {"filename": "java_float.mustache", "ext": ".java"} +enum_template_file = {"filename": "java_enum.mustache", "ext": ".java"} +string_template_file = {"filename": "java_string.mustache", "ext": ".java"} partials = { "label": "{{#lang_pack.label}}{{label}}{{/lang_pack.label}}", @@ -48,22 +48,21 @@ def get_class_location(class_name: str, class_map: dict, version: str) -> str: def run_template(output_path: str, class_details: dict) -> None: if class_details["is_a_datatype_class"] or class_details["class_name"] in ("Float", "Decimal"): - templates = float_template_files + template = float_template_file elif class_details["is_an_enum_class"]: - templates = enum_template_files + template = enum_template_file elif class_details["is_a_primitive_class"]: - templates = string_template_files + template = string_template_file else: - templates = template_files + template = class_template_file if class_details["class_name"] in ("Integer", "Boolean"): # These classes are defined already # We have to implement operators for them return - for template_info in templates: - class_file = Path(output_path) / (class_details["class_name"] + template_info["ext"]) - _write_templated_file(class_file, class_details, template_info["filename"]) + class_file = Path(output_path) / (class_details["class_name"] + template["ext"]) + _write_templated_file(class_file, class_details, template["filename"]) def _write_templated_file(class_file: Path, class_details: dict, template_filename: str) -> None: diff --git a/cimgen/languages/javascript/lang_pack.py b/cimgen/languages/javascript/lang_pack.py index 41a829e..3577a50 100644 --- a/cimgen/languages/javascript/lang_pack.py +++ b/cimgen/languages/javascript/lang_pack.py @@ -19,9 +19,9 @@ def setup(output_path: str, cgmes_profile_details: list[dict], cim_namespace: st # These are the files that are used to generate the javascript files. -template_files = [{"filename": "handlebars_template.mustache", "ext": ".js"}] -base_template_files = [{"filename": "handlebars_baseclass_template.mustache", "ext": ".js"}] -profile_template_files = [{"filename": "handlebars_cgmesProfile_template.mustache", "ext": ".js"}] +template_file = {"filename": "handlebars_template.mustache", "ext": ".js"} +base_template_file = {"filename": "handlebars_baseclass_template.mustache", "ext": ".js"} +profile_template_file = {"filename": "handlebars_cgmesProfile_template.mustache", "ext": ".js"} partials = {} @@ -118,9 +118,8 @@ def run_template(output_path: str, class_details: dict) -> None: sys.exit(1) class_details["renderAttribute"] = renderAttribute - for template_info in template_files: - class_file = os.path.join(output_path, class_details["class_name"] + template_info["ext"]) - _write_templated_file(class_file, class_details, template_info["filename"]) + class_file = os.path.join(output_path, class_details["class_name"] + template_file["ext"]) + _write_templated_file(class_file, class_details, template_file["filename"]) def _write_templated_file(class_file: str, class_details: dict, template_filename: str) -> None: @@ -138,19 +137,17 @@ def _write_templated_file(class_file: str, class_details: dict, template_filenam # creates the Base class file, all classes inherit from this class def _create_base(output_path: str) -> None: - for template_info in base_template_files: - class_file = os.path.join(output_path, "BaseClass" + template_info["ext"]) - _write_templated_file(class_file, {}, template_info["filename"]) + class_file = os.path.join(output_path, "BaseClass" + base_template_file["ext"]) + _write_templated_file(class_file, {}, base_template_file["filename"]) def _create_cgmes_profile(output_path: str, profile_details: list[dict], cim_namespace: str) -> None: - for template_info in profile_template_files: - class_file = os.path.join(output_path, "CGMESProfile" + template_info["ext"]) - class_details = { - "profiles": profile_details, - "cim_namespace": cim_namespace, - } - _write_templated_file(class_file, class_details, template_info["filename"]) + class_file = os.path.join(output_path, "CGMESProfile" + profile_template_file["ext"]) + class_details = { + "profiles": profile_details, + "cim_namespace": cim_namespace, + } + _write_templated_file(class_file, class_details, profile_template_file["filename"]) def _get_class_type(class_details: dict) -> str: diff --git a/cimgen/languages/python/lang_pack.py b/cimgen/languages/python/lang_pack.py index ff565e8..bb69219 100644 --- a/cimgen/languages/python/lang_pack.py +++ b/cimgen/languages/python/lang_pack.py @@ -24,8 +24,8 @@ def setup(output_path: str, cgmes_profile_details: list[dict], cim_namespace: st # These are the files that are used to generate the python files. -template_files = [{"filename": "cimpy_class_template.mustache", "ext": ".py"}] -profile_template_files = [{"filename": "cimpy_cgmesProfile_template.mustache", "ext": ".py"}] +class_template_file = {"filename": "cimpy_class_template.mustache", "ext": ".py"} +profile_template_file = {"filename": "cimpy_cgmesProfile_template.mustache", "ext": ".py"} partials = {} @@ -70,9 +70,8 @@ def get_class_location(class_name: str, class_map: dict, version: str) -> str: def run_template(output_path: str, class_details: dict) -> None: if class_details["class_name"] == "String": return - for template_info in template_files: - class_file = os.path.join(output_path, class_details["class_name"] + template_info["ext"]) - _write_templated_file(class_file, class_details, template_info["filename"]) + class_file = os.path.join(output_path, class_details["class_name"] + class_template_file["ext"]) + _write_templated_file(class_file, class_details, class_template_file["filename"]) def _write_templated_file(class_file: str, class_details: dict, template_filename: str) -> None: @@ -106,13 +105,12 @@ def _create_base(path: str) -> None: def _create_cgmes_profile(output_path: str, profile_details: list[dict], cim_namespace: str) -> None: - for template_info in profile_template_files: - class_file = os.path.join(output_path, "CGMESProfile" + template_info["ext"]) - class_details = { - "profiles": profile_details, - "cim_namespace": cim_namespace, - } - _write_templated_file(class_file, class_details, template_info["filename"]) + class_file = os.path.join(output_path, "CGMESProfile" + profile_template_file["ext"]) + class_details = { + "profiles": profile_details, + "cim_namespace": cim_namespace, + } + _write_templated_file(class_file, class_details, profile_template_file["filename"]) class_blacklist = ["CGMESProfile"]