Skip to content

Commit a7909c0

Browse files
authored
Merge pull request #102 from OpenSemanticLab/feat-add-support-for-recursive-schema-templates
Feat add support for recursive schema templates
2 parents 3e493aa + 034ec76 commit a7909c0

5 files changed

Lines changed: 809 additions & 48 deletions

File tree

‎src/osw/core.py‎

Lines changed: 64 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,12 @@
2323
import osw.model.entity as model
2424
from osw.defaults import params as default_params
2525
from osw.model.static import OswBaseModel
26+
from osw.utils.oold import (
27+
AggregateGeneratedSchemasParam,
28+
AggregateGeneratedSchemasParamMode,
29+
aggregate_generated_schemas,
30+
escape_json_strings,
31+
)
2632
from osw.utils.templates import (
2733
compile_handlebars_template,
2834
eval_compiled_handlebars_template,
@@ -905,8 +911,6 @@ class _ApplyOverwriteParam(OswBaseModel):
905911
entity: OswBaseModel # actually model.Entity but this causes the "type" error
906912
policy: Union[OSW.OverwriteClassParam, OVERWRITE_CLASS_OPTIONS]
907913
namespace: Optional[str]
908-
meta_category_title: Optional[str]
909-
meta_category_template_str: Optional[str]
910914
remove_empty: Optional[bool] = True
911915
inplace: Optional[bool] = False
912916
debug: Optional[bool] = False
@@ -1115,7 +1119,7 @@ class StoreEntityParam(OswBaseModel):
11151119
"""ID to document the change. Entities within the same store_entity() call will
11161120
share the same change_id. This parameter can also be used to link multiple
11171121
store_entity() calls."""
1118-
meta_category_title: Optional[str] = "Category:Category"
1122+
meta_category_title: Optional[Union[str, List[str]]] = "Category:Category"
11191123
debug: Optional[bool] = False
11201124
_overwrite_per_class: Dict[str, Dict[str, OSW.OverwriteClassParam]] = (
11211125
PrivateAttr()
@@ -1188,25 +1192,35 @@ def store_entity(
11881192

11891193
max_index = len(param.entities)
11901194

1191-
meta_category = self.site.get_page(
1192-
WtSite.GetPageParam(titles=[param.meta_category_title])
1193-
).pages[0]
1194-
# ToDo: we have to do this iteratively to support meta categories inheritance
1195-
meta_category_template_str = meta_category.get_slot_content("schema_template")
1196-
meta_category_template = None
1195+
meta_category_templates = {}
11971196
if param.namespace == "Category":
1198-
if param.meta_category_title:
1199-
meta_category = self.site.get_page(
1200-
WtSite.GetPageParam(titles=[param.meta_category_title])
1201-
).pages[0]
1202-
meta_category_template_str = meta_category.get_slot_content(
1203-
"schema_template"
1204-
)
1205-
if meta_category_template_str:
1206-
meta_category_template = compile_handlebars_template(
1207-
meta_category_template_str
1197+
meta_category_titles = param.meta_category_title
1198+
if not isinstance(meta_category_titles, list):
1199+
meta_category_titles = [meta_category_titles]
1200+
meta_category_template_strs = {}
1201+
# We have to do this iteratively to support meta categories inheritance
1202+
while meta_category_titles is not None and len(meta_category_titles) > 0:
1203+
meta_categories = self.site.get_page(
1204+
WtSite.GetPageParam(titles=meta_category_titles)
1205+
).pages
1206+
for meta_category in meta_categories:
1207+
meta_category_template_strs[meta_category.title] = (
1208+
meta_category.get_slot_content("schema_template")
1209+
)
1210+
1211+
meta_category_titles = meta_category.get_slot_content("jsondata").get(
1212+
"subclass_of"
12081213
)
12091214

1215+
for title in meta_category_template_strs.keys():
1216+
meta_category_template_str = meta_category_template_strs[title]
1217+
if meta_category_template_str:
1218+
meta_category_templates[title] = compile_handlebars_template(
1219+
meta_category_template_str
1220+
)
1221+
# inverse order to have the most generic template first
1222+
meta_category_templates = dict(reversed(meta_category_templates.items()))
1223+
12101224
def store_entity_(
12111225
entity_: model.Entity,
12121226
namespace_: str = None,
@@ -1228,41 +1242,47 @@ def store_entity_(
12281242
entity=entity_,
12291243
namespace=namespace_,
12301244
policy=overwrite_class_param,
1231-
meta_category_template_str=meta_category_template_str,
12321245
remove_empty=param.remove_empty,
12331246
debug=param.debug,
12341247
)
12351248
)
1236-
if meta_category_template:
1249+
if len(meta_category_templates.keys()) > 0:
1250+
generated_schemas = {}
12371251
try:
12381252
jsondata = page.get_slot_content("jsondata")
12391253
if param.remove_empty:
12401254
remove_empty(jsondata)
1241-
schema_str = eval_compiled_handlebars_template(
1242-
meta_category_template,
1243-
jsondata,
1244-
{
1245-
"_page_title": entity_title, # Legacy
1246-
"_current_subject_": entity_title,
1247-
},
1248-
)
1249-
schema = json.loads(schema_str)
1250-
# Put generated schema in definitions section,
1251-
# currently only enabled for Characteristics
1252-
if hasattr(model, "CharacteristicType") and isinstance(
1253-
entity_, model.CharacteristicType
1254-
):
1255-
new_schema = {
1256-
"$defs": {"generated": schema},
1257-
"allOf": [{"$ref": "#/$defs/generated"}],
1258-
"@context": schema.pop("@context", None),
1259-
"title": schema.pop("title", ""),
1260-
}
1261-
schema["title"] = "Generated" + new_schema["title"]
1262-
schema = new_schema
1263-
page.set_slot_content("jsonschema", new_schema)
1255+
1256+
for key in meta_category_templates:
1257+
meta_category_template = meta_category_templates[key]
1258+
schema_str = eval_compiled_handlebars_template(
1259+
meta_category_template,
1260+
escape_json_strings(jsondata),
1261+
{
1262+
"_page_title": entity_title, # Legacy
1263+
"_current_subject_": entity_title,
1264+
},
1265+
)
1266+
generated_schemas[key] = json.loads(schema_str)
12641267
except Exception as e:
12651268
print(f"Schema generation from template failed for {entity_}: {e}")
1269+
1270+
mode = AggregateGeneratedSchemasParamMode.ROOT_LEVEL
1271+
# Put generated schema in definitions section,
1272+
# currently only enabled for Characteristics
1273+
if hasattr(model, "CharacteristicType") and isinstance(
1274+
entity_, model.CharacteristicType
1275+
):
1276+
mode = AggregateGeneratedSchemasParamMode.DEFINITIONS_SECTION
1277+
1278+
new_schema = aggregate_generated_schemas(
1279+
AggregateGeneratedSchemasParam(
1280+
schema=page.get_slot_content("jsonschema"),
1281+
generated_schemas=generated_schemas,
1282+
mode=mode,
1283+
)
1284+
).aggregated_schema
1285+
page.set_slot_content("jsonschema", new_schema)
12661286
page.edit() # will set page.changed if the content of the page has changed
12671287
if page.changed:
12681288
if index is None:

0 commit comments

Comments
 (0)