@@ -712,6 +712,9 @@ class LoadEntityParam(BaseModel):
712712 autofetch_schema : Optional [bool ] = True
713713 """If true, load the corresponding schemas /
714714 categories ad-hoc if not already present"""
715+ model_to_use : Optional [Type [OswBaseModel ]] = None
716+ """If provided this model will be used to create an entity (instance of the
717+ model), instead of instantiating the autofetched schema."""
715718 remove_empty : Optional [bool ] = True
716719 """If true, remove key with an empty string, list, dict or set as value
717720 from the jsondata."""
@@ -770,6 +773,9 @@ def load_entity(
770773 else :
771774 param = entity_title
772775
776+ if param .model_to_use :
777+ print (f"Using schema { param .model_to_use .__name__ } to create entity" )
778+
773779 # store original cache state
774780 cache_state = self .site .get_cache_enabled ()
775781 if param .disable_cache :
@@ -797,23 +803,29 @@ def load_entity(
797803 schemas .append (schema )
798804 # generate model if not already exists
799805 cls_name : str = schema ["title" ]
800- if not hasattr (model , cls_name ):
801- if param .autofetch_schema :
802- self .fetch_schema (
803- OSW .FetchSchemaParam (
804- schema_title = category , mode = "append"
806+ # If a schema_to_use is provided, we do not need to check if the
807+ # model exists
808+ if not param .model_to_use :
809+ if not hasattr (model , cls_name ):
810+ if param .autofetch_schema :
811+ self .fetch_schema (
812+ OSW .FetchSchemaParam (
813+ schema_title = category , mode = "append"
814+ )
805815 )
816+ if not hasattr (model , cls_name ):
817+ schemas_fetched = False
818+ print (
819+ f"Error: Model { cls_name } not found. Schema { category } "
820+ f"needs to be fetched first."
806821 )
807- if not hasattr (model , cls_name ):
808- schemas_fetched = False
809- print (
810- f"Error: Model { cls_name } not found. Schema { category } "
811- f"needs to be fetched first."
812- )
813822 if not schemas_fetched :
814823 continue
815824
816- if len (schemas ) == 0 :
825+ if param .model_to_use :
826+ entity : model .OswBaseModel = param .model_to_use (** jsondata )
827+
828+ elif len (schemas ) == 0 :
817829 print ("Error: no schema defined" )
818830
819831 elif len (schemas ) == 1 :
@@ -1055,7 +1067,7 @@ def set_content(content_to_set: dict) -> None:
10551067 if param .remove_empty :
10561068 remove_empty (local_content ["jsondata" ])
10571069 if param .debug :
1058- print (f"'local_content': { str (remote_content )} " )
1070+ print (f"'local_content': { str (local_content )} " )
10591071 # Apply the overwrite logic
10601072 # a) If there is a key in the remote content that is not in the local
10611073 # content, we have to keep it
@@ -1513,7 +1525,7 @@ def __init__(self, **data):
15131525 self ._titles = [parts ["title" ] for parts in self ._category_string_parts ]
15141526
15151527 def query_instances (
1516- self , category : Union [str , OswBaseModel , OSW .QueryInstancesParam ]
1528+ self , category : Union [str , Type [ OswBaseModel ] , OSW .QueryInstancesParam ]
15171529 ) -> List [str ]:
15181530 if not isinstance (category , OSW .QueryInstancesParam ):
15191531 category = OSW .QueryInstancesParam (categories = category )
0 commit comments