Skip to content

Commit

Permalink
Merge branch 'main' into dependabot/pip/coverage-7.2.7
Browse files Browse the repository at this point in the history
  • Loading branch information
hf-kklein committed Aug 10, 2023
2 parents 06e2884 + ebabc97 commit 5963c65
Show file tree
Hide file tree
Showing 3 changed files with 113 additions and 110 deletions.
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ 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]
maus = "maus:main"
maus = "maus.cli:main"

[tool.hatch.metadata.hooks.fancy-pypi-readme]
content-type = "text/x-rst"
Expand Down
109 changes: 0 additions & 109 deletions src/maus/__init__.py
Original file line number Diff line number Diff line change
@@ -1,112 +1,3 @@
"""
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",
required=True,
)
@click.option(
"-sghp",
"--sgh_path",
type=click.Path(exists=True, path_type=Path),
help="Path to the sgh json file",
required=True,
)
@click.option(
"-tp",
"--template_path",
type=click.Path(exists=True, path_type=Path),
help="Path to the template file",
required=True,
)
@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",
)
# pylint:disable=no-value-for-parameter
def main(
flat_ahb_path: Path,
sgh_path: Path,
template_path: Path,
check_path: Path,
output_path: Path,
):
"""
🐭 MAUS CLI is a standalone executable that generates .maus.json files from given input data
"""

if check_path is None and output_path is None or check_path is not None and output_path is not None:
# pylint:disable=line-too-long
click.secho(
"❌ You need to specify either the `output_path` or the `check_path` parameter. Please use --help to see more information.",
fg="red",
)
raise click.Abort()

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")
raise click.Abort()

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
112 changes: 112 additions & 0 deletions src/maus/cli.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
"""
the maus cli tool
"""
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",
required=True,
)
@click.option(
"-sghp",
"--sgh_path",
type=click.Path(exists=True, path_type=Path),
help="Path to the sgh json file",
required=True,
)
@click.option(
"-tp",
"--template_path",
type=click.Path(exists=True, path_type=Path),
help="Path to the template file",
required=True,
)
@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",
)
# pylint:disable=no-value-for-parameter
def main(
flat_ahb_path: Path,
sgh_path: Path,
template_path: Path,
check_path: Path,
output_path: Path,
):
"""
🐭 MAUS CLI is a standalone executable that generates .maus.json files from given input data
"""

if check_path is None and output_path is None or check_path is not None and output_path is not None:
# pylint:disable=line-too-long
click.secho(
"❌ You need to specify either the `output_path` or the `check_path` parameter. Please use --help to see more information.",
fg="red",
)
raise click.Abort()

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")
raise click.Abort()

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

0 comments on commit 5963c65

Please sign in to comment.