Skip to content

Commit

Permalink
Add argument to optionally define the BO4E-version (#43)
Browse files Browse the repository at this point in the history
* Add argument to optionally define the BO4E-version

* 🩹

* Consistency

* 🩹

* Update src/bo4e_generator/schema.py

Co-authored-by: konstantin <konstantin.klein@hochfrequenz.de>

---------

Co-authored-by: konstantin <konstantin.klein@hochfrequenz.de>
  • Loading branch information
lord-haffi and hf-kklein authored Jan 14, 2024
1 parent 9dcdcad commit b283bab
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 7 deletions.
23 changes: 19 additions & 4 deletions src/bo4e_generator/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
"""
import shutil
from pathlib import Path
from typing import Optional

import click

Expand All @@ -22,15 +23,19 @@ def resolve_paths(input_directory: Path, output_directory: Path) -> tuple[Path,


def generate_bo4e_schemas(
input_directory: Path, output_directory: Path, pydantic_v1: bool = False, clear_output: bool = False
input_directory: Path,
output_directory: Path,
pydantic_v1: bool = False,
clear_output: bool = False,
target_version: Optional[str] = None,
):
"""
Generate all BO4E schemas from the given input directory and save them in the given output directory.
"""
input_directory, output_directory = resolve_paths(input_directory, output_directory)
namespace = get_namespace(input_directory)
file_contents = parse_bo4e_schemas(input_directory, namespace, pydantic_v1)
version = get_version(namespace)
version = get_version(target_version, namespace)
file_contents[Path("__version__.py")] = bo4e_version_file_content(version)
file_contents[Path("__init__.py")] = bo4e_init_file_content(namespace, version)
if clear_output and output_directory.exists():
Expand Down Expand Up @@ -76,12 +81,22 @@ def generate_bo4e_schemas(
is_flag=True,
default=False,
)
@click.option(
"--target-version",
"-t",
help="Optionally set the target BO4E version. If not defined, it tries to read it from `_version`. "
"If it can't be found, it will be set to 'unknown'.",
type=str,
default=None,
)
@click.version_option(package_name="BO4E-Python-Generator")
def main(input_dir: Path, output_dir: Path, pydantic_v1: bool, clear_output: bool):
def main(
input_dir: Path, output_dir: Path, pydantic_v1: bool, clear_output: bool, target_version: Optional[str] = None
):
"""
CLI entry point for the bo4e-generator.
"""
generate_bo4e_schemas(input_dir, output_dir, pydantic_v1, clear_output)
generate_bo4e_schemas(input_dir, output_dir, pydantic_v1, clear_output, target_version)


if __name__ == "__main__":
Expand Down
9 changes: 7 additions & 2 deletions src/bo4e_generator/schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import json
import re
from pathlib import Path
from typing import Any
from typing import Any, Optional

from pydantic import BaseModel

Expand Down Expand Up @@ -70,10 +70,15 @@ def get_namespace(input_directory: Path) -> dict[str, SchemaMetadata]:
return namespace


def get_version(namespace: dict[str, SchemaMetadata]) -> str:
def get_version(target_version: Optional[str], namespace: dict[str, SchemaMetadata]) -> str:
"""
Get the version of the bo4e schemas.
"""
if target_version is not None:
gh_version_regex = re.compile(r"^v(?P<version>(?:\d+\.)*\d+)(?:-(?P<release_candidate>rc\d+))?$")
if gh_version_regex.match(target_version) is not None:
target_version = gh_version_regex.sub(r"\g<version>\g<release_candidate>", target_version)
return target_version
# The chosen class is arbitrary. All bo's and com's should contain the same version information.
try:
return namespace["Angebot"].schema_parsed["properties"]["_version"]["default"]
Expand Down
12 changes: 11 additions & 1 deletion unittests/test_main.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,17 @@ def test_main(self):
os.chdir(BASE_DIR)
runner = CliRunner()
result = runner.invoke(
main, ["--input-dir", str(INPUT_DIR), "--output-dir", str(OUTPUT_DIR), "-p2", "--clear-output"]
main,
[
"--input-dir",
str(INPUT_DIR),
"--output-dir",
str(OUTPUT_DIR),
"-p2",
"--clear-output",
"-t",
"v0.6.1-rc13",
],
)

assert (
Expand Down

0 comments on commit b283bab

Please sign in to comment.