1818import rdflib
1919from jsonpath_ng .ext import parse
2020from mwclient .client import Site
21+ from oold .generator import Generator
22+ from oold .model .v1 import (
23+ ResolveParam ,
24+ Resolver ,
25+ ResolveResult ,
26+ SetResolverParam ,
27+ set_resolver ,
28+ )
2129from pydantic import PydanticDeprecatedSince20
2230from pydantic .v1 import BaseModel , Field , PrivateAttr , create_model , validator
2331from pyld import jsonld
@@ -99,6 +107,30 @@ class Config:
99107
100108 site : WtSite
101109
110+ def __init__ (self , ** data : Any ):
111+ super ().__init__ (** data )
112+
113+ # implement resolver backend with osw.load_entity
114+ class MyResolver (Resolver ):
115+
116+ osw_obj : OSW
117+
118+ def resolve (self , request : ResolveParam ):
119+ print ("RESOLVE" , request )
120+ entities = self .osw_obj .load_entity (
121+ OSW .LoadEntityParam (titles = request .iris )
122+ ).entities
123+ # create a dict with request.iris as keys and the loaded entities as values
124+ # by iterating over both lists
125+ nodes = {}
126+ for iri , entity in zip (request .iris , entities ):
127+ nodes [iri ] = entity
128+ return ResolveResult (nodes = nodes )
129+
130+ r = MyResolver (osw_obj = self )
131+ set_resolver (SetResolverParam (iri = "Item" , resolver = r ))
132+ set_resolver (SetResolverParam (iri = "Category" , resolver = r ))
133+
102134 @property
103135 def mw_site (self ) -> Site :
104136 """Returns the mwclient Site object of the OSW instance."""
@@ -396,6 +428,8 @@ class _FetchSchemaParam(BaseModel):
396428 )
397429 legacy_generator : Optional [bool ] = False
398430 """uses legacy command line for code generation if true"""
431+ fetched_schema_titles : Optional [List [str ]] = []
432+ """keep track of fetched schema titles to prevent recursion"""
399433
400434 def _fetch_schema (self , fetchSchemaParam : _FetchSchemaParam = None ) -> None :
401435 """Loads the given schema from the OSW instance and autogenerates python
@@ -411,6 +445,7 @@ def _fetch_schema(self, fetchSchemaParam: _FetchSchemaParam = None) -> None:
411445 if fetchSchemaParam is None :
412446 fetchSchemaParam = OSW ._FetchSchemaParam ()
413447 schema_title = fetchSchemaParam .schema_title
448+ fetchSchemaParam .fetched_schema_titles .append (schema_title )
414449 root = fetchSchemaParam .root
415450 schema_name = schema_title .split (":" )[- 1 ]
416451 page = self .site .get_page (WtSite .GetPageParam (titles = [schema_title ])).pages [0 ]
@@ -433,6 +468,12 @@ def _fetch_schema(self, fetchSchemaParam: _FetchSchemaParam = None) -> None:
433468 if (schema_str is None ) or (schema_str == "" ):
434469 print (f"Error: Schema { schema_title } does not exist" )
435470 schema_str = "{}" # empty schema to make reference work
471+
472+ generator = Generator ()
473+ schemas_for_preprocessing = [json .loads (schema_str )]
474+ generator .preprocess (schemas_for_preprocessing )
475+ schema_str = json .dumps (schemas_for_preprocessing [0 ])
476+
436477 schema = json .loads (
437478 schema_str .replace ("$ref" , "dollarref" ).replace (
438479 # '$' is a special char for root object in jsonpath
@@ -461,10 +502,12 @@ def _fetch_schema(self, fetchSchemaParam: _FetchSchemaParam = None) -> None:
461502 # print(f"replace {match.value} with {value}")
462503 if (
463504 ref_schema_title != schema_title
505+ and ref_schema_title not in fetchSchemaParam .fetched_schema_titles
464506 ): # prevent recursion in case of self references
465- self ._fetch_schema (
466- OSW ._FetchSchemaParam (schema_title = ref_schema_title , root = False )
467- ) # resolve references recursive
507+ _param = fetchSchemaParam .copy ()
508+ _param .root = False
509+ _param .schema_title = ref_schema_title
510+ self ._fetch_schema (_param ) # resolve references recursive
468511
469512 model_dir_path = os .path .join (
470513 os .path .dirname (os .path .abspath (__file__ )), "model"
@@ -530,14 +573,19 @@ def _fetch_schema(self, fetchSchemaParam: _FetchSchemaParam = None) -> None:
530573 # use_default=True,
531574 apply_default_values_for_required_fields = True ,
532575 use_unique_items_as_set = True ,
533- enum_field_as_literal = datamodel_code_generator .LiteralType .All ,
576+ # enum_field_as_literal=datamodel_code_generator.LiteralType.All,
577+ enum_field_as_literal = "all" ,
578+ # will create MyEnum(str, Enum) instead of MyEnum(Enum)
579+ use_subclass_enum = True ,
580+ set_default_enum_member = True ,
534581 use_title_as_name = True ,
535582 use_schema_description = True ,
536583 use_field_description = True ,
537584 encoding = "utf-8" ,
538585 use_double_quotes = True ,
539586 collapse_root_models = True ,
540587 reuse_model = True ,
588+ field_include_all_keys = True ,
541589 )
542590 warnings .filterwarnings ("default" , category = PydanticDeprecatedSince20 )
543591
0 commit comments