From b5ea64c37d44ce33f941c0cc0a800564d3d503f4 Mon Sep 17 00:00:00 2001 From: Konstantin Date: Thu, 5 Dec 2024 15:43:58 +0100 Subject: [PATCH 1/7] fix: add extra entry point to `cli.py` module --- pyproject.toml | 4 ++-- src/fundamend/cli.py | 4 ++++ 2 files changed, 6 insertions(+), 2 deletions(-) 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..70916c7 100644 --- a/src/fundamend/cli.py +++ b/src/fundamend/cli.py @@ -52,3 +52,7 @@ def main(xml_in_path: Path) -> None: _convert_to_json_file(xml_path) else: _convert_to_json_file(xml_in_path) + +def cli() -> None: + """entry point of the script defined in pyproject.toml""" + main() \ No newline at end of file From 2b747b75c758593bf7fdc1325d622345deef5346 Mon Sep 17 00:00:00 2001 From: Konstantin Date: Thu, 5 Dec 2024 15:55:32 +0100 Subject: [PATCH 2/7] fix --- README.md | 4 ++-- src/fundamend/cli.py | 22 +++++++++++++++------- 2 files changed, 17 insertions(+), 9 deletions(-) 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/src/fundamend/cli.py b/src/fundamend/cli.py index 70916c7..b42ca94 100644 --- a/src/fundamend/cli.py +++ b/src/fundamend/cli.py @@ -3,6 +3,7 @@ import json import sys from pathlib import Path +from typing_extensions import Annotated import typer from pydantic import RootModel @@ -40,19 +41,26 @@ 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") + if not xml_path.exists(): + err_console.print(f"The path {xml_path.absolute()} does not exist") sys.exit(1) - if xml_in_path.is_dir(): - for xml_path in xml_in_path.rglob("*.xml"): + 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""" - main() \ No newline at end of file + typer.run(main) \ No newline at end of file From 6a9543f9b2eeb58f1ee5694354db4d8b76145fe6 Mon Sep 17 00:00:00 2001 From: Konstantin Date: Thu, 5 Dec 2024 15:55:43 +0100 Subject: [PATCH 3/7] ignore version file --- .gitignore | 2 ++ 1 file changed, 2 insertions(+) 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 From d17e0a127bdef60874e163efd2f9a5806a183b95 Mon Sep 17 00:00:00 2001 From: Konstantin Date: Thu, 5 Dec 2024 15:55:59 +0100 Subject: [PATCH 4/7] format --- src/fundamend/cli.py | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/src/fundamend/cli.py b/src/fundamend/cli.py index b42ca94..6a827a1 100644 --- a/src/fundamend/cli.py +++ b/src/fundamend/cli.py @@ -3,11 +3,11 @@ import json import sys from pathlib import Path -from typing_extensions import Annotated import typer from pydantic import RootModel from rich.console import Console +from typing_extensions import Annotated from fundamend import AhbReader, Anwendungshandbuch, MessageImplementationGuide, MigReader @@ -41,14 +41,19 @@ def _convert_to_json_file(xml_file_path: Path) -> Path: @app.command() -def main(xml_path: Annotated[Path, typer.Option( +def main( + xml_path: Annotated[ + Path, + typer.Option( exists=True, file_okay=True, dir_okay=True, writable=True, readable=True, resolve_path=True, - )]) -> None: + ), + ] +) -> None: """ converts the xml file from xml_in_path to a json file next to the .xml """ @@ -61,6 +66,7 @@ def main(xml_path: Annotated[Path, typer.Option( else: _convert_to_json_file(xml_path) + def cli() -> None: """entry point of the script defined in pyproject.toml""" - typer.run(main) \ No newline at end of file + typer.run(main) From f707950f30cc55c275b5098a417d374fd4116e45 Mon Sep 17 00:00:00 2001 From: Konstantin Date: Thu, 5 Dec 2024 15:58:11 +0100 Subject: [PATCH 5/7] fix redefining --- src/fundamend/cli.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/fundamend/cli.py b/src/fundamend/cli.py index 6a827a1..f8a8afc 100644 --- a/src/fundamend/cli.py +++ b/src/fundamend/cli.py @@ -61,8 +61,8 @@ def main( err_console.print(f"The path {xml_path.absolute()} does not exist") sys.exit(1) if xml_path.is_dir(): - for xml_path in xml_path.rglob("*.xml"): - _convert_to_json_file(xml_path) + for _xml_path in xml_path.rglob("*.xml"): + _convert_to_json_file(_xml_path) else: _convert_to_json_file(xml_path) From 6405c41fd450e2f827e98e8218f126dc95ed7172 Mon Sep 17 00:00:00 2001 From: Konstantin Date: Thu, 5 Dec 2024 16:21:09 +0100 Subject: [PATCH 6/7] more fixes --- src/fundamend/cli.py | 10 ++++++---- unittests/test_cli.py | 13 +++++++------ 2 files changed, 13 insertions(+), 10 deletions(-) diff --git a/src/fundamend/cli.py b/src/fundamend/cli.py index f8a8afc..3caec7d 100644 --- a/src/fundamend/cli.py +++ b/src/fundamend/cli.py @@ -11,7 +11,7 @@ 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 @@ -57,9 +57,7 @@ def main( """ converts the xml file from xml_in_path to a json file next to the .xml """ - if not xml_path.exists(): - err_console.print(f"The path {xml_path.absolute()} does not exist") - sys.exit(1) + assert xml_path.exists() if xml_path.is_dir(): for _xml_path in xml_path.rglob("*.xml"): _convert_to_json_file(_xml_path) @@ -70,3 +68,7 @@ def main( 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() From 0e068013341c7c8bb3625a9c6f67a73d891e48b3 Mon Sep 17 00:00:00 2001 From: Konstantin Date: Thu, 5 Dec 2024 16:23:02 +0100 Subject: [PATCH 7/7] fix pylint --- src/fundamend/cli.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/fundamend/cli.py b/src/fundamend/cli.py index 3caec7d..6ca17f9 100644 --- a/src/fundamend/cli.py +++ b/src/fundamend/cli.py @@ -1,7 +1,6 @@ """contains the entrypoint for the command line interface""" import json -import sys from pathlib import Path import typer @@ -57,7 +56,7 @@ def main( """ converts the xml file from xml_in_path to a json file next to the .xml """ - assert xml_path.exists() + 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)