@@ -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
4250def 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