Skip to content

Commit

Permalink
changed sorting key including major and minor versions, adapted tests
Browse files Browse the repository at this point in the history
  • Loading branch information
DeltaDaniel committed Oct 7, 2024
1 parent b54d02c commit 8bc3e91
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 15 deletions.
2 changes: 1 addition & 1 deletion src/migmose/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ def main(
format_version,
output_dir_for_format,
)
document_version = _extract_document_version(file)
document_version, *_ = _extract_document_version(file)
reduced_nested_nachrichtenstruktur.output_tree(m_format, output_dir_for_format, document_version)


Expand Down
17 changes: 9 additions & 8 deletions src/migmose/parsing.py
Original file line number Diff line number Diff line change
Expand Up @@ -160,28 +160,29 @@ def parse_raw_nachrichtenstrukturzeile(input_path: Path) -> list[str]:


_pattern = re.compile(
r"MIG(?:Strom|Gas)?-?informatorischeLesefassung?(.*?)"
r"MIG(?:Strom|Gas)?-?informatorischeLesefassung?((\d+)\.(\d+)([a-z]?))"
r"(?:_|KonsolidierteLesefassung|-AußerordentlicheVeröffentlichung)",
re.IGNORECASE,
)


def _extract_document_version(path: Path | str) -> str:
def _extract_document_version(path: Path | str) -> tuple[str, int | None, int | None, str]:
"""Returns the document version, major, minor, and suffix from the given file path."""
if isinstance(path, str):
document_str = path
else:
document_str = str(path)
matches = _pattern.search(document_str)
if matches:
document_version = matches.group(1)
document_version, major, minor, suffix = matches.groups()
if document_version == "":
logger.warning(f"❌ No document version found in {path}.", fg="red")
return document_version
return document_version or "", int(major) or 0, int(minor) or 0, suffix or ""
logger.error(f"❌ Unexpected document name in {path}.", fg="red")
return ""
return "", None, None, ""


def _get_sort_key(path: Path) -> tuple[int, int, str]:
def _get_sort_key(path: Path) -> tuple[int, int, int | None, int | None, str]:
"""
Extracts the sort key from the given path.
Expand All @@ -194,5 +195,5 @@ def _get_sort_key(path: Path) -> tuple[int, int, str]:
parts = path.stem.split("_")
gueltig_von_date = int(parts[-1])
gueltig_bis_date = int(parts[-2])
version_number = _extract_document_version(parts[-3])
return gueltig_von_date, gueltig_bis_date, version_number
_, major, minor, suffix = _extract_document_version(parts[-3])
return gueltig_von_date, gueltig_bis_date, major, minor, suffix
35 changes: 30 additions & 5 deletions unittests/__snapshots__/test_parsing.ambr
Original file line number Diff line number Diff line change
@@ -1,16 +1,41 @@
# serializer version: 1
# name: TestParsing.test_extract_document_version[IFTSTA]
''
tuple(
'',
None,
None,
'',
)
# ---
# name: TestParsing.test_extract_document_version[REMADV]
'2.9b'
tuple(
'2.9b',
2,
9,
'b',
)
# ---
# name: TestParsing.test_extract_document_version[REQOTE]
'1.3'
tuple(
'1.3',
1,
3,
'',
)
# ---
# name: TestParsing.test_extract_document_version[UTILMDG]
'G1.0a'
tuple(
'',
None,
None,
'',
)
# ---
# name: TestParsing.test_extract_document_version[UTILMDS]
'S1.1'
tuple(
'',
None,
None,
'',
)
# ---
2 changes: 1 addition & 1 deletion unittests/test_reduced_nested_nachrichtenstruktur.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ def test_output_tree(self, message_format: EdifactFormat, tmp_path, snapshot):
reduced_nested_nachrichtenstruktur = ReducedNestedNachrichtenstruktur.create_reduced_nested_nachrichtenstruktur(
nested_nachrichtenstruktur
)
document_version = _extract_document_version(file_path)
document_version, *_ = _extract_document_version(file_path)
reduced_nested_nachrichtenstruktur.output_tree(message_format, tmp_path, document_version)
with open(tmp_path / f"{message_format}{document_version}.tree", "r", encoding="utf-8") as actual_file:
assert actual_file.read() == snapshot
Expand Down

0 comments on commit 8bc3e91

Please sign in to comment.