Skip to content

Commit 7226526

Browse files
authored
fix: add extra entry point to cli.py module, use annotated typer argument (#56)
1 parent c285a28 commit 7226526

File tree

5 files changed

+42
-20
lines changed

5 files changed

+42
-20
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,3 +134,5 @@ dmypy.json
134134
.vscode/
135135

136136
src/_your_package_version.py
137+
138+
src/_fundamend_version.py

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -131,11 +131,11 @@ pip install fundamend[cli]
131131
```
132132
Kann ein CLI-Tool in der entsprechenden venv installiert werden, das einzelne MIG- und AHB-XML-Dateien in entsprechende JSONs konvertiert:
133133
```bash
134-
(myvenv): xml2json path/to/mig.xml
134+
(myvenv): xml2json --xml-path path/to/mig.xml
135135
```
136136
erzeugt `path/to/mig.json`. Und
137137
```bash
138-
(myvenv): xml2json path/to/my/directory
138+
(myvenv): xml2json --xml-path path/to/my/directory
139139
```
140140
konvertiert alle XML-Dateien im entsprechenden Verzeichnis.
141141

pyproject.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,10 +70,10 @@ profile = "black"
7070
max-line-length = 120
7171

7272
[project.scripts]
73-
xml2json = "fundamend.cli:main"
73+
xml2json = "fundamend.cli:cli"
7474
# fundamend is the package in the src directory
7575
# .cli means the cli.py module inside the fundamend package
76-
# :main means the def main() function inside the cli.py module
76+
# :cli means the def cli() function inside the cli.py module
7777

7878
[mypy]
7979
truethy-bool = true

src/fundamend/cli.py

Lines changed: 29 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
11
"""contains the entrypoint for the command line interface"""
22

33
import json
4-
import sys
54
from pathlib import Path
65

76
import typer
87
from pydantic import RootModel
98
from rich.console import Console
9+
from typing_extensions import Annotated
1010

1111
from fundamend import AhbReader, Anwendungshandbuch, MessageImplementationGuide, MigReader
1212

13-
app = typer.Typer(help="Convert XML(s) by BDEW to JSON(s)")
13+
app = typer.Typer(name="xml2json", help="Convert XML(s) by BDEW to JSON(s)")
1414
err_console = Console(stderr=True) # https://typer.tiangolo.com/tutorial/printing/#printing-to-standard-error
1515

1616

@@ -40,15 +40,34 @@ def _convert_to_json_file(xml_file_path: Path) -> Path:
4040

4141

4242
@app.command()
43-
def main(xml_in_path: Path) -> None:
43+
def main(
44+
xml_path: Annotated[
45+
Path,
46+
typer.Option(
47+
exists=True,
48+
file_okay=True,
49+
dir_okay=True,
50+
writable=True,
51+
readable=True,
52+
resolve_path=True,
53+
),
54+
]
55+
) -> None:
4456
"""
4557
converts the xml file from xml_in_path to a json file next to the .xml
4658
"""
47-
if not xml_in_path.exists():
48-
err_console.print(f"The path {xml_in_path.absolute()} does not exist")
49-
sys.exit(1)
50-
if xml_in_path.is_dir():
51-
for xml_path in xml_in_path.rglob("*.xml"):
52-
_convert_to_json_file(xml_path)
59+
assert xml_path.exists() # ensured by typer
60+
if xml_path.is_dir():
61+
for _xml_path in xml_path.rglob("*.xml"):
62+
_convert_to_json_file(_xml_path)
5363
else:
54-
_convert_to_json_file(xml_in_path)
64+
_convert_to_json_file(xml_path)
65+
66+
67+
def cli() -> None:
68+
"""entry point of the script defined in pyproject.toml"""
69+
typer.run(main)
70+
71+
72+
if __name__ == "__main__":
73+
app()

unittests/test_cli.py

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
try:
77
from typer.testing import CliRunner
88

9+
runner = CliRunner()
910
from fundamend.cli import app
1011
except ImportError:
1112
_SKIP_TESTS = True
@@ -23,8 +24,8 @@ def test_cli_single_file_mig(tmp_path: Path) -> None:
2324
original_mig_file = Path(__file__).parent / "example_files" / "UTILTS_MIG_1.1c_Lesefassung_2023_12_12.xml"
2425
tmp_mig_path = tmp_path / "my_mig.xml"
2526
_copy_xml_file(original_mig_file, tmp_mig_path)
26-
runner = CliRunner()
27-
runner.invoke(app, [str(tmp_mig_path)])
27+
result = runner.invoke(app, ["--xml-path", str(tmp_mig_path.absolute())])
28+
assert result.exit_code == 0
2829
assert (tmp_path / "my_mig.json").exists()
2930

3031

@@ -34,8 +35,8 @@ def test_cli_single_file_ahb(tmp_path: Path) -> None:
3435
original_ahb_file = Path(__file__).parent / "example_files" / "UTILTS_AHB_1.1d_Konsultationsfassung_2024_04_02.xml"
3536
tmp_ahb_path = tmp_path / "my_ahb.xml"
3637
_copy_xml_file(original_ahb_file, tmp_ahb_path)
37-
runner = CliRunner()
38-
runner.invoke(app, [str(tmp_ahb_path)])
38+
result = runner.invoke(app, ["--xml-path", str(tmp_ahb_path)])
39+
assert result.exit_code == 0
3940
assert (tmp_path / "my_ahb.json").exists()
4041

4142

@@ -48,7 +49,7 @@ def test_cli_directory(tmp_path: Path) -> None:
4849
tmp_ahb_path = tmp_path / "my_ahb.xml"
4950
_copy_xml_file(original_ahb_file, tmp_ahb_path)
5051
_copy_xml_file(original_mig_file, tmp_mig_path)
51-
runner = CliRunner()
52-
runner.invoke(app, [str(tmp_path)])
52+
result = runner.invoke(app, ["--xml-path", str(tmp_path)])
53+
assert result.exit_code == 0
5354
assert (tmp_path / "my_mig.json").exists()
5455
assert (tmp_path / "my_ahb.json").exists()

0 commit comments

Comments
 (0)