generated from Hochfrequenz/python_template_repository
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Add CLI Command
xml2json
(#55)
- Loading branch information
Showing
8 changed files
with
143 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,3 +8,4 @@ als | |
paket | ||
beginn | ||
referenz | ||
alle |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
"""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 fundamend import AhbReader, Anwendungshandbuch, MessageImplementationGuide, MigReader | ||
|
||
app = typer.Typer(help="Convert XML(s) by BDEW to JSON(s)") | ||
err_console = Console(stderr=True) # https://typer.tiangolo.com/tutorial/printing/#printing-to-standard-error | ||
|
||
|
||
def _convert_to_json_file(xml_file_path: Path) -> Path: | ||
"""converts the given XML file to a JSON file and returns the path of the latter""" | ||
if not xml_file_path.is_file(): | ||
raise ValueError(f"The given path {xml_file_path.absolute()} is not a file") | ||
is_ahb = "ahb" in xml_file_path.stem.lower() | ||
is_mig = "mig" in xml_file_path.stem.lower() | ||
if is_ahb and is_mig: | ||
raise ValueError(f"Cannot detect if {xml_file_path} is an AHB or MIG") | ||
root_model: RootModel[Anwendungshandbuch] | RootModel[MessageImplementationGuide] | ||
if is_ahb: | ||
ahb_model = AhbReader(xml_file_path).read() | ||
root_model = RootModel[Anwendungshandbuch](ahb_model) | ||
elif is_mig: | ||
mig_model = MigReader(xml_file_path).read() | ||
root_model = RootModel[MessageImplementationGuide](mig_model) | ||
else: | ||
raise ValueError(f"Seems like {xml_file_path} is neither an AHB nor a MIG") | ||
out_dict = root_model.model_dump(mode="json") | ||
json_file_path = xml_file_path.with_suffix(".json") | ||
with open(json_file_path, encoding="utf-8", mode="w") as outfile: | ||
json.dump(out_dict, outfile, indent=True, ensure_ascii=False) | ||
print(f"Successfully converted {xml_file_path} file to JSON {json_file_path}") | ||
return json_file_path | ||
|
||
|
||
@app.command() | ||
def main(xml_in_path: Path) -> 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) | ||
else: | ||
_convert_to_json_file(xml_in_path) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
from pathlib import Path | ||
|
||
import pytest | ||
|
||
_SKIP_TESTS = False | ||
try: | ||
from typer.testing import CliRunner | ||
|
||
from fundamend.cli import app | ||
except ImportError: | ||
_SKIP_TESTS = True | ||
|
||
|
||
def _copy_xml_file(inpath: Path, outpath: Path) -> None: | ||
with open(outpath, encoding="utf-8", mode="w") as outfile: | ||
with open(inpath, encoding="utf-8", mode="r") as infile: | ||
outfile.write(infile.read()) | ||
|
||
|
||
def test_cli_single_file_mig(tmp_path: Path) -> None: | ||
if _SKIP_TESTS: | ||
pytest.skip("Seems like typer is not installed") | ||
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)]) | ||
assert (tmp_path / "my_mig.json").exists() | ||
|
||
|
||
def test_cli_single_file_ahb(tmp_path: Path) -> None: | ||
if _SKIP_TESTS: | ||
pytest.skip("Seems like typer is not installed") | ||
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)]) | ||
assert (tmp_path / "my_ahb.json").exists() | ||
|
||
|
||
def test_cli_directory(tmp_path: Path) -> None: | ||
if _SKIP_TESTS: | ||
pytest.skip("Seems like typer is not installed") | ||
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" | ||
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) | ||
_copy_xml_file(original_mig_file, tmp_mig_path) | ||
runner = CliRunner() | ||
runner.invoke(app, [str(tmp_path)]) | ||
assert (tmp_path / "my_mig.json").exists() | ||
assert (tmp_path / "my_ahb.json").exists() |