33import importlib
44import json
55import os
6+ import pathlib
67import platform
78import re
89import sys
1213from uuid import UUID , uuid4
1314from warnings import warn
1415
16+ import datamodel_code_generator
1517import rdflib
1618from jsonpath_ng .ext import parse
1719from mwclient .client import Site
@@ -330,6 +332,8 @@ class FetchSchemaParam(BaseModel):
330332 "replace"
331333 # type 'FetchSchemaMode' requires: 'from __future__ import annotations'
332334 )
335+ legacy_generator : Optional [bool ] = False
336+ """uses legacy command line for code generation if true"""
333337
334338 def fetch_schema (self , fetchSchemaParam : FetchSchemaParam = None ) -> None :
335339 """Loads the given schemas from the OSW instance and autogenerates python
@@ -348,7 +352,11 @@ def fetch_schema(self, fetchSchemaParam: FetchSchemaParam = None) -> None:
348352 if not first : # 'replace' makes only sense for the first schema
349353 mode = "append"
350354 self ._fetch_schema (
351- OSW ._FetchSchemaParam (schema_title = schema_title , mode = mode )
355+ OSW ._FetchSchemaParam (
356+ schema_title = schema_title ,
357+ mode = mode ,
358+ legacy_generator = fetchSchemaParam .legacy_generator ,
359+ )
352360 )
353361 first = False
354362
@@ -372,6 +380,8 @@ class _FetchSchemaParam(BaseModel):
372380 "replace"
373381 # type 'FetchSchemaMode' requires: 'from __future__ import annotations'
374382 )
383+ legacy_generator : Optional [bool ] = False
384+ """uses legacy command line for code generation if true"""
375385
376386 def _fetch_schema (self , fetchSchemaParam : _FetchSchemaParam = None ) -> None :
377387 """Loads the given schema from the OSW instance and autogenerates python
@@ -455,41 +465,61 @@ def _fetch_schema(self, fetchSchemaParam: _FetchSchemaParam = None) -> None:
455465 result_model_path = os .path .join (model_dir_path , "entity.py" )
456466 temp_model_path = os .path .join (model_dir_path , "temp.py" )
457467 if root :
458- exec_name = "datamodel-codegen"
459- # default: assume datamodel-codegen is in PATH
460- exec_path = exec_name
461- if platform .system () == "Windows" :
462- exec_name += ".exe"
463- exec_path = os .path .join (
464- os .path .dirname (os .path .abspath (sys .executable )), exec_name
465- )
466- if not os .path .isfile (exec_path ):
468+ if fetchSchemaParam .legacy_generator :
469+ exec_name = "datamodel-codegen"
470+ # default: assume datamodel-codegen is in PATH
471+ exec_path = exec_name
472+ if platform .system () == "Windows" :
473+ exec_name += ".exe"
467474 exec_path = os .path .join (
468- os .path .dirname (os .path .abspath (sys .executable )),
469- "Scripts" ,
470- exec_name ,
475+ os .path .dirname (os .path .abspath (sys .executable )), exec_name
471476 )
472- if not os .path .isfile (exec_path ):
473- print ("Error: datamodel-codegen not found" )
474- return
475- os .system (
476- f"{ exec_path } \
477- --input { schema_path } \
478- --input-file-type jsonschema \
479- --output { temp_model_path } \
480- --base-class osw.model.static.OswBaseModel \
481- --use-default \
482- --use-unique-items-as-set \
483- --enum-field-as-literal all \
484- --use-title-as-name \
485- --use-schema-description \
486- --use-field-description \
487- --encoding utf-8 \
488- --use-double-quotes \
489- --collapse-root-models \
490- --reuse-model \
491- "
492- )
477+ if not os .path .isfile (exec_path ):
478+ exec_path = os .path .join (
479+ os .path .dirname (os .path .abspath (sys .executable )),
480+ "Scripts" ,
481+ exec_name ,
482+ )
483+ if not os .path .isfile (exec_path ):
484+ print ("Error: datamodel-codegen not found" )
485+ return
486+ os .system (
487+ f"{ exec_path } \
488+ --input { schema_path } \
489+ --input-file-type jsonschema \
490+ --output { temp_model_path } \
491+ --base-class osw.model.static.OswBaseModel \
492+ --use-default \
493+ --use-unique-items-as-set \
494+ --enum-field-as-literal all \
495+ --use-title-as-name \
496+ --use-schema-description \
497+ --use-field-description \
498+ --encoding utf-8 \
499+ --use-double-quotes \
500+ --collapse-root-models \
501+ --reuse-model \
502+ "
503+ )
504+ else :
505+ datamodel_code_generator .generate (
506+ input_ = pathlib .Path (schema_path ),
507+ input_file_type = "jsonschema" ,
508+ output = pathlib .Path (temp_model_path ),
509+ base_class = "osw.model.static.OswBaseModel" ,
510+ # use_default=True,
511+ apply_default_values_for_required_fields = True ,
512+ use_unique_items_as_set = True ,
513+ enum_field_as_literal = datamodel_code_generator .LiteralType .All ,
514+ use_title_as_name = True ,
515+ use_schema_description = True ,
516+ use_field_description = True ,
517+ encoding = "utf-8" ,
518+ use_double_quotes = True ,
519+ collapse_root_models = True ,
520+ reuse_model = True ,
521+ )
522+
493523 # see https://koxudaxi.github.io/datamodel-code-generator/
494524 # --base-class OswBaseModel: use a custom base class
495525 # --custom-template-dir src/model/template_data/
0 commit comments