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

Fix maus 0.3.x compatability issues #232

Draft
wants to merge 15 commits into
base: main
Choose a base branch
from
31 changes: 30 additions & 1 deletion src/maus/reader/mig_xml_reader.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@
"""
import re
from pathlib import Path
from typing import List, Set, TypeVar, Union
from typing import List, Set, TypeVar, Union, Optional
from xml.etree.ElementTree import Element

from maus.reader.ahb_location_xml import from_xml_elements

try:
from lxml import etree # type:ignore[import]
except ImportError as import_error:
Expand Down Expand Up @@ -135,6 +137,31 @@ def _get_candidate_index_from_key(self, layer: AhbLocationLayer, candidates: Lis
# todo: what if there are >1 matches. using the first one just hides data problems. we should use one instead
return first(possible_results)

def _find_element_using_explicit_location(self, ahb_location: AhbLocation) -> Optional[Element]:
"""
find the perfect match, if it exists
:param ahb_location:
:return: None if no perfect match was found; the perfect match otherwise
"""
all_explicit_locations = self._original_tree.xpath(f"//ahbLocations")
location_layers_only = AhbLocation(
layers=ahb_location.layers, data_element_id=None, qualifier=None
)
for explicit_locations in all_explicit_locations:
locations_from_mig_xml = from_xml_elements(explicit_locations)
for location_from_mig_xml in locations_from_mig_xml:
location_layers_from_mig_xml = AhbLocation(
layers=location_from_mig_xml.layers, data_element_id=None, qualifier=None
)
if ahb_location==location_from_mig_xml or (location_layers_only==location_layers_from_mig_xml and ahb_location.data_element_id is None):
result = explicit_locations.getparent()
if ahb_location.data_element_id is None:
while result.tag!="class":
result = result.getparent()
return result
return None


# First make it work, then split it up
# pylint:disable=too-many-branches
def get_element(self, ahb_location: AhbLocation) -> Element:
Expand All @@ -143,6 +170,8 @@ def get_element(self, ahb_location: AhbLocation) -> Element:
Raises ValueErrors if it cannot find the group or the result would be ambiguous.
"""
candidates: List[Element]
if (perfect_match := self._find_element_using_explicit_location(ahb_location)) is not None:
return perfect_match
final_query_path = f"/{self.get_format_name()}/class[@ref='/']"
for layer in ahb_location.layers:
query_path = final_query_path + f"/class[@ref='{layer.segment_group_key or 'UNH'}']"
Expand Down
2 changes: 1 addition & 1 deletion tests/integration_tests/edifact-templates
11 changes: 10 additions & 1 deletion tests/integration_tests/test_anmeldung_maus.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,21 @@ def test_maus_creation_11001_52e(self, datafiles):
@pytest.mark.datafiles("../../machine-readable_anwendungshandbuecher/FV2210/UTILMD/flatahb/11002.json")
@pytest.mark.datafiles("./edifact-templates/segment_group_hierarchies/FV2210/UTILMD.sgh.json")
def test_maus_creation_11002_52e(self, datafiles):
create_maus_and_assert(
result = create_maus_and_assert(
flat_ahb_path=Path(datafiles) / "11002.json",
sgh_path=Path(datafiles) / "UTILMD.sgh.json",
template_path=Path(datafiles) / Path("UTILMD5.2e.template"),
maus_path=Path("edifact-templates/maus/FV2210/UTILMD/11002_maus.json"),
)
assert result is not None
geplante_msb_ablesungen_strom = result.maus.find_segments(
segment_predicate=lambda seg: seg.section_name == "Geplante Turnusablesung des MSB (Strom)",
)
assert not any(
msba
for msba in geplante_msb_ablesungen_strom
if "Referenz auf die ID der Marktlokation" in msba.discriminator
)

@pytest.mark.datafiles("./edifact-templates/edi/UTILMD/UTILMD5.2e.template")
@pytest.mark.datafiles("../../machine-readable_anwendungshandbuecher/FV2210/UTILMD/flatahb/11003.json")
Expand Down