Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support Pydantic v1 output #12

Merged
merged 3 commits into from
Nov 10, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 12 additions & 4 deletions src/bo4e_generator/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,13 @@
from bo4e_generator.schema import get_namespace


def generate_bo4e_schemas(input_directory: Path, output_directory: Path):
def generate_bo4e_schemas(input_directory: Path, output_directory: Path, pydantic_v1: bool = False):
"""
Generate all BO4E schemas from the given input directory and save them in the given output directory.
"""
namespace = get_namespace(input_directory, output_directory)
for schema_metadata in namespace.values():
result = generate_bo4e_schema(schema_metadata, namespace)
result = generate_bo4e_schema(schema_metadata, namespace, pydantic_v1)
schema_metadata.save(result)
print(f"Generated {schema_metadata}")
create_init_files(output_directory)
Expand All @@ -37,13 +37,21 @@ def generate_bo4e_schemas(input_directory: Path, output_directory: Path):
help="Output directory for the generated python files.",
required=True,
)
@click.option(
"--pydantic-v1/--pydantic-v2",
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

wie gibt man das nun an?
kann man entweder --pydantic-v1 oder -pydantic-v2 angeben?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Genau. Und der default ist v2

"-p",
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

und wie würde man in der abgekürzten Form angeben?
-p 1 vs -p 2 oder -p v1 ?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is_flag=True,
help="Generate pydantic v1 models instead of pydantic v2 models.",
required=False,
default=False,
)
@click.help_option()
@click.version_option(package_name="BO4E-Python-Generator")
def main(input_dir: Path, output_dir: Path):
def main(input_dir: Path, output_dir: Path, pydantic_v1: bool):
"""
CLI entry point for the bo4e-generator.
"""
generate_bo4e_schemas(input_dir, output_dir)
generate_bo4e_schemas(input_dir, output_dir, pydantic_v1)


if __name__ == "__main__":
Expand Down
10 changes: 7 additions & 3 deletions src/bo4e_generator/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -191,12 +191,16 @@ def remove_future_import(python_code: str) -> str:
return re.sub(r"from __future__ import annotations\n\n", "", python_code)


def generate_bo4e_schema(schema_metadata: SchemaMetadata, namespace: dict[str, SchemaMetadata]) -> str:
def generate_bo4e_schema(
schema_metadata: SchemaMetadata, namespace: dict[str, SchemaMetadata], pydantic_v1: bool = False
) -> str:
"""
Generate a pydantic v2 model from the given schema. Returns the resulting code as string.
"""
data_model_types = get_bo4e_data_model_types(
DataModelType.PydanticV2BaseModel, target_python_version=PythonVersion.PY_311, namespace=namespace
DataModelType.PydanticBaseModel if pydantic_v1 else DataModelType.PydanticV2BaseModel,
target_python_version=PythonVersion.PY_311,
namespace=namespace,
)
monkey_patch_field_name_resolver()
monkey_patch_relative_import()
Expand All @@ -208,7 +212,7 @@ def generate_bo4e_schema(schema_metadata: SchemaMetadata, namespace: dict[str, S
data_model_field_type=data_model_types.field_model,
data_type_manager_type=data_model_types.data_type_manager,
dump_resolve_reference_action=data_model_types.dump_resolve_reference_action,
use_annotated=True,
use_annotated=not pydantic_v1,
use_double_quotes=True,
use_schema_description=True,
use_subclass_enum=True,
Expand Down
4 changes: 2 additions & 2 deletions unittests/test_main.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@
class TestMain:
def test_main(self):
runner = CliRunner()
result = runner.invoke(main, ["--input-dir", str(INPUT_DIR), "--output-dir", str(OUTPUT_DIR)])
# generate_bo4e_schemas(input_directory=INPUT_DIR, output_directory=OUTPUT_DIR)
result = runner.invoke(main, ["--input-dir", str(INPUT_DIR), "--output-dir", str(OUTPUT_DIR), "--pydantic-v2"])

assert result.exit_code == 0, f"Error: {result.output}"
assert (OUTPUT_DIR / "bo" / "angebot.py").exists()
assert (OUTPUT_DIR / "bo" / "preisblatt_netznutzung.py").exists()
Expand Down