generated from Hochfrequenz/python_template_repository
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
generate sgh files from the nested nachrichtenstrukturjson files (#83)
* introduce segment group hierarchies * linter issues * Added unittests for sgh * Update src/migmose/mig/segmentgrouphierarchies.py Co-authored-by: konstantin <konstantin.klein@hochfrequenz.de> * Update src/migmose/mig/segmentgrouphierarchies.py Co-authored-by: konstantin <konstantin.klein@hochfrequenz.de> * add reference to maus segmentgrouphierarchy class * modified unittest * linter * modified to_json for segementgrouphierarchy --------- Co-authored-by: konstantin <konstantin.klein@hochfrequenz.de>
- Loading branch information
1 parent
ef19d55
commit 44b529c
Showing
6 changed files
with
280 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
""" | ||
contains class for trees consisting of segments of mig tables | ||
""" | ||
|
||
import json | ||
from pathlib import Path | ||
from typing import Optional | ||
|
||
from loguru import logger | ||
from maus.edifact import EdifactFormat | ||
from pydantic import BaseModel, Field | ||
|
||
from migmose.mig.reducednestednachrichtenstruktur import ReducedNestedNachrichtenstruktur | ||
|
||
|
||
class SegmentGroupHierarchy(BaseModel): | ||
""" | ||
This module contains the same model for a segment group hierarchy as used by the MAUS library, | ||
which can be found here: | ||
(https://github.com/Hochfrequenz/mig_ahb_utility_stack/blob/eae2d84d600f42257cf364190da486aa011a5882/src/maus/models/message_implementation_guide.py#L17). # pylint: disable=line-too-long | ||
However, instead of using the attrs class, we are creating a Pydantic class. | ||
""" | ||
|
||
opening_segment: Optional[str] = None | ||
segment_group: Optional[str] = None | ||
sub_hierarchy: list[Optional["SegmentGroupHierarchy"]] = Field(default_factory=list) | ||
|
||
@classmethod | ||
def create_segmentgroup_hierarchy( | ||
cls, reduced_nested_nachrichtenstruktur: ReducedNestedNachrichtenstruktur | ||
) -> "SegmentGroupHierarchy": | ||
"""init Segmentrgroup Hierarchy""" | ||
segment_group: Optional[str] = None | ||
opening_segment: Optional[str] = None | ||
if reduced_nested_nachrichtenstruktur.header_linie is not None: | ||
segment_group = reduced_nested_nachrichtenstruktur.header_linie.bezeichnung | ||
if ( | ||
any(reduced_nested_nachrichtenstruktur.segmente) | ||
and reduced_nested_nachrichtenstruktur.segmente[0] is not None | ||
): | ||
opening_segment = reduced_nested_nachrichtenstruktur.segmente[0].bezeichnung | ||
sub_hierarchy: list[Optional["SegmentGroupHierarchy"]] = [] | ||
for sub_segmentgroup in reduced_nested_nachrichtenstruktur.segmentgruppen: | ||
if sub_segmentgroup is not None: | ||
sub_hierarchy.append(cls.create_segmentgroup_hierarchy(sub_segmentgroup)) | ||
|
||
return SegmentGroupHierarchy( | ||
segment_group=segment_group, opening_segment=opening_segment, sub_hierarchy=sub_hierarchy | ||
) | ||
|
||
def to_json(self, message_type: EdifactFormat, output_dir: Path) -> None: | ||
""" | ||
writes the reduced NestedNachrichtenstruktur as json | ||
""" | ||
output_dir.mkdir(parents=True, exist_ok=True) | ||
file_path = output_dir.joinpath("sgh.json") | ||
structured_json = self.model_dump(mode="json") | ||
with open(file_path, "w", encoding="utf-8") as json_file: | ||
json.dump(structured_json, json_file, indent=4) | ||
logger.info("Wrote segmentgroup hierarchy (sgh.json) for {} to {}", message_type, file_path) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
{ | ||
"opening_segment": "UNH", | ||
"segment_group": null, | ||
"sub_hierarchy": [ | ||
{ | ||
"opening_segment": "NAD", | ||
"segment_group": "SG1", | ||
"sub_hierarchy": [ | ||
{ | ||
"opening_segment": "CTA", | ||
"segment_group": "SG2", | ||
"sub_hierarchy": [] | ||
} | ||
] | ||
}, | ||
{ | ||
"opening_segment": "EQD", | ||
"segment_group": "SG4", | ||
"sub_hierarchy": [ | ||
{ | ||
"opening_segment": "LOC", | ||
"segment_group": "SG6", | ||
"sub_hierarchy": [] | ||
}, | ||
{ | ||
"opening_segment": "STS", | ||
"segment_group": "SG7", | ||
"sub_hierarchy": [] | ||
} | ||
] | ||
}, | ||
{ | ||
"opening_segment": "CNI", | ||
"segment_group": "SG14", | ||
"sub_hierarchy": [ | ||
{ | ||
"opening_segment": "STS", | ||
"segment_group": "SG15", | ||
"sub_hierarchy": [ | ||
{ | ||
"opening_segment": "EFI", | ||
"segment_group": "SG16", | ||
"sub_hierarchy": [] | ||
}, | ||
{ | ||
"opening_segment": "NAD", | ||
"segment_group": "SG17", | ||
"sub_hierarchy": [ | ||
{ | ||
"opening_segment": "CTA", | ||
"segment_group": "SG18", | ||
"sub_hierarchy": [] | ||
} | ||
] | ||
}, | ||
{ | ||
"opening_segment": "GID", | ||
"segment_group": "SG25", | ||
"sub_hierarchy": [] | ||
} | ||
] | ||
} | ||
] | ||
} | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
{ | ||
"opening_segment": "UNH", | ||
"segment_group": null, | ||
"sub_hierarchy": [ | ||
{ | ||
"opening_segment": "RFF", | ||
"segment_group": "SG1", | ||
"sub_hierarchy": [] | ||
}, | ||
{ | ||
"opening_segment": "NAD", | ||
"segment_group": "SG3", | ||
"sub_hierarchy": [ | ||
{ | ||
"opening_segment": "CTA", | ||
"segment_group": "SG6", | ||
"sub_hierarchy": [] | ||
} | ||
] | ||
} | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
{ | ||
"opening_segment": "UNH", | ||
"segment_group": null, | ||
"sub_hierarchy": [ | ||
{ | ||
"opening_segment": "RFF", | ||
"segment_group": "SG1", | ||
"sub_hierarchy": [] | ||
}, | ||
{ | ||
"opening_segment": "NAD", | ||
"segment_group": "SG2", | ||
"sub_hierarchy": [ | ||
{ | ||
"opening_segment": "CTA", | ||
"segment_group": "SG3", | ||
"sub_hierarchy": [] | ||
} | ||
] | ||
}, | ||
{ | ||
"opening_segment": "IDE", | ||
"segment_group": "SG4", | ||
"sub_hierarchy": [ | ||
{ | ||
"opening_segment": "LOC", | ||
"segment_group": "SG5", | ||
"sub_hierarchy": [] | ||
}, | ||
{ | ||
"opening_segment": "RFF", | ||
"segment_group": "SG6", | ||
"sub_hierarchy": [] | ||
}, | ||
{ | ||
"opening_segment": "SEQ", | ||
"segment_group": "SG8", | ||
"sub_hierarchy": [ | ||
{ | ||
"opening_segment": "QTY", | ||
"segment_group": "SG9", | ||
"sub_hierarchy": [] | ||
}, | ||
{ | ||
"opening_segment": "CCI", | ||
"segment_group": "SG10", | ||
"sub_hierarchy": [] | ||
} | ||
] | ||
}, | ||
{ | ||
"opening_segment": "NAD", | ||
"segment_group": "SG12", | ||
"sub_hierarchy": [] | ||
} | ||
] | ||
} | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
import json | ||
|
||
import pytest | ||
from maus.edifact import EdifactFormat, EdifactFormatVersion | ||
from maus.models.message_implementation_guide import SegmentGroupHierarchy as MausSGH | ||
from maus.models.message_implementation_guide import SegmentGroupHierarchySchema | ||
|
||
from migmose.mig.nachrichtenstrukturtabelle import NachrichtenstrukturTabelle | ||
from migmose.mig.nestednachrichtenstruktur import NestedNachrichtenstruktur | ||
from migmose.mig.reducednestednachrichtenstruktur import ReducedNestedNachrichtenstruktur | ||
from migmose.mig.segmentgrouphierarchies import SegmentGroupHierarchy | ||
from migmose.parsing import find_file_to_format, parse_raw_nachrichtenstrukturzeile | ||
from unittests import expected_output_dir, path_to_test_edi_energy_mirror_repo | ||
|
||
|
||
class TestSegmentGroupHierarchy: | ||
"""test class for create_reduced_nested_nachrichtenstruktur""" | ||
|
||
@pytest.mark.parametrize( | ||
"message_format", | ||
[ | ||
pytest.param(EdifactFormat.ORDCHG, id="ORDCHG"), | ||
pytest.param(EdifactFormat.UTILMD, id="UTILMD"), | ||
pytest.param(EdifactFormat.IFTSTA, id="IFTSTA"), | ||
], | ||
) | ||
def test_create_create_segmentgroup_hierarchy(self, message_format: EdifactFormat, tmp_path): | ||
"""test if the reduced nested nachrichtenstruktur is created correctly""" | ||
file_path_dict = find_file_to_format( | ||
[message_format], path_to_test_edi_energy_mirror_repo, EdifactFormatVersion.FV2310 | ||
) | ||
file_path = file_path_dict[message_format] | ||
raw_lines = parse_raw_nachrichtenstrukturzeile(file_path) | ||
nachrichtenstrukturtabelle = NachrichtenstrukturTabelle.create_nachrichtenstruktur_tabelle(raw_lines) | ||
nested_nachrichtenstruktur, _ = NestedNachrichtenstruktur.create_nested_nachrichtenstruktur( | ||
nachrichtenstrukturtabelle | ||
) | ||
reduced_nested_nachrichtenstruktur = ReducedNestedNachrichtenstruktur.create_reduced_nested_nachrichtenstruktur( | ||
nested_nachrichtenstruktur | ||
) | ||
sgh = SegmentGroupHierarchy.create_segmentgroup_hierarchy(reduced_nested_nachrichtenstruktur) | ||
sgh.to_json(message_format, tmp_path) | ||
with open(tmp_path / "sgh.json", "r", encoding="utf-8") as file1: | ||
actual_sgh_json = json.load(file1) | ||
with open( | ||
expected_output_dir / EdifactFormatVersion.FV2310 / message_format / "sgh.json", | ||
"r", | ||
encoding="utf-8", | ||
) as sgh_file: | ||
expected_sgh_json = json.load(sgh_file) | ||
assert actual_sgh_json == expected_sgh_json | ||
with open(tmp_path / "sgh.json", "r", encoding="utf-8") as sgh_file: | ||
sgh = SegmentGroupHierarchySchema().loads(sgh_file.read()) | ||
assert isinstance(sgh, MausSGH) |