Skip to content

Commit

Permalink
✨Support wild cards as CLI pruefi args
Browse files Browse the repository at this point in the history
  • Loading branch information
hf-kklein committed Jul 11, 2023
1 parent bc60cba commit 2a36d8a
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 10 deletions.
32 changes: 25 additions & 7 deletions src/kohlrahbi/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand All @@ -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():
Expand Down Expand Up @@ -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()

Expand Down
29 changes: 26 additions & 3 deletions unittests/test_input_checks.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit 2a36d8a

Please sign in to comment.