diff --git a/src/kohlrahbi/__init__.py b/src/kohlrahbi/__init__.py index 2243e04e..0b484b69 100644 --- a/src/kohlrahbi/__init__.py +++ b/src/kohlrahbi/__init__.py @@ -6,7 +6,7 @@ import re import sys from pathlib import Path -from typing import Any +from typing import Any, Optional import click import docx # type:ignore[import] @@ -21,12 +21,29 @@ _pruefi_pattern = re.compile(r"^[1-9]\d{4}$") -def get_valid_pruefis(list_of_pruefis: list[str]) -> list[str]: +def get_valid_pruefis(list_of_pruefis: list[str], all_known_pruefis: Optional[list[str]] = None) -> list[str]: """ This function returns a new list with only those pruefis which match the pruefi_pattern. + It also """ - valid_pruefis: list[str] = [pruefi for pruefi in list_of_pruefis if _pruefi_pattern.match(pruefi)] - return valid_pruefis + result: set[str] = set() + for pruefi in list_of_pruefis: + if pruefi.count("*") > 1: + raise ValueError(f"Only one wildcard '*' is allowed per pruefi: '{pruefi}'") + if pruefi.endswith("*") and all_known_pruefis: + result = result.union({x for x in all_known_pruefis if x.startswith(pruefi[:-1])}) + elif pruefi.startswith("*") and all_known_pruefis: + result = result.union({x for x in all_known_pruefis if x.endswith(pruefi[1:])}) + elif "*" in pruefi: + first_part = pruefi.split("*")[0] + second_part = pruefi.split("*")[1] + result = result.union( + {x for x in all_known_pruefis if x.startswith(first_part) and x.endswith(second_part)} + ) + elif _pruefi_pattern.match(pruefi): + result.add(pruefi) + # else: is neither a wildcard nor a valid pruefi + return sorted(list(result)) def check_python_version(): @@ -140,15 +157,16 @@ def main(pruefis: list[str], input_path: Path, output_path: Path, file_type: lis output_path.mkdir(parents=True) click.secho(f"I created a new directory at {output_path}", fg="yellow") + all_known_pruefis = load_all_known_pruefis_from_file() if len(pruefis) == 0: click.secho("☝️ No pruefis were given. I will parse all known pruefis.", fg="yellow") - pruefis = load_all_known_pruefis_from_file() + pruefis = all_known_pruefis if len(file_type) == 0: click.secho( "ℹ You did not provide any value for the parameter --file-type. No files will be created.", fg="yellow" ) - valid_pruefis: list[str] = get_valid_pruefis(list_of_pruefis=pruefis) - if valid_pruefis == []: + valid_pruefis: list[str] = get_valid_pruefis(list_of_pruefis=pruefis, all_known_pruefis=all_known_pruefis) + if not any(valid_pruefis): click.secho("⚠️ There are no valid pruefidentifkatoren.", fg="red") raise click.Abort() diff --git a/unittests/test_input_checks.py b/unittests/test_input_checks.py index f8cb8328..bf548569 100644 --- a/unittests/test_input_checks.py +++ b/unittests/test_input_checks.py @@ -4,35 +4,58 @@ @pytest.mark.parametrize( - "input_pruefis, expected_pruefis", + "input_pruefis, expected_pruefis, known_pruefis", [ pytest.param( ["11042", "13007"], ["11042", "13007"], + None, id="only valid pruefis", ), pytest.param( ["01042", "13007"], ["13007"], + None, id="invalid pruefi: leading zero", ), pytest.param( ["1042", "13007"], ["13007"], + None, id="invalid pruefi: only four digits", ), pytest.param( ["abc", "13007"], ["13007"], + None, id="invalid pruefi: characters", ), pytest.param( ["abc"], [], + None, id="invalid pruefi: empty result", ), + pytest.param( + ["11*"], + ["11001", "11002", "11003"], + ["11001", "11002", "11003", "12001", "12002", "12003", "13001", "13002", "13003"], + id="wildcard at end", + ), + pytest.param( + ["*1"], + ["11001", "12001", "13001"], + ["11001", "11002", "11003", "12001", "12002", "12003", "13001", "13002", "13003"], + id="wildcard at begin", + ), + pytest.param( + ["11*1"], + ["11001"], + ["11001", "11002", "11003", "12001", "12002", "12003", "13001", "13002", "13003"], + id="wildcard in the middle", # who should seriously want this? + ), ], ) -def test_get_only_valid_pruefis(input_pruefis, expected_pruefis): - valid_pruefis = get_valid_pruefis(list_of_pruefis=input_pruefis) +def test_get_only_valid_pruefis(input_pruefis: list[str], expected_pruefis: list[str], known_pruefis: list[str] | None): + valid_pruefis = get_valid_pruefis(list_of_pruefis=input_pruefis, all_known_pruefis=known_pruefis) assert valid_pruefis == expected_pruefis