Skip to content

Commit 8f7789f

Browse files
committed
feat: apply schema templates recursive
1 parent a336191 commit 8f7789f

1 file changed

Lines changed: 63 additions & 44 deletions

File tree

‎src/osw/core.py‎

Lines changed: 63 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,11 @@
2222

2323
import osw.model.entity as model
2424
from osw.model.static import OswBaseModel
25+
from osw.utils.oold import (
26+
AggregateGeneratedSchemasParam,
27+
AggregateGeneratedSchemasParamMode,
28+
aggregate_generated_schemas,
29+
)
2530
from osw.utils.templates import (
2631
compile_handlebars_template,
2732
eval_compiled_handlebars_template,
@@ -825,8 +830,6 @@ class _ApplyOverwriteParam(OswBaseModel):
825830
entity: OswBaseModel # actually model.Entity but this causes the "type" error
826831
policy: Union[OSW.OverwriteClassParam, OVERWRITE_CLASS_OPTIONS]
827832
namespace: Optional[str]
828-
meta_category_title: Optional[str]
829-
meta_category_template_str: Optional[str]
830833
remove_empty: Optional[bool] = True
831834
inplace: Optional[bool] = False
832835
debug: Optional[bool] = False
@@ -1035,7 +1038,7 @@ class StoreEntityParam(OswBaseModel):
10351038
"""ID to document the change. Entities within the same store_entity() call will
10361039
share the same change_id. This parameter can also be used to link multiple
10371040
store_entity() calls."""
1038-
meta_category_title: Optional[str] = "Category:Category"
1041+
meta_category_title: Optional[Union[str, List[str]]] = "Category:Category"
10391042
debug: Optional[bool] = False
10401043
_overwrite_per_class: Dict[str, Dict[str, OSW.OverwriteClassParam]] = (
10411044
PrivateAttr()
@@ -1108,25 +1111,35 @@ def store_entity(
11081111

11091112
max_index = len(param.entities)
11101113

1111-
meta_category = self.site.get_page(
1112-
WtSite.GetPageParam(titles=[param.meta_category_title])
1113-
).pages[0]
1114-
# ToDo: we have to do this iteratively to support meta categories inheritance
1115-
meta_category_template_str = meta_category.get_slot_content("schema_template")
1116-
meta_category_template = None
1114+
meta_category_templates = {}
11171115
if param.namespace == "Category":
1118-
if param.meta_category_title:
1119-
meta_category = self.site.get_page(
1120-
WtSite.GetPageParam(titles=[param.meta_category_title])
1121-
).pages[0]
1122-
meta_category_template_str = meta_category.get_slot_content(
1123-
"schema_template"
1124-
)
1125-
if meta_category_template_str:
1126-
meta_category_template = compile_handlebars_template(
1127-
meta_category_template_str
1116+
meta_category_titles = param.meta_category_title
1117+
if not isinstance(meta_category_titles, list):
1118+
meta_category_titles = [meta_category_titles]
1119+
meta_category_template_strs = {}
1120+
# We have to do this iteratively to support meta categories inheritance
1121+
while meta_category_titles is not None and len(meta_category_titles) > 0:
1122+
meta_categories = self.site.get_page(
1123+
WtSite.GetPageParam(titles=meta_category_titles)
1124+
).pages
1125+
for meta_category in meta_categories:
1126+
meta_category_template_strs[meta_category.title] = (
1127+
meta_category.get_slot_content("schema_template")
1128+
)
1129+
1130+
meta_category_titles = meta_category.get_slot_content("jsondata").get(
1131+
"subclass_of"
11281132
)
11291133

1134+
for title in meta_category_template_strs.keys():
1135+
meta_category_template_str = meta_category_template_strs[title]
1136+
if meta_category_template_str:
1137+
meta_category_templates[title] = compile_handlebars_template(
1138+
meta_category_template_str
1139+
)
1140+
# inverse order to have the most generic template first
1141+
meta_category_templates = dict(reversed(meta_category_templates.items()))
1142+
11301143
def store_entity_(
11311144
entity_: model.Entity,
11321145
namespace_: str = None,
@@ -1148,41 +1161,47 @@ def store_entity_(
11481161
entity=entity_,
11491162
namespace=namespace_,
11501163
policy=overwrite_class_param,
1151-
meta_category_template_str=meta_category_template_str,
11521164
remove_empty=param.remove_empty,
11531165
debug=param.debug,
11541166
)
11551167
)
1156-
if meta_category_template:
1168+
if len(meta_category_templates.keys()) > 0:
1169+
generated_schemas = {}
11571170
try:
11581171
jsondata = page.get_slot_content("jsondata")
11591172
if param.remove_empty:
11601173
remove_empty(jsondata)
1161-
schema_str = eval_compiled_handlebars_template(
1162-
meta_category_template,
1163-
jsondata,
1164-
{
1165-
"_page_title": entity_title, # Legacy
1166-
"_current_subject_": entity_title,
1167-
},
1168-
)
1169-
schema = json.loads(schema_str)
1170-
# Put generated schema in definitions section,
1171-
# currently only enabled for Characteristics
1172-
if hasattr(model, "CharacteristicType") and isinstance(
1173-
entity_, model.CharacteristicType
1174-
):
1175-
new_schema = {
1176-
"$defs": {"generated": schema},
1177-
"allOf": [{"$ref": "#/$defs/generated"}],
1178-
"@context": schema.pop("@context", None),
1179-
"title": schema.pop("title", ""),
1180-
}
1181-
schema["title"] = "Generated" + new_schema["title"]
1182-
schema = new_schema
1183-
page.set_slot_content("jsonschema", new_schema)
1174+
1175+
for key in meta_category_templates:
1176+
meta_category_template = meta_category_templates[key]
1177+
schema_str = eval_compiled_handlebars_template(
1178+
meta_category_template,
1179+
jsondata,
1180+
{
1181+
"_page_title": entity_title, # Legacy
1182+
"_current_subject_": entity_title,
1183+
},
1184+
)
1185+
generated_schemas[key] = json.loads(schema_str)
11841186
except Exception as e:
11851187
print(f"Schema generation from template failed for {entity_}: {e}")
1188+
1189+
mode = AggregateGeneratedSchemasParamMode.ROOT_LEVEL
1190+
# Put generated schema in definitions section,
1191+
# currently only enabled for Characteristics
1192+
if hasattr(model, "CharacteristicType") and isinstance(
1193+
entity_, model.CharacteristicType
1194+
):
1195+
mode = AggregateGeneratedSchemasParamMode.DEFINITIONS_SECTION
1196+
1197+
new_schema = aggregate_generated_schemas(
1198+
AggregateGeneratedSchemasParam(
1199+
schema=page.get_slot_content("jsonschema"),
1200+
generated_schemas=generated_schemas,
1201+
mode=mode,
1202+
)
1203+
).aggregated_schema
1204+
page.set_slot_content("jsonschema", new_schema)
11861205
page.edit() # will set page.changed if the content of the page has changed
11871206
if page.changed:
11881207
if index is None:

0 commit comments

Comments
 (0)