From 7226526c3ea22b19fa207241c3eb4939065d8064 Mon Sep 17 00:00:00 2001 From: konstantin Date: Thu, 5 Dec 2024 16:28:22 +0100 Subject: [PATCH] fix: add extra entry point to `cli.py` module, use annotated typer argument (#56) --- .gitignore | 2 ++ README.md | 4 ++-- pyproject.toml | 4 ++-- src/fundamend/cli.py | 39 +++++++++++++++++++++++++++++---------- unittests/test_cli.py | 13 +++++++------ 5 files changed, 42 insertions(+), 20 deletions(-) diff --git a/.gitignore b/.gitignore index fb33be7..a16607f 100644 --- a/.gitignore +++ b/.gitignore @@ -134,3 +134,5 @@ dmypy.json .vscode/ src/_your_package_version.py + +src/_fundamend_version.py diff --git a/README.md b/README.md index ed99dc7..23f2424 100644 --- a/README.md +++ b/README.md @@ -131,11 +131,11 @@ pip install fundamend[cli] ``` Kann ein CLI-Tool in der entsprechenden venv installiert werden, das einzelne MIG- und AHB-XML-Dateien in entsprechende JSONs konvertiert: ```bash -(myvenv): xml2json path/to/mig.xml +(myvenv): xml2json --xml-path path/to/mig.xml ``` erzeugt `path/to/mig.json`. Und ```bash -(myvenv): xml2json path/to/my/directory +(myvenv): xml2json --xml-path path/to/my/directory ``` konvertiert alle XML-Dateien im entsprechenden Verzeichnis. diff --git a/pyproject.toml b/pyproject.toml index 23b7e7a..5b41b98 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -70,10 +70,10 @@ profile = "black" max-line-length = 120 [project.scripts] -xml2json = "fundamend.cli:main" +xml2json = "fundamend.cli:cli" # fundamend is the package in the src directory # .cli means the cli.py module inside the fundamend package -# :main means the def main() function inside the cli.py module +# :cli means the def cli() function inside the cli.py module [mypy] truethy-bool = true diff --git a/src/fundamend/cli.py b/src/fundamend/cli.py index 05af850..6ca17f9 100644 --- a/src/fundamend/cli.py +++ b/src/fundamend/cli.py @@ -1,16 +1,16 @@ """contains the entrypoint for the command line interface""" import json -import sys from pathlib import Path import typer from pydantic import RootModel from rich.console import Console +from typing_extensions import Annotated from fundamend import AhbReader, Anwendungshandbuch, MessageImplementationGuide, MigReader -app = typer.Typer(help="Convert XML(s) by BDEW to JSON(s)") +app = typer.Typer(name="xml2json", help="Convert XML(s) by BDEW to JSON(s)") err_console = Console(stderr=True) # https://typer.tiangolo.com/tutorial/printing/#printing-to-standard-error @@ -40,15 +40,34 @@ def _convert_to_json_file(xml_file_path: Path) -> Path: @app.command() -def main(xml_in_path: Path) -> None: +def main( + xml_path: Annotated[ + Path, + typer.Option( + exists=True, + file_okay=True, + dir_okay=True, + writable=True, + readable=True, + resolve_path=True, + ), + ] +) -> None: """ converts the xml file from xml_in_path to a json file next to the .xml """ - if not xml_in_path.exists(): - err_console.print(f"The path {xml_in_path.absolute()} does not exist") - sys.exit(1) - if xml_in_path.is_dir(): - for xml_path in xml_in_path.rglob("*.xml"): - _convert_to_json_file(xml_path) + assert xml_path.exists() # ensured by typer + if xml_path.is_dir(): + for _xml_path in xml_path.rglob("*.xml"): + _convert_to_json_file(_xml_path) else: - _convert_to_json_file(xml_in_path) + _convert_to_json_file(xml_path) + + +def cli() -> None: + """entry point of the script defined in pyproject.toml""" + typer.run(main) + + +if __name__ == "__main__": + app() diff --git a/unittests/test_cli.py b/unittests/test_cli.py index 7aa3633..67a1eae 100644 --- a/unittests/test_cli.py +++ b/unittests/test_cli.py @@ -6,6 +6,7 @@ try: from typer.testing import CliRunner + runner = CliRunner() from fundamend.cli import app except ImportError: _SKIP_TESTS = True @@ -23,8 +24,8 @@ def test_cli_single_file_mig(tmp_path: Path) -> None: original_mig_file = Path(__file__).parent / "example_files" / "UTILTS_MIG_1.1c_Lesefassung_2023_12_12.xml" tmp_mig_path = tmp_path / "my_mig.xml" _copy_xml_file(original_mig_file, tmp_mig_path) - runner = CliRunner() - runner.invoke(app, [str(tmp_mig_path)]) + result = runner.invoke(app, ["--xml-path", str(tmp_mig_path.absolute())]) + assert result.exit_code == 0 assert (tmp_path / "my_mig.json").exists() @@ -34,8 +35,8 @@ def test_cli_single_file_ahb(tmp_path: Path) -> None: original_ahb_file = Path(__file__).parent / "example_files" / "UTILTS_AHB_1.1d_Konsultationsfassung_2024_04_02.xml" tmp_ahb_path = tmp_path / "my_ahb.xml" _copy_xml_file(original_ahb_file, tmp_ahb_path) - runner = CliRunner() - runner.invoke(app, [str(tmp_ahb_path)]) + result = runner.invoke(app, ["--xml-path", str(tmp_ahb_path)]) + assert result.exit_code == 0 assert (tmp_path / "my_ahb.json").exists() @@ -48,7 +49,7 @@ def test_cli_directory(tmp_path: Path) -> None: tmp_ahb_path = tmp_path / "my_ahb.xml" _copy_xml_file(original_ahb_file, tmp_ahb_path) _copy_xml_file(original_mig_file, tmp_mig_path) - runner = CliRunner() - runner.invoke(app, [str(tmp_path)]) + result = runner.invoke(app, ["--xml-path", str(tmp_path)]) + assert result.exit_code == 0 assert (tmp_path / "my_mig.json").exists() assert (tmp_path / "my_ahb.json").exists()