Skip to content

Commit a17f34c

Browse files
committed
feat: load schemas ad-hoc
1 parent 2e137e8 commit a17f34c

File tree

1 file changed

+29
-1
lines changed

1 file changed

+29
-1
lines changed

src/osw/core.py

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -327,9 +327,14 @@ def _fetch_schema(self, fetchSchemaParam: _FetchSchemaParam = None) -> None:
327327
print(f"Error: Page {schema_title} does not exist")
328328
return
329329
if schema_title.startswith("Category:"):
330-
schema_str = json.dumps(page.get_slot_content("jsonschema"))
330+
schema_str = ""
331+
if page.get_slot_content("jsonschema"):
332+
schema_str = json.dumps(page.get_slot_content("jsonschema"))
331333
else:
332334
schema_str = page.get_content()
335+
if (schema_str is None) or (schema_str == ""):
336+
print(f"Error: Schema {schema_title} does not exist")
337+
return
333338
schema = json.loads(
334339
schema_str.replace("$ref", "dollarref")
335340
) # '$' is a special char for root object in jsonpath
@@ -566,6 +571,9 @@ class LoadEntityParam(BaseModel):
566571
"""
567572

568573
titles: Union[str, List[str]]
574+
"""the pages titles to load"""
575+
autofetch_schema: Optional[bool] = True
576+
"""if true, load the corresponding schemas / categories ad-hoc if not already present"""
569577

570578
class LoadEntityResult(BaseModel):
571579
"""Result of load_entity()
@@ -599,11 +607,13 @@ def load_entity(
599607
"""
600608

601609
titles = []
610+
load_param = OSW.LoadEntityParam()
602611
if isinstance(entity_title, str): # single title
603612
titles = [entity_title]
604613
if isinstance(entity_title, list): # list of titles
605614
titles = entity_title
606615
if isinstance(entity_title, OSW.LoadEntityParam): # LoadEntityParam
616+
load_param = entity_title
607617
titles = entity_title.titles
608618
entities = []
609619

@@ -615,6 +625,7 @@ def load_entity(
615625
for page in pages:
616626
entity = None
617627
schemas = []
628+
schemas_fetched = True
618629
jsondata = page.get_slot_content("jsondata")
619630
if jsondata:
620631
for category in jsondata["type"]:
@@ -624,6 +635,22 @@ def load_entity(
624635
.get_slot_content("jsonschema")
625636
)
626637
schemas.append(schema)
638+
# generate model if not already exists
639+
cls = schema["title"]
640+
if not hasattr(model, cls):
641+
if load_param.autofetch_schema:
642+
self.fetch_schema(
643+
OSW.FetchSchemaParam(
644+
schema_title=category, mode="append"
645+
)
646+
)
647+
if not hasattr(model, cls):
648+
schemas_fetched = False
649+
print(
650+
f"Error: Model {cls} not found. Schema {category} needs to be fetched first."
651+
)
652+
if not schemas_fetched:
653+
continue
627654

628655
if len(schemas) == 0:
629656
print("Error: no schema defined")
@@ -696,6 +723,7 @@ def store_entity(
696723
meta_category = self.site.get_page(
697724
WtSite.GetPageParam(titles=[param.meta_category_title])
698725
).pages[0]
726+
# ToDo: we have to do this iteratively to support meta categories inheritance
699727
meta_category_template = meta_category.get_slot_content("schema_template")
700728
if meta_category_template:
701729
meta_category_template = compile_handlebars_template(meta_category_template)

0 commit comments

Comments
 (0)