From 5c7e0d7c119621c8dc5d8195dd9076b864d3d192 Mon Sep 17 00:00:00 2001 From: Leon Haffmans <49658102+lord-haffi@users.noreply.github.com> Date: Fri, 10 Nov 2023 20:43:08 +0100 Subject: [PATCH] Add `__version__` to `bo4e/__init__.py` (#11) --- src/bo4e_generator/__main__.py | 4 ++-- src/bo4e_generator/parser.py | 6 ++++-- src/bo4e_generator/schema.py | 8 ++++++++ unittests/test_main.py | 4 ++++ 4 files changed, 18 insertions(+), 4 deletions(-) diff --git a/src/bo4e_generator/__main__.py b/src/bo4e_generator/__main__.py index 9dedb9b..14f0217 100644 --- a/src/bo4e_generator/__main__.py +++ b/src/bo4e_generator/__main__.py @@ -6,7 +6,7 @@ import click from bo4e_generator.parser import create_init_files, generate_bo4e_schema -from bo4e_generator.schema import get_namespace +from bo4e_generator.schema import get_namespace, get_version def resolve_paths(input_directory: Path, output_directory: Path) -> tuple[Path, Path]: @@ -30,7 +30,7 @@ def generate_bo4e_schemas(input_directory: Path, output_directory: Path): result = generate_bo4e_schema(schema_metadata, namespace) schema_metadata.save(result) print(f"Generated {schema_metadata}") - create_init_files(output_directory) + create_init_files(output_directory, get_version(namespace)) print(f"Generated __init__.py files in {output_directory}") diff --git a/src/bo4e_generator/parser.py b/src/bo4e_generator/parser.py index 198a5e5..36dda0a 100644 --- a/src/bo4e_generator/parser.py +++ b/src/bo4e_generator/parser.py @@ -175,11 +175,13 @@ def relative(current_module: str, reference: str) -> Tuple[str, str]: datamodel_code_generator.parser.base.relative = relative -def create_init_files(output_path: Path) -> None: +def create_init_files(output_path: Path, version: str) -> None: """ Create __init__.py files in all subdirectories of the given output directory and in the directory itself. """ - (output_path / "__init__.py").touch() + (output_path / "__init__.py").write_text( + f'""" Contains information about the bo4e version """\n\n__version__ = "{version}"\n' + ) for directory in output_path.glob("**/"): (directory / "__init__.py").touch() diff --git a/src/bo4e_generator/schema.py b/src/bo4e_generator/schema.py index 358f707..908acf7 100644 --- a/src/bo4e_generator/schema.py +++ b/src/bo4e_generator/schema.py @@ -67,3 +67,11 @@ def get_namespace(input_directory: Path, output_directory: Path) -> dict[str, Sc class_name=class_name, ) return namespace + + +def get_version(namespace: dict[str, SchemaMetadata]) -> str: + """ + Get the version of the bo4e schemas. + """ + # The chosen class is arbitrary. All bo's and com's should contain the same version information. + return namespace["Angebot"].schema_parsed["properties"]["_version"]["default"] diff --git a/unittests/test_main.py b/unittests/test_main.py index 588966a..4a9b210 100644 --- a/unittests/test_main.py +++ b/unittests/test_main.py @@ -34,6 +34,10 @@ def test_main(self): assert issubclass(Angebot, BaseModel) assert "typ" in Angebot.model_fields + from .output.bo4e import __version__ # type: ignore[import-not-found] + + assert __version__ == "0.6.1rc13" + def test_single(self): os.chdir(BASE_DIR) namespace = get_namespace(INPUT_DIR.resolve(), OUTPUT_DIR.resolve())