Skip to content

Commit 96e50ac

Browse files
authored
Compatibility patch for ZusatzAttribut (#53)
* Compatibility patch for `ZusatzAttribut` * 📄 * 🚨🩹
1 parent 885467f commit 96e50ac

2 files changed

Lines changed: 21 additions & 16 deletions

File tree

src/bo4e_generator/parser.py

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ def get_bo4e_data_model_types(
3838
def _module_path(self) -> list[str]:
3939
if self.name not in namespace:
4040
raise ValueError(f"Model not in namespace: {self.name}")
41-
return [namespace[self.name].pkg, namespace[self.name].module_name]
41+
return list(namespace[self.name].module_path)
4242

4343
@property # type: ignore[misc]
4444
# "property" used with a non-method
@@ -156,9 +156,7 @@ def bo4e_init_file_content(namespace: dict[str, SchemaMetadata], version: str) -
156156
init_file_content += "]\n\n"
157157

158158
for schema_metadata in namespace.values():
159-
init_file_content += (
160-
f"from .{schema_metadata.pkg}.{schema_metadata.module_name} import {schema_metadata.class_name}\n"
161-
)
159+
init_file_content += f"from .{'.'.join(schema_metadata.module_path)} import {schema_metadata.class_name}\n"
162160
init_file_content += "\nfrom .__version__ import __version__\n"
163161

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

221218
if module_path not in parse_result:
222219
raise KeyError(

src/bo4e_generator/schema.py

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,11 @@ class SchemaMetadata(BaseModel):
2020
schema_text: str
2121
schema_parsed: SchemaType
2222
class_name: str
23-
pkg: str
24-
"e.g. 'bo'"
2523
input_file: Path
2624
output_file: Path
2725
"The output file will be a relative path"
28-
module_name: str
29-
"e.g. 'preisblatt_netznutzung"
26+
module_path: tuple[str, ...]
27+
"e.g. ('bo', 'preisblatt_netznutzung') or ('zusatz_attribut')"
3028

3129
def save(self, content: str):
3230
"""
@@ -35,8 +33,18 @@ def save(self, content: str):
3533
self.output_file.parent.mkdir(parents=True, exist_ok=True)
3634
self.output_file.write_text(content)
3735

36+
@property
37+
def module_name(self) -> str:
38+
"""e.g. 'preisblatt_netznutzung' or 'zusatz_attribut'"""
39+
return self.module_path[-1]
40+
41+
@property
42+
def module_path_with_extension(self) -> tuple[str, ...]:
43+
"""e.g. ('bo', 'preisblatt_netznutzung.py') or ('zusatz_attribut.py')"""
44+
return *self.module_path[:-1], f"{self.module_path[-1]}.py"
45+
3846
def __str__(self):
39-
return f"{self.pkg}.{self.class_name}"
47+
return ".".join(self.module_path)
4048

4149

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

5563
namespace: dict[str, SchemaMetadata] = {}
5664
for file_path in input_directory.rglob("*.json"):
65+
relative_path = file_path.relative_to(input_directory)
66+
module_path = tuple(camel_to_snake(part) for part in relative_path.with_suffix("").parts)
5767
schema_text = file_path.read_text()
5868
schema_parsed = json.loads(schema_text)
5969
class_name = schema_parsed["title"].replace(" ", "_")
60-
module_name = camel_to_snake(class_name)
6170

6271
namespace[class_name] = SchemaMetadata(
63-
pkg=file_path.parent.name,
64-
module_name=module_name,
72+
module_path=module_path,
6573
input_file=file_path,
66-
output_file=file_path.relative_to(input_directory).with_name(f"{module_name}.py"),
74+
output_file=Path(*module_path).with_suffix(".py"),
6775
schema_text=schema_text,
6876
schema_parsed=schema_parsed,
6977
class_name=class_name,

0 commit comments

Comments
 (0)