diff --git a/cimgen/languages/modernpython/lang_pack.py b/cimgen/languages/modernpython/lang_pack.py index 64cb410..ce56405 100644 --- a/cimgen/languages/modernpython/lang_pack.py +++ b/cimgen/languages/modernpython/lang_pack.py @@ -98,14 +98,6 @@ def _get_python_type(datatype): return "float" -def _set_imports(attributes): - import_set = set() - for attribute in attributes: - if attribute["is_datatype_attribute"] or attribute["is_primitive_attribute"]: - import_set.add(attribute["attribute_class"]) - return sorted(import_set) - - def _set_datatype_attributes(attributes) -> dict: datatype_attributes = {} datatype_attributes["python_type"] = "None" @@ -136,7 +128,6 @@ def run_template(output_path, class_details): template = class_template_file class_details["setDefault"] = _set_default class_details["setType"] = _set_type - class_details["imports"] = _set_imports(class_details["attributes"]) resource_file = _create_file(output_path, class_details, template) _write_templated_file(resource_file, class_details, template["filename"]) diff --git a/cimgen/languages/modernpython/templates/class_template.mustache b/cimgen/languages/modernpython/templates/class_template.mustache index 18dd114..da6ee72 100644 --- a/cimgen/languages/modernpython/templates/class_template.mustache +++ b/cimgen/languages/modernpython/templates/class_template.mustache @@ -10,9 +10,6 @@ from pydantic.dataclasses import dataclass from ..utils.profile import BaseProfile, Profile from {{class_location}} import {{sub_class_of}} -{{#imports}} -from .{{.}} import {{.}} -{{/imports}} @dataclass @@ -35,17 +32,17 @@ class {{class_name}}({{sub_class_of}}): {{/attr_origin}} ], "is_used": {{#is_used}}True{{/is_used}}{{^is_used}}False{{/is_used}}, - "attribute_namespace": "{{attribute_namespace}}", # NOSONAR + "namespace": "{{attribute_namespace}}", # NOSONAR "is_class_attribute": {{#is_class_attribute}}True{{/is_class_attribute}}{{^is_class_attribute}}False{{/is_class_attribute}}, "is_datatype_attribute": {{#is_datatype_attribute}}True{{/is_datatype_attribute}}{{^is_datatype_attribute}}False{{/is_datatype_attribute}}, "is_enum_attribute": {{#is_enum_attribute}}True{{/is_enum_attribute}}{{^is_enum_attribute}}False{{/is_enum_attribute}}, "is_list_attribute": {{#is_list_attribute}}True{{/is_list_attribute}}{{^is_list_attribute}}False{{/is_list_attribute}}, "is_primitive_attribute": {{#is_primitive_attribute}}True{{/is_primitive_attribute}}{{^is_primitive_attribute}}False{{/is_primitive_attribute}}, {{#is_datatype_attribute}} - "attribute_class": {{attribute_class}}, + "attribute_class": "{{attribute_class}}", {{/is_datatype_attribute}} {{#is_primitive_attribute}} - "attribute_class": {{attribute_class}}, + "attribute_class": "{{attribute_class}}", {{/is_primitive_attribute}} }, ) diff --git a/cimgen/languages/modernpython/utils/base.py b/cimgen/languages/modernpython/utils/base.py index 29f500a..deaf471 100644 --- a/cimgen/languages/modernpython/utils/base.py +++ b/cimgen/languages/modernpython/utils/base.py @@ -144,7 +144,7 @@ def cgmes_attributes_in_profile(self, profile: BaseProfile | None) -> dict[str, # This can go up to Base, which will give the default cim NS. infos["namespace"] = self.namespace extra = getattr(f.default, "json_schema_extra", None) - if extra is not None and extra.get("is_used"): + if extra and extra.get("is_used"): # adding the extras, used for xml generation extra_info = { "attr_name": qualname, @@ -155,9 +155,9 @@ def cgmes_attributes_in_profile(self, profile: BaseProfile | None) -> dict[str, "is_datatype_attribute": extra.get("is_datatype_attribute"), "attribute_class": extra.get("attribute_class"), } - if (extra.get("attribute_namespace", None)) is not None: + if extra.get("namespace"): # The attribute has an explicit namesapce - extra_info["namespace"] = extra.get("attribute_namespace", self.namespace) + extra_info["namespace"] = extra.get("namespace", self.namespace) infos.update(extra_info) infos["value"] = getattr(self, shortname) @@ -219,11 +219,7 @@ def _remove_empty_attributes(attributes: dict) -> dict: # Remove empty attributes if attribute["value"] is None or attribute["value"] == "": del attributes[key] - elif ( - "is_datatype_attribute" in attribute - and attribute["attribute_class"] - and attribute["attribute_class"].name == "Boolean" - ): + elif attribute.get("attribute_class") and attribute["attribute_class"] == "Boolean": attribute["value"] = str(attribute["value"]).lower() return attributes @@ -278,7 +274,7 @@ def _parse_xml_fragment(self, xml_fragment: str) -> dict: xml_attribute = xml_attribute[0] attr = key.rsplit(".")[-1] - attr_value = self._extract_attr_value_from_etree(class_attribute, xml_attribute) + attr_value = self._extract_attr_value_from_etree(attr, class_attribute, xml_attribute) if hasattr(self, attr) and attr_value is not None: attribute_dict[attr] = attr_value @@ -297,7 +293,7 @@ def _extract_mrid_from_etree(self, xml_tree: etree.Element) -> dict: mrid_dict = {"mRID": value} return mrid_dict - def _extract_attr_value_from_etree(self, class_attribute: dict, xml_attribute: etree.Element): + def _extract_attr_value_from_etree(self, attr_name: str, class_attribute: dict, xml_attribute: etree.Element): """Parsing the attribute value from etree attributes""" attr_value = None # class attributes are pointing to another class/instance defined in .attrib @@ -317,10 +313,11 @@ def _extract_attr_value_from_etree(self, class_attribute: dict, xml_attribute: e attr_value = self.key.append(attr_value) elif class_attribute["is_primitive_attribute"] or class_attribute["is_datatype_attribute"]: attr_value = xml_attribute.text - if class_attribute["attribute_class"].type == bool: + if self.__dataclass_fields__[attr_name].type == bool: attr_value = {"true": True, "false": False}.get(attr_value, None) else: - attr_value = class_attribute["attribute_class"].type(attr_value) + # types are int, float or str (date, time and datetime treated as str) + attr_value = self.__dataclass_fields__[attr_name].type(attr_value) else: # other attributes types are defined in .text attr_value = xml_attribute.text