Skip to content

Commit

Permalink
Compatibility patch for ZusatzAttribut (#53)
Browse files Browse the repository at this point in the history
* Compatibility patch for `ZusatzAttribut`

* 📄

* 🚨🩹
  • Loading branch information
lord-haffi authored Feb 15, 2024
1 parent 885467f commit 96e50ac
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 16 deletions.
11 changes: 4 additions & 7 deletions src/bo4e_generator/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ def get_bo4e_data_model_types(
def _module_path(self) -> list[str]:
if self.name not in namespace:
raise ValueError(f"Model not in namespace: {self.name}")
return [namespace[self.name].pkg, namespace[self.name].module_name]
return list(namespace[self.name].module_path)

@property # type: ignore[misc]
# "property" used with a non-method
Expand Down Expand Up @@ -156,9 +156,7 @@ def bo4e_init_file_content(namespace: dict[str, SchemaMetadata], version: str) -
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 += f"from .{'.'.join(schema_metadata.module_path)} import {schema_metadata.class_name}\n"
init_file_content += "\nfrom .__version__ import __version__\n"

return init_file_content
Expand Down Expand Up @@ -212,11 +210,10 @@ def parse_bo4e_schemas(
raise ValueError(f"Unexpected type of parse result: {type(parse_result)}")
file_contents = {}
for schema_metadata in namespace.values():
module_path = schema_metadata.module_path_with_extension
if schema_metadata.module_name.startswith("_"):
# Because somehow the generator uses the prefix also on the module name. Don't know why.
module_path = (schema_metadata.pkg, f"field{schema_metadata.module_name}.py")
else:
module_path = (schema_metadata.pkg, f"{schema_metadata.module_name}.py")
module_path = *module_path[:-1], f"field{module_path[-1]}"

if module_path not in parse_result:
raise KeyError(
Expand Down
26 changes: 17 additions & 9 deletions src/bo4e_generator/schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,11 @@ class SchemaMetadata(BaseModel):
schema_text: str
schema_parsed: SchemaType
class_name: str
pkg: str
"e.g. 'bo'"
input_file: Path
output_file: Path
"The output file will be a relative path"
module_name: str
"e.g. 'preisblatt_netznutzung"
module_path: tuple[str, ...]
"e.g. ('bo', 'preisblatt_netznutzung') or ('zusatz_attribut')"

def save(self, content: str):
"""
Expand All @@ -35,8 +33,18 @@ def save(self, content: str):
self.output_file.parent.mkdir(parents=True, exist_ok=True)
self.output_file.write_text(content)

@property
def module_name(self) -> str:
"""e.g. 'preisblatt_netznutzung' or 'zusatz_attribut'"""
return self.module_path[-1]

@property
def module_path_with_extension(self) -> tuple[str, ...]:
"""e.g. ('bo', 'preisblatt_netznutzung.py') or ('zusatz_attribut.py')"""
return *self.module_path[:-1], f"{self.module_path[-1]}.py"

def __str__(self):
return f"{self.pkg}.{self.class_name}"
return ".".join(self.module_path)


def camel_to_snake(name: str) -> str:
Expand All @@ -54,16 +62,16 @@ def get_namespace(input_directory: Path) -> dict[str, SchemaMetadata]:

namespace: dict[str, SchemaMetadata] = {}
for file_path in input_directory.rglob("*.json"):
relative_path = file_path.relative_to(input_directory)
module_path = tuple(camel_to_snake(part) for part in relative_path.with_suffix("").parts)
schema_text = file_path.read_text()
schema_parsed = json.loads(schema_text)
class_name = schema_parsed["title"].replace(" ", "_")
module_name = camel_to_snake(class_name)

namespace[class_name] = SchemaMetadata(
pkg=file_path.parent.name,
module_name=module_name,
module_path=module_path,
input_file=file_path,
output_file=file_path.relative_to(input_directory).with_name(f"{module_name}.py"),
output_file=Path(*module_path).with_suffix(".py"),
schema_text=schema_text,
schema_parsed=schema_parsed,
class_name=class_name,
Expand Down

0 comments on commit 96e50ac

Please sign in to comment.