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
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Improved readability of Writer
Added ability to add attributes to profile Model header

Signed-off-by: HUG0-D <hugo.delesalle@zaphiro.ch>
  • Loading branch information
HUG0-D committed Nov 25, 2024
commit bae046cb80eed01dcf7da2d66ab57bf7e150bda7
36 changes: 24 additions & 12 deletions cimgen/languages/modernpython/utils/writer.py
Original file line number Diff line number Diff line change
@@ -1,22 +1,28 @@
from lxml import etree
from .base import Base
from pydantic import BaseModel
from .constants import NAMESPACES
from .profile import BaseProfile, Profile
from typing import Optional
from typing import Dict, Optional


class Writer:
"""Class for writing CIM RDF/XML files."""
class Writer(BaseModel):
"""Class for writing CIM RDF/XML files

def __init__(self, objects: dict[str, Base]):
"""Constructor.
Args:
objects (dict): Mapping of rdfid to CIM object
Model_metadata (Optional[Dict[str, str]]): any additional data to add in header
default = {"modelingAuthoritySet": "www.sogno.energy" }
"""

:param objects: Mapping of rdfid to CIM object.
"""
self.objects = objects
objects: Dict
Model_metadata: Dict[str, str] = {}

def write(
self, outputfile: str, model_id: str, class_profile_map: dict[str, BaseProfile], custom_namespaces: dict
self,
outputfile: str,
model_id: str,
class_profile_map: Dict[str, BaseProfile],
custom_namespaces: Dict = {},
) -> dict[BaseProfile, str]:
"""Write CIM RDF/XML files.

@@ -28,6 +34,7 @@ def write(
:param outputfile: Stem of the output file, resulting files: <outputfile>_<profile.long_name>.xml.
:param model_id: Stem of the model IDs, resulting IDs: <model_id>_<profile.long_name>.
:param class_profile_map: Mapping of CIM type to profile.
:param custom_namespaces: Optional[Dict[str, str]]: {"namespace_prefix": "namespace_uri"}
:return: Mapping of profile to outputfile.
"""
profile_list: list[BaseProfile] = list(Profile)
@@ -42,18 +49,23 @@ def write(
profile_file_map[profile] = full_file_name
return profile_file_map

def _generate(self, profile: BaseProfile, model_id: str, custom_namespaces) -> Optional[etree.ElementTree]:
def _generate(
self, profile: BaseProfile, model_id: str, custom_namespaces: Dict = {}
) -> Optional[etree.ElementTree]:
"""Write CIM objects as RDF/XML data to a string.

This function creates RDF/XML tree corresponding to one profile.

:param profile: Only data for this profile should be written.
:param model_id: Stem of the model IDs, resulting IDs: <modelID>_<profileName>.
:param custom_namespaces: Optional[Dict[str, str]]: {"namespace_prefix": "namespace_uri"}
:return: etree of the profile
"""
Model = {"modelingAuthoritySet": "www.sogno.energy"}
Model.update(self.Model_metadata)
FullModel = {
"id": model_id,
"Model": {"modelingAuthoritySet": "www.sogno.energy"},
"Model": Model,
}
for uri in profile.uris:
FullModel["Model"].update({"profile": uri})