Skip to content

Commit

Permalink
👩‍💻👨‍💻Enable using maus as CLI tool (#240)
Browse files Browse the repository at this point in the history
---------

Co-authored-by: konstantin <[email protected]>
  • Loading branch information
hf-krechan and hf-kklein authored Mar 30, 2023
1 parent d3c2ea1 commit ff1e170
Show file tree
Hide file tree
Showing 3 changed files with 91 additions and 2 deletions.
4 changes: 2 additions & 2 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,8 @@ Homepage = "https://github.com/Hochfrequenz/mig_ahb_utility_stack"
Documentation = "https://maus.readthedocs.io/en/latest/"

# wird das tool als CLI script verwendet, dann muss hier der Name des Scripts angegeben werden
# [project.scripts]
# kohlrahbi = "kohlrahbi:main"
[project.scripts]
maus = "maus:main"

[tool.hatch.metadata.hooks.fancy-pypi-readme]
content-type = "text/x-rst"
Expand Down
81 changes: 81 additions & 0 deletions src/maus/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,84 @@
"""
maus is the MIG AHB Utility Stack
"""
import json
from pathlib import Path

import click

from maus.mig_ahb_matching import to_deep_ahb
from maus.models.anwendungshandbuch import (
DeepAnwendungshandbuch,
DeepAnwendungshandbuchSchema,
FlatAnwendungshandbuchSchema,
)
from maus.models.message_implementation_guide import SegmentGroupHierarchySchema
from maus.reader.mig_xml_reader import MigXmlReader


# pylint:disable=too-many-arguments
@click.command()
@click.version_option()
@click.option(
"-fap", "--flat_ahb_path", type=click.Path(exists=True, path_type=Path), help="Path to the flat ahb json file"
)
@click.option("-sghp", "--sgh_path", type=click.Path(exists=True, path_type=Path), help="Path to the sgh json file")
@click.option("-tp", "--template_path", type=click.Path(exists=True, path_type=Path), help="Path to the template file")
@click.option("-cp", "--check_path", type=click.Path(exists=True, path_type=Path), help="Path to the maus json file")
@click.option(
"-o",
"--output_path",
type=click.Path(dir_okay=False, file_okay=True, path_type=Path),
help="Path to the output file",
)
@click.option("-v", "--verbose", is_flag=True, help="Print additional information")
# pylint:disable=no-value-for-parameter
def main(
flat_ahb_path: Path,
sgh_path: Path,
template_path: Path,
check_path: Path,
output_path: Path,
):
"""
The main entry point for the maus command line interface
"""

with open(flat_ahb_path, "r", encoding="utf-8") as flat_ahb_file:
flat_ahb = FlatAnwendungshandbuchSchema().load(json.load(flat_ahb_file))
with open(sgh_path, "r", encoding="utf-8") as sgh_file:
sgh = SegmentGroupHierarchySchema().loads(sgh_file.read())

mig_reader = MigXmlReader(template_path)

# create new maus.json files
maus = to_deep_ahb(flat_ahb, sgh, mig_reader)

if output_path is not None and check_path is not None:
click.secho("❌ You can only specify one of the output_path and maus_to_check_path parameters", fg="red")
click.Abort() # pylint:disable=pointless-exception-statement

if output_path is not None:
maus_dict = DeepAnwendungshandbuchSchema().dump(maus)

with open(output_path, "w", encoding="utf-8") as maus_file:
json.dump(maus_dict, maus_file, indent=2, ensure_ascii=False, sort_keys=True)

if check_path is not None:
with open(check_path, "r", encoding="utf-8") as maus_file:
expected_maus: DeepAnwendungshandbuch = DeepAnwendungshandbuchSchema().loads(maus_file.read())

# reset the line index to make the comparison work
# this is fine cause there is no logic built on top of the line index
maus.reset_ahb_line_index()
expected_maus.reset_ahb_line_index()

if expected_maus == maus:
click.secho("✅ The generated maus.json matches the expected one", fg="green")
else:
click.secho("❌ The generated maus.json does not match the expected one!", fg="red")
raise click.Abort()


if __name__ == "__main__":
main() # pylint:disable=no-value-for-parameter
8 changes: 8 additions & 0 deletions src/maus/models/anwendungshandbuch.py
Original file line number Diff line number Diff line change
Expand Up @@ -425,6 +425,14 @@ class DeepAnwendungshandbuch:
)
) #: the nested data

def reset_ahb_line_index(self) -> None:
"""
reset the ahb line index for all lines in the DeepAnwendungshandbuch
:return: nothing; modifies the instance
"""
for line in self.lines:
line.reset_ahb_line_index()

@staticmethod
def _query_segment_group(
segment_group: SegmentGroup, predicate: Callable[[SegmentGroup], bool]
Expand Down

0 comments on commit ff1e170

Please sign in to comment.