From 22dfede66476e92842be46c855084bd5af138a6f Mon Sep 17 00:00:00 2001 From: Leon Haffmans <49658102+lord-haffi@users.noreply.github.com> Date: Mon, 4 Dec 2023 09:19:36 +0100 Subject: [PATCH] Create better `__init__.py` file (#24) * Default value for BO4E-Versions without `_version` * Create better `__init__.py` file --- src/bo4e_generator/__main__.py | 6 ++++-- src/bo4e_generator/parser.py | 35 +++++++++++++++++++++++++++++++++- 2 files changed, 38 insertions(+), 3 deletions(-) diff --git a/src/bo4e_generator/__main__.py b/src/bo4e_generator/__main__.py index 57f1d6a..c006099 100644 --- a/src/bo4e_generator/__main__.py +++ b/src/bo4e_generator/__main__.py @@ -5,7 +5,7 @@ import click -from bo4e_generator.parser import bo4e_init_file_content, parse_bo4e_schemas +from bo4e_generator.parser import bo4e_init_file_content, bo4e_version_file_content, parse_bo4e_schemas from bo4e_generator.schema import get_namespace, get_version @@ -27,7 +27,9 @@ def generate_bo4e_schemas(input_directory: Path, output_directory: Path, pydanti 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) - file_contents[Path("__init__.py")] = bo4e_init_file_content(get_version(namespace)) + version = get_version(namespace) + file_contents[Path("__version__.py")] = bo4e_version_file_content(version) + file_contents[Path("__init__.py")] = bo4e_init_file_content(namespace, version) for relative_file_path, file_content in file_contents.items(): file_path = output_directory / relative_file_path file_path.parent.mkdir(parents=True, exist_ok=True) diff --git a/src/bo4e_generator/parser.py b/src/bo4e_generator/parser.py index 875d40c..9248b38 100644 --- a/src/bo4e_generator/parser.py +++ b/src/bo4e_generator/parser.py @@ -104,13 +104,46 @@ def relative(current_module: str, reference: str) -> Tuple[str, str]: datamodel_code_generator.parser.base.relative = relative -def bo4e_init_file_content(version: str) -> str: +def bo4e_version_file_content(version: str) -> str: """ Create __init__.py files in all subdirectories of the given output directory and in the directory itself. """ return f'""" Contains information about the bo4e version """\n\n__version__ = "{version}"\n' +INIT_FILE_COMMENT = ''' +""" +BO4E v{version} - Generated Python implementation of the BO4E standard + +BO4E is a standard for the exchange of business objects in the energy industry. +All our software used to generate this BO4E-implementation is open-source and released under the Apache-2.0 license. + +The BO4E version can be queried using `bo4e.__version__`. +""" +''' + + +def bo4e_init_file_content(namespace: dict[str, SchemaMetadata], version: str) -> str: + """ + Create __init__.py files in all subdirectories of the given output directory and in the directory itself. + """ + init_file_content = INIT_FILE_COMMENT.strip().format(version=version) + + init_file_content += "\n\n__all__ = [\n" + for class_name in namespace: + init_file_content += f' "{class_name}",\n' + init_file_content += ' "__version__",\n' + init_file_content += "]\n\n" + + for schema_metadata in namespace.values(): + init_file_content += ( + f"from .{schema_metadata.pkg}.{schema_metadata.module_name} import {schema_metadata.class_name}\n" + ) + init_file_content += "\nfrom .__version__ import __version__\n" + + return init_file_content + + def remove_future_import(python_code: str) -> str: """ Remove the future import from the generated code.