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.
* init datamodell * add logger * read line by line * added json output * initialize NachrichtenStrukturzeile from raw parsed table line * message_type from maus.edifact.EdifactFormat * changed names: SegmentGruppe -> NestedNachrichtenstruktur * renamed: BaumdiagrammSegmentGruppe -> ReducedNestedNachrichtenstruktur * ➕ added test for json output of nested nachrichtenstruktur * updated docstrings --------- Co-authored-by: kevin <68426071+hf-krechan@users.noreply.github.com> Co-authored-by: konstantin <konstantin.klein@hochfrequenz.de>
- Loading branch information
1 parent
c76255a
commit dbd8507
Showing
19 changed files
with
509 additions
and
339 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
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,116 @@ | ||
""" | ||
contains class for structured segmentgroups in mig tables. Builds table recursively. | ||
""" | ||
|
||
import json | ||
from pathlib import Path | ||
from types import NoneType | ||
from typing import Optional, Tuple | ||
|
||
from loguru import logger | ||
from maus.edifact import EdifactFormat | ||
from pydantic import BaseModel | ||
|
||
from migmose.mig.nachrichtenstruktur import NachrichtenstrukturTabelle | ||
from migmose.mig.nachrichtenstrukturzeile import NachrichtenstrukturZeile | ||
|
||
|
||
class NestedNachrichtenstruktur(BaseModel): | ||
""" | ||
class for structured segmentgroups in mig tables. Builds table recursively. Inherits from NachrichtenstrukturZeile | ||
e.g.(ORDCHG): | ||
{ | ||
"segmente": [ | ||
{ | ||
"zaehler": "0160", | ||
"nr": "7", | ||
"bezeichnung": "NAD", | ||
"standard_status": "M", | ||
"bdew_status": "M", | ||
"standard_maximale_wiederholungen": 1, | ||
"bdew_maximale_wiederholungen": 1, | ||
"ebene": 1, | ||
"inhalt": "MP-ID Absender" | ||
} | ||
], | ||
"segmentgruppen": [ | ||
{ | ||
"segmente": [ | ||
{ | ||
"zaehler": "0260", | ||
"nr": "8", | ||
"bezeichnung": "CTA", | ||
"standard_status": "M", | ||
"bdew_status": "M", | ||
"standard_maximale_wiederholungen": 1, | ||
"bdew_maximale_wiederholungen": 1, | ||
"ebene": 2, | ||
"inhalt": "Ansprechpartner" | ||
}, | ||
{ | ||
"zaehler": "0270", | ||
"nr": "9", | ||
"bezeichnung": "COM", | ||
"standard_status": "C", | ||
"bdew_status": "R", | ||
"standard_maximale_wiederholungen": 5, | ||
"bdew_maximale_wiederholungen": 5, | ||
"ebene": 3, | ||
"inhalt": "Kommunikationsverbindung" | ||
} | ||
], | ||
"segmentgruppen": [] | ||
} | ||
] | ||
} | ||
""" | ||
|
||
header_linie: Optional[NachrichtenstrukturZeile] = None | ||
segmente: list[Optional[NachrichtenstrukturZeile]] = [] | ||
segmentgruppen: list[Optional["NestedNachrichtenstruktur"]] = [] | ||
|
||
@classmethod | ||
def create_nested_nachrichtenstruktur( | ||
cls, table: NachrichtenstrukturTabelle, header_line: Optional[NachrichtenstrukturZeile] = None, index: int = 0 | ||
) -> Tuple["NestedNachrichtenstruktur", int]: | ||
"""init nested Nachrichtenstruktur""" | ||
collected_segments: list[Optional[NachrichtenstrukturZeile]] = [] | ||
collected_segmentgroups: list[Optional["NestedNachrichtenstruktur"]] = [] | ||
i = index | ||
while i < len(table.lines): | ||
line = table.lines[i] | ||
is_line_segmentgruppe = line.nr is None | ||
if is_line_segmentgruppe: | ||
added_segmentgroup, i = cls.create_nested_nachrichtenstruktur(table, line, i + 1) | ||
collected_segmentgroups.append(added_segmentgroup) | ||
else: | ||
collected_segments.append(line) | ||
i += 1 | ||
if i < len(table.lines) and not isinstance(header_line, NoneType): | ||
is_next_line_segmentgruppe = table.lines[i].nr is None | ||
is_current_ebene_greater_than_next_ebene = line.ebene > table.lines[i].ebene | ||
is_current_header_ebene_greater_eq_than_next_ebene = header_line.ebene >= table.lines[i].ebene | ||
|
||
if ( | ||
is_next_line_segmentgruppe and is_current_header_ebene_greater_eq_than_next_ebene | ||
) or is_current_ebene_greater_than_next_ebene: | ||
return ( | ||
cls( | ||
header_linie=header_line, | ||
segmente=collected_segments, | ||
segmentgruppen=collected_segmentgroups, | ||
), | ||
i, | ||
) | ||
return cls(header_linie=header_line, segmente=collected_segments, segmentgruppen=collected_segmentgroups), i | ||
|
||
def output_as_json(self, message_type: EdifactFormat, output_dir: Path) -> None: | ||
""" | ||
writes the NestedNachrichtenstruktur as json | ||
""" | ||
output_dir.mkdir(parents=True, exist_ok=True) | ||
file_path = output_dir.joinpath(f"{message_type}_nested_nachrichtenstruktur.json") | ||
structured_json = self.model_dump() | ||
with open(file_path, "w", encoding="utf-8") as json_file: | ||
json.dump(structured_json, json_file, indent=4) | ||
logger.info(f"Wrote nested Nachrichtenstruktur for {message_type} to {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
This file was deleted.
Oops, something went wrong.
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
Binary file not shown.
Binary file added
BIN
+1.26 MB
unittests/test_data/IFTSTA_MIG_2_0e_info_außerordentliche_20230929.docx
Binary file not shown.
Binary file not shown.
259 changes: 0 additions & 259 deletions
259
unittests/test_data/ORDCHG_MIG_1_1_info_20230331_v2.xml
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.