generated from Hochfrequenz/python_template_repository
-
Notifications
You must be signed in to change notification settings - Fork 2
feat: Prepare For Multi-Command CLI Interface #121
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 5 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
dd7c6de
put current command into subfolder
hf-krechan 7a32e60
update cli.py to new structure
hf-krechan 3ec2b2f
remove unused imports
hf-krechan 90e3a10
add module docstring
hf-krechan d8bcbcf
streamline command
hf-krechan 629ae3d
update cli commands in README
hf-krechan b3c77c1
Merge branch 'main' into prepare-multiple-commands
hf-kklein File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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,11 @@ | ||
""" | ||
Contains the commands for the CLI. | ||
""" | ||
|
||
import typer | ||
|
||
from fundamend.commands.xml2json import app as xml2json_app | ||
|
||
app = typer.Typer() | ||
|
||
app.add_typer(xml2json_app) |
This file contains hidden or 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,64 @@ | ||||||||
""" | ||||||||
Contains the command to convert XML files to JSON files. | ||||||||
""" | ||||||||
|
||||||||
import json | ||||||||
from pathlib import Path | ||||||||
|
||||||||
import typer | ||||||||
from pydantic import RootModel | ||||||||
from typing_extensions import Annotated | ||||||||
|
||||||||
from fundamend import AhbReader, Anwendungshandbuch, MessageImplementationGuide, MigReader | ||||||||
|
||||||||
app = typer.Typer() | ||||||||
|
||||||||
|
||||||||
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 | ||||||||
|
||||||||
Comment on lines
+38
to
+40
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [nitpick] Consider replacing print() with typer.echo() for consistent CLI output handling which integrates better with Typer's logging and testing facilities.
Suggested change
Copilot uses AI. Check for mistakes. Positive FeedbackNegative Feedback |
||||||||
|
||||||||
@app.command() | ||||||||
def xml2json( | ||||||||
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 | ||||||||
""" | ||||||||
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_path) |
This file contains hidden or 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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
please adapt the readme accordingly: https://github.com/Hochfrequenz/xml-fundamend-python?tab=readme-ov-file#cli-tool-f%C3%BCr-xml%EF%B8%8Fjson-konvertierung