Skip to content

Commit

Permalink
Fix toml path (#262)
Browse files Browse the repository at this point in the history
* wip

* Delete src/kohlrahbi/all_known_pruefis.toml

* magically works somehow

* fix type hint

* fix typing

* fix

at least the test
  • Loading branch information
hf-kklein committed Mar 28, 2024
1 parent 141f6e3 commit b9b8d94
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 489 deletions.
33 changes: 25 additions & 8 deletions src/kohlrahbi/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
import docx # type:ignore[import]
import pandas as pd
import tomlkit
from maus.edifact import EdifactFormat
from maus.edifact import EdifactFormat, EdifactFormatVersion

from kohlrahbi.ahb.ahbtable import AhbTable
from kohlrahbi.changehistory.changehistorytable import ChangeHistoryTable
Expand Down Expand Up @@ -83,14 +83,18 @@ def check_output_path(path: Path) -> None:


def load_all_known_pruefis_from_file(
path_to_all_known_pruefis: Path = Path(__file__).parent / Path("all_known_pruefis.toml"),
path_to_all_known_pruefis: Path | None, format_version: EdifactFormatVersion
) -> dict[str, str | None]:
"""
Loads the file which contains all known Prüfidentifikatoren.
The file may be manually updated with the script `collect_pruefis.py`.
"""

with open(path_to_all_known_pruefis, "rb") as file:
actual_path: Path
if path_to_all_known_pruefis is None:
actual_path = Path(__file__).parent / "format_versions" / Path(f"{format_version}_all_known_pruefis.toml")
else:
actual_path = path_to_all_known_pruefis
with open(actual_path, "rb") as file:
state_of_kohlrahbi: dict[str, Any] = tomlkit.load(file)

meta_data_section = state_of_kohlrahbi.get("meta_data")
Expand Down Expand Up @@ -208,14 +212,16 @@ def scrape_change_histories(input_path: Path, output_path: Path) -> None:
save_change_histories_to_excel(change_history_collection, output_path)


def load_pruefis_if_empty(pruefi_to_file_mapping: dict[str, str | None]) -> dict[str, str | None]:
def load_pruefis_if_empty(
pruefi_to_file_mapping: dict[str, str | None], format_version: EdifactFormatVersion
) -> dict[str, str | None]:
"""
If the user did not provide any pruefis we load all known pruefis
and the paths to the file containing them from the toml file.
"""
if not pruefi_to_file_mapping:
click.secho("☝️ No pruefis were given. I will parse all known pruefis.", fg="yellow")
return load_all_known_pruefis_from_file()
return load_all_known_pruefis_from_file(path_to_all_known_pruefis=None, format_version=format_version)
return pruefi_to_file_mapping


Expand Down Expand Up @@ -318,11 +324,12 @@ def scrape_pruefis(
basic_input_path: Path,
output_path: Path,
file_type: Literal["flatahb", "csv", "xlsx", "conditions"],
format_version: EdifactFormatVersion,
) -> None:
"""
starts the scraping process for provided pruefi_to_file_mappings
"""
pruefi_to_file_mapping = load_pruefis_if_empty(pruefi_to_file_mapping)
pruefi_to_file_mapping = load_pruefis_if_empty(pruefi_to_file_mapping, format_version)
validate_file_type(file_type)

valid_pruefis = validate_pruefis(list(pruefi_to_file_mapping.keys()))
Expand Down Expand Up @@ -384,6 +391,12 @@ def scrape_pruefis(
type=click.Choice(["flatahb", "csv", "xlsx", "conditions"], case_sensitive=False),
multiple=True,
)
@click.option(
"--format-version",
multiple=False,
type=click.Choice([e.value for e in EdifactFormatVersion], case_sensitive=False),
help="Format version(s) of the AHB documents, e.g. FV2310",
)
@click.option(
"--assume-yes",
"-y",
Expand All @@ -397,13 +410,16 @@ def main(
input_path: Path,
output_path: Path,
file_type: Literal["flatahb", "csv", "xlsx", "conditions"],
format_version: EdifactFormatVersion | str,
assume_yes: bool,
) -> None:
"""
A program to get a machine readable version of the AHBs docx files published by edi@energy.
"""
check_python_version()

format_version: EdifactFormatVersion # type:ignore[no-redef]
if isinstance(format_version, str):
format_version = EdifactFormatVersion(format_version)
if not assume_yes:
check_output_path(path=output_path)
else:
Expand All @@ -422,6 +438,7 @@ def main(
basic_input_path=input_path,
output_path=output_path,
file_type=file_type,
format_version=format_version,
)
case "changehistory":
scrape_change_histories(input_path=input_path, output_path=output_path)
Expand Down
Loading

0 comments on commit b9b8d94

Please sign in to comment.