Skip to content

Commit

Permalink
Fix json_dump error by setting attribute_class as str
Browse files Browse the repository at this point in the history
Fix attribute_namespace to namespace to be coherant with pycgmes

Signed-off-by: HUG0-D <[email protected]>
  • Loading branch information
HUG0-D committed Nov 27, 2024
1 parent 55550f6 commit 9126975
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 27 deletions.
9 changes: 0 additions & 9 deletions cimgen/languages/modernpython/lang_pack.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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"])

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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}}
},
)
Expand Down
21 changes: 9 additions & 12 deletions cimgen/languages/modernpython/utils/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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)
Expand Down Expand Up @@ -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

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

Expand All @@ -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
Expand All @@ -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
Expand Down

0 comments on commit 9126975

Please sign in to comment.