Skip to content

Commit

Permalink
Simplified template file handling for java, javascript and python
Browse files Browse the repository at this point in the history
Signed-off-by: Thomas Günther <[email protected]>
  • Loading branch information
tom-hg57 committed Dec 26, 2024
1 parent 7d6f393 commit c26b74f
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 39 deletions.
21 changes: 10 additions & 11 deletions cimgen/languages/java/lang_pack.py
Original file line number Diff line number Diff line change
Expand Up @@ -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}}",
Expand All @@ -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:
Expand Down
29 changes: 13 additions & 16 deletions cimgen/languages/javascript/lang_pack.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {}

Expand Down Expand Up @@ -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:
Expand All @@ -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:
Expand Down
22 changes: 10 additions & 12 deletions cimgen/languages/python/lang_pack.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {}

Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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"]
Expand Down

0 comments on commit c26b74f

Please sign in to comment.