Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -134,3 +134,5 @@ dmypy.json
.vscode/

src/_your_package_version.py

src/_fundamend_version.py
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Expand Down
4 changes: 2 additions & 2 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
39 changes: 29 additions & 10 deletions src/fundamend/cli.py
Original file line number Diff line number Diff line change
@@ -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


Expand Down Expand Up @@ -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()
13 changes: 7 additions & 6 deletions unittests/test_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
try:
from typer.testing import CliRunner

runner = CliRunner()
from fundamend.cli import app
except ImportError:
_SKIP_TESTS = True
Expand All @@ -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()


Expand All @@ -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()


Expand All @@ -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()
Loading