Skip to content

Commit b283bab

Browse files
Add argument to optionally define the BO4E-version (#43)
* 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>
1 parent 9dcdcad commit b283bab

3 files changed

Lines changed: 37 additions & 7 deletions

File tree

src/bo4e_generator/__main__.py

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
"""
44
import shutil
55
from pathlib import Path
6+
from typing import Optional
67

78
import click
89

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

2324

2425
def generate_bo4e_schemas(
25-
input_directory: Path, output_directory: Path, pydantic_v1: bool = False, clear_output: bool = False
26+
input_directory: Path,
27+
output_directory: Path,
28+
pydantic_v1: bool = False,
29+
clear_output: bool = False,
30+
target_version: Optional[str] = None,
2631
):
2732
"""
2833
Generate all BO4E schemas from the given input directory and save them in the given output directory.
2934
"""
3035
input_directory, output_directory = resolve_paths(input_directory, output_directory)
3136
namespace = get_namespace(input_directory)
3237
file_contents = parse_bo4e_schemas(input_directory, namespace, pydantic_v1)
33-
version = get_version(namespace)
38+
version = get_version(target_version, namespace)
3439
file_contents[Path("__version__.py")] = bo4e_version_file_content(version)
3540
file_contents[Path("__init__.py")] = bo4e_init_file_content(namespace, version)
3641
if clear_output and output_directory.exists():
@@ -76,12 +81,22 @@ def generate_bo4e_schemas(
7681
is_flag=True,
7782
default=False,
7883
)
84+
@click.option(
85+
"--target-version",
86+
"-t",
87+
help="Optionally set the target BO4E version. If not defined, it tries to read it from `_version`. "
88+
"If it can't be found, it will be set to 'unknown'.",
89+
type=str,
90+
default=None,
91+
)
7992
@click.version_option(package_name="BO4E-Python-Generator")
80-
def main(input_dir: Path, output_dir: Path, pydantic_v1: bool, clear_output: bool):
93+
def main(
94+
input_dir: Path, output_dir: Path, pydantic_v1: bool, clear_output: bool, target_version: Optional[str] = None
95+
):
8196
"""
8297
CLI entry point for the bo4e-generator.
8398
"""
84-
generate_bo4e_schemas(input_dir, output_dir, pydantic_v1, clear_output)
99+
generate_bo4e_schemas(input_dir, output_dir, pydantic_v1, clear_output, target_version)
85100

86101

87102
if __name__ == "__main__":

src/bo4e_generator/schema.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
import json
55
import re
66
from pathlib import Path
7-
from typing import Any
7+
from typing import Any, Optional
88

99
from pydantic import BaseModel
1010

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

7272

73-
def get_version(namespace: dict[str, SchemaMetadata]) -> str:
73+
def get_version(target_version: Optional[str], namespace: dict[str, SchemaMetadata]) -> str:
7474
"""
7575
Get the version of the bo4e schemas.
7676
"""
77+
if target_version is not None:
78+
gh_version_regex = re.compile(r"^v(?P<version>(?:\d+\.)*\d+)(?:-(?P<release_candidate>rc\d+))?$")
79+
if gh_version_regex.match(target_version) is not None:
80+
target_version = gh_version_regex.sub(r"\g<version>\g<release_candidate>", target_version)
81+
return target_version
7782
# The chosen class is arbitrary. All bo's and com's should contain the same version information.
7883
try:
7984
return namespace["Angebot"].schema_parsed["properties"]["_version"]["default"]

unittests/test_main.py

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,17 @@ def test_main(self):
1717
os.chdir(BASE_DIR)
1818
runner = CliRunner()
1919
result = runner.invoke(
20-
main, ["--input-dir", str(INPUT_DIR), "--output-dir", str(OUTPUT_DIR), "-p2", "--clear-output"]
20+
main,
21+
[
22+
"--input-dir",
23+
str(INPUT_DIR),
24+
"--output-dir",
25+
str(OUTPUT_DIR),
26+
"-p2",
27+
"--clear-output",
28+
"-t",
29+
"v0.6.1-rc13",
30+
],
2131
)
2232

2333
assert (

0 commit comments

Comments
 (0)