Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

XML generation and parsing #43

Open
wants to merge 22 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
211daee
Identify classes and atrributes namespaces
HUG0-D Nov 18, 2024
35d013e
add attribute_namespace to json_extra to handle custom attributes
HUG0-D Nov 18, 2024
d68cad4
Extended function cgmes_attributes_in_profile to get json_extras of t…
HUG0-D Nov 18, 2024
aa73c07
Added function to export the python object to an xml fragment
HUG0-D Nov 18, 2024
8bc3eaf
Added function in base to create a class instance from an etree eleme…
HUG0-D Nov 18, 2024
ef3dd60
update_from_xml allows to update a class instance by parsing addition…
HUG0-D Nov 18, 2024
657b738
Added reader to parse profiles and create python object using the bas…
HUG0-D Nov 19, 2024
502e327
Added writer to export profiles using the .to_xml base function
HUG0-D Nov 19, 2024
f668f2a
Improved readability of Reader
HUG0-D Nov 19, 2024
bae046c
Improved readability of Writer
HUG0-D Nov 19, 2024
a8e078f
Fix sonar issues
HUG0-D Nov 19, 2024
778658a
Fix sonar issues
HUG0-D Nov 20, 2024
362080e
Fix sonar issues
HUG0-D Nov 20, 2024
4f00acd
Refactor functions to_xml and _parse_xml_fragment to split in differe…
HUG0-D Nov 20, 2024
339e5d7
Fix sonar issues
HUG0-D Nov 20, 2024
dabbcf1
Fix comments
HUG0-D Nov 20, 2024
da4ec07
Removed extra indentation levels
HUG0-D Nov 25, 2024
55550f6
Add enum wrapped_comments
HUG0-D Nov 27, 2024
9126975
Fix json_dump error by setting attribute_class as str
HUG0-D Nov 27, 2024
7a095f3
Changed var name for writer
HUG0-D Nov 27, 2024
f531c06
Added subfolder within resources to store primitives, enum and datatypes
HUG0-D Dec 16, 2024
275da4c
Added function to get main profile of an attribute (useful in writer)
HUG0-D Dec 16, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions cimgen/cimgen.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ def asJson(self):
jsonObject = {}
if self.about() is not None:
jsonObject["about"] = self.about()
if self.namespace() is not None:
jsonObject["namespace"] = self.namespace()
if self.comment() is not None:
jsonObject["comment"] = self.comment()
if self.dataType() is not None:
Expand Down Expand Up @@ -51,6 +53,12 @@ def about(self):
else:
return None

def namespace(self):
if "$rdf:about" in self.jsonDefinition:
return self.jsonDefinition["$rdf:about"][: -len(self.about())]
else:
return None

# Capitalized True/False is valid in python but not in json.
# Do not use this function in combination with json.load()
def is_used(self) -> bool:
Expand Down Expand Up @@ -211,6 +219,7 @@ def __init__(self, rdfsEntry):
self.super = rdfsEntry.subClassOf()
self.subclasses = []
self.stereotype = rdfsEntry.stereotype()
self.namespace = rdfsEntry.namespace()

def attributes(self):
return self.attribute_list
Expand Down Expand Up @@ -405,6 +414,13 @@ def _parse_rdf(input_dic, version): # NOSONAR
for instance in enum_instances:
clarse = _get_rid_of_hash(instance["type"])
if clarse and clarse in classes_map:
if instance.get("comment"):
instance["wrapped_comment"] = wrap_and_clean(
instance["comment"],
width=100,
initial_indent="# ",
subsequent_indent=(" # "),
)
classes_map[clarse].add_enum_instance(instance)
else:
logger.info("Class {} for enum instance {} not found.".format(clarse, instance))
Expand All @@ -429,6 +445,7 @@ def _write_python_files(elem_dict, lang_pack, output_path, version):
"class_location": lang_pack.get_class_location(class_name, elem_dict, version),
"class_name": class_name,
"class_origin": elem_dict[class_name].origins(),
"class_namespace": _get_namespace(elem_dict[class_name].namespace),
"enum_instances": elem_dict[class_name].enum_instances(),
"is_an_enum_class": elem_dict[class_name].is_an_enum_class(),
"is_a_primitive_class": elem_dict[class_name].is_a_primitive_class(),
Expand Down Expand Up @@ -467,6 +484,7 @@ def _write_python_files(elem_dict, lang_pack, output_path, version):
attribute["is_primitive_attribute"] = _get_bool_string(attribute_type == "primitive")
attribute["is_datatype_attribute"] = _get_bool_string(attribute_type == "datatype")
attribute["attribute_class"] = attribute_class
attribute["attribute_namespace"] = _get_namespace(attribute["namespace"])

class_details["attributes"].sort(key=lambda d: d["label"])
_write_files(class_details, output_path, version)
Expand Down Expand Up @@ -749,6 +767,14 @@ def _get_attribute_type(attribute: dict, class_infos: CIMComponentDefinition) ->
return attribute_type


def _get_namespace(parsed_namespace: str) -> str:
if parsed_namespace == "#":
namespace = cim_namespace
else:
namespace = parsed_namespace
return namespace


def _get_bool_string(bool_value: bool) -> str:
"""Convert boolean value into a string which is usable in both Python and Json.

Expand Down
19 changes: 9 additions & 10 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,13 +128,20 @@ 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"])


def _create_file(output_path, class_details, template) -> str:
resource_file = Path(output_path) / "resources" / (class_details["class_name"] + template["ext"])
if (
class_details["is_a_primitive_class"]
or class_details["is_a_datatype_class"]
or class_details["is_an_enum_class"]
):
class_category = "types"
else:
class_category = ""
resource_file = Path(output_path) / "resources" / class_category / (class_details["class_name"] + template["ext"])
resource_file.parent.mkdir(exist_ok=True)
return str(resource_file)

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,16 +32,17 @@ class {{class_name}}({{sub_class_of}}):
{{/attr_origin}}
],
"is_used": {{#is_used}}True{{/is_used}}{{^is_used}}False{{/is_used}},
"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
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
Generated from the CGMES files via cimgen: https://github.com/sogno-platform/cimgen
"""

from ..utils.datatypes import CIMDatatype
from ..utils.profile import Profile
from ...utils.datatypes import CIMDatatype
from ...utils.profile import Profile
{{#isFixed_imports}}
from .{{.}} import {{.}}
{{/isFixed_imports}}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,10 @@ from enum import Enum

class {{class_name}}(str, Enum):
"""
{{{class_comment}}} # noqa: E501
{{{wrapped_class_comment}}}
"""

{{#enum_instances}}
{{label}} = "{{label}}"{{#comment}} # {{comment}}{{/comment}} # noqa: E501
{{label}} = "{{label}}"{{#comment}}
{{wrapped_comment}}{{/comment}}
{{/enum_instances}}
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ Generated from the CGMES files via cimgen: https://github.com/sogno-platform/cim
"""

from datetime import date, datetime, time
from ..utils.datatypes import Primitive
from ..utils.profile import Profile
from ...utils.datatypes import Primitive
from ...utils.profile import Profile

{{class_name}} = Primitive(
name="{{class_name}}",
Expand Down
Loading
Loading