Skip to content

Commit

Permalink
Add __version__ to bo4e/__init__.py (#11)
Browse files Browse the repository at this point in the history
  • Loading branch information
lord-haffi authored Nov 10, 2023
1 parent 335b45a commit 5c7e0d7
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 4 deletions.
4 changes: 2 additions & 2 deletions src/bo4e_generator/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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]:
Expand All @@ -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}")


Expand Down
6 changes: 4 additions & 2 deletions src/bo4e_generator/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand Down
8 changes: 8 additions & 0 deletions src/bo4e_generator/schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"]
4 changes: 4 additions & 0 deletions unittests/test_main.py
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Expand Down

0 comments on commit 5c7e0d7

Please sign in to comment.