1919
2020import osw .model .entity as model
2121from osw .model .static import OswBaseModel
22+ from osw .utils .oold import (
23+ AggregateGeneratedSchemasParam ,
24+ AggregateGeneratedSchemasParamMode ,
25+ aggregate_generated_schemas ,
26+ )
2227from osw .utils .templates import (
2328 compile_handlebars_template ,
2429 eval_compiled_handlebars_template ,
@@ -779,8 +784,6 @@ class _ApplyOverwriteParam(OswBaseModel):
779784 entity : OswBaseModel # actually model.Entity but this causes the "type" error
780785 policy : Union [OSW .OverwriteClassParam , OVERWRITE_CLASS_OPTIONS ]
781786 namespace : Optional [str ]
782- meta_category_title : Optional [str ]
783- meta_category_template_str : Optional [str ]
784787 remove_empty : Optional [bool ] = True
785788 inplace : Optional [bool ] = False
786789 debug : Optional [bool ] = False
@@ -988,7 +991,7 @@ class StoreEntityParam(OswBaseModel):
988991 """ID to document the change. Entities within the same store_entity() call will
989992 share the same change_id. This parameter can also be used to link multiple
990993 store_entity() calls."""
991- meta_category_title : Optional [str ] = "Category:Category"
994+ meta_category_title : Optional [Union [ str , List [ str ]] ] = "Category:Category"
992995 debug : Optional [bool ] = False
993996 _overwrite_per_class : Dict [str , Dict [str , OSW .OverwriteClassParam ]] = (
994997 PrivateAttr ()
@@ -1061,25 +1064,35 @@ def store_entity(
10611064
10621065 max_index = len (param .entities )
10631066
1064- meta_category = self .site .get_page (
1065- WtSite .GetPageParam (titles = [param .meta_category_title ])
1066- ).pages [0 ]
1067- # ToDo: we have to do this iteratively to support meta categories inheritance
1068- meta_category_template_str = meta_category .get_slot_content ("schema_template" )
1069- meta_category_template = None
1067+ meta_category_templates = {}
10701068 if param .namespace == "Category" :
1071- if param .meta_category_title :
1072- meta_category = self .site .get_page (
1073- WtSite .GetPageParam (titles = [param .meta_category_title ])
1074- ).pages [0 ]
1075- meta_category_template_str = meta_category .get_slot_content (
1076- "schema_template"
1077- )
1078- if meta_category_template_str :
1079- meta_category_template = compile_handlebars_template (
1080- meta_category_template_str
1069+ meta_category_titles = param .meta_category_title
1070+ if not isinstance (meta_category_titles , list ):
1071+ meta_category_titles = [meta_category_titles ]
1072+ meta_category_template_strs = {}
1073+ # We have to do this iteratively to support meta categories inheritance
1074+ while meta_category_titles is not None and len (meta_category_titles ) > 0 :
1075+ meta_categories = self .site .get_page (
1076+ WtSite .GetPageParam (titles = meta_category_titles )
1077+ ).pages
1078+ for meta_category in meta_categories :
1079+ meta_category_template_strs [meta_category .title ] = (
1080+ meta_category .get_slot_content ("schema_template" )
1081+ )
1082+
1083+ meta_category_titles = meta_category .get_slot_content ("jsondata" ).get (
1084+ "subclass_of"
10811085 )
10821086
1087+ for title in meta_category_template_strs .keys ():
1088+ meta_category_template_str = meta_category_template_strs [title ]
1089+ if meta_category_template_str :
1090+ meta_category_templates [title ] = compile_handlebars_template (
1091+ meta_category_template_str
1092+ )
1093+ # inverse order to have the most generic template first
1094+ meta_category_templates = dict (reversed (meta_category_templates .items ()))
1095+
10831096 def store_entity_ (
10841097 entity_ : model .Entity ,
10851098 namespace_ : str = None ,
@@ -1101,41 +1114,47 @@ def store_entity_(
11011114 entity = entity_ ,
11021115 namespace = namespace_ ,
11031116 policy = overwrite_class_param ,
1104- meta_category_template_str = meta_category_template_str ,
11051117 remove_empty = param .remove_empty ,
11061118 debug = param .debug ,
11071119 )
11081120 )
1109- if meta_category_template :
1121+ if len (meta_category_templates .keys ()) > 0 :
1122+ generated_schemas = {}
11101123 try :
11111124 jsondata = page .get_slot_content ("jsondata" )
11121125 if param .remove_empty :
11131126 remove_empty (jsondata )
1114- schema_str = eval_compiled_handlebars_template (
1115- meta_category_template ,
1116- jsondata ,
1117- {
1118- "_page_title" : entity_title , # Legacy
1119- "_current_subject_" : entity_title ,
1120- },
1121- )
1122- schema = json .loads (schema_str )
1123- # Put generated schema in definitions section,
1124- # currently only enabled for Characteristics
1125- if hasattr (model , "CharacteristicType" ) and isinstance (
1126- entity_ , model .CharacteristicType
1127- ):
1128- new_schema = {
1129- "$defs" : {"generated" : schema },
1130- "allOf" : [{"$ref" : "#/$defs/generated" }],
1131- "@context" : schema .pop ("@context" , None ),
1132- "title" : schema .pop ("title" , "" ),
1133- }
1134- schema ["title" ] = "Generated" + new_schema ["title" ]
1135- schema = new_schema
1136- page .set_slot_content ("jsonschema" , new_schema )
1127+
1128+ for key in meta_category_templates :
1129+ meta_category_template = meta_category_templates [key ]
1130+ schema_str = eval_compiled_handlebars_template (
1131+ meta_category_template ,
1132+ jsondata ,
1133+ {
1134+ "_page_title" : entity_title , # Legacy
1135+ "_current_subject_" : entity_title ,
1136+ },
1137+ )
1138+ generated_schemas [key ] = json .loads (schema_str )
11371139 except Exception as e :
11381140 print (f"Schema generation from template failed for { entity_ } : { e } " )
1141+
1142+ mode = AggregateGeneratedSchemasParamMode .ROOT_LEVEL
1143+ # Put generated schema in definitions section,
1144+ # currently only enabled for Characteristics
1145+ if hasattr (model , "CharacteristicType" ) and isinstance (
1146+ entity_ , model .CharacteristicType
1147+ ):
1148+ mode = AggregateGeneratedSchemasParamMode .DEFINITIONS_SECTION
1149+
1150+ new_schema = aggregate_generated_schemas (
1151+ AggregateGeneratedSchemasParam (
1152+ schema = page .get_slot_content ("jsonschema" ),
1153+ generated_schemas = generated_schemas ,
1154+ mode = mode ,
1155+ )
1156+ ).aggregated_schema
1157+ page .set_slot_content ("jsonschema" , new_schema )
11391158 page .edit () # will set page.changed if the content of the page has changed
11401159 if page .changed :
11411160 if index is None :
0 commit comments