def parse_dict( self, manager: ImportManager, schema: Dict, entry_name: EntryName, att_name: str, att_type: str, desc: str, ): if (SchemaKeywords.dict_key_type not in schema or SchemaKeywords.dict_value_type not in schema): raise TypeNotDeclaredException( f"Item type of the attribute {att_name} for the entry " f" {entry_name.class_name} not declared. This attribute is " f"a composite type: {att_type}, it should have a " f"{SchemaKeywords.dict_key_type} and " f"{SchemaKeywords.dict_value_type}.") key_type = schema[SchemaKeywords.dict_key_type] if not valid_composite_key(key_type): raise UnsupportedTypeException( f"Key type {key_type} for entry {entry_name.name}'s " f"attribute {att_name} is not supported, we only support a " f"limited set of keys.") value_type = schema[SchemaKeywords.dict_value_type] if is_composite_type(value_type): # Case of nested. raise UnsupportedTypeException( f"Item type {value_type} for entry {entry_name.name}'s " f"attribute {att_name} is a composite type, we do not support " f"nested composite type.") if not manager.is_known_name(value_type): # Case of unknown. raise TypeNotDeclaredException( f"Item type {value_type} for the entry " f"{entry_name.name} of the attribute {att_name} " f"not declared in ontology.") # Make sure the import of these related types are handled. manager.add_object_to_import(value_type) self_ref = entry_name.class_name == value_type default_val: Dict = {} return DictProperty( manager, att_name, key_type, value_type, description=desc, default_val=default_val, self_ref=self_ref, )
def parse_list( self, manager: ImportManager, schema: Dict, entry_name: EntryName, att_name: str, att_type: str, desc: str) -> ListProperty: if SchemaKeywords.element_type not in schema: raise TypeNotDeclaredException( f"Item type for the attribute {att_name} of the entry " f"[{entry_name.class_name}] not declared. This attribute is " f"a composite type: {att_type}, it should have a " f"{SchemaKeywords.element_type}.") item_type = schema[SchemaKeywords.element_type] if is_composite_type(item_type): # Case of nested. raise UnsupportedTypeException( f"Item type {item_type} for entry {entry_name.name}'s " f"attribute {att_name} is a composite type, we do not support " f"nested composite type.") if not manager.is_known_name(item_type): # Case of unknown. raise TypeNotDeclaredException( f"Item type {item_type} for the entry " f"{entry_name.name} of the attribute {att_name} " f"not declared in ontology.") # Make sure the import of these related types are handled. manager.add_object_to_import(item_type) self_ref = entry_name.class_name == item_type return ListProperty( manager, att_name, item_type, description=desc, default_val=[], self_ref=self_ref)
def parse_property(self, entry_name: EntryName, schema: Dict) -> Property: """ Parses instance and class properties defined in an entry schema and checks for the constraints allowed by the ontology generation system. Args: entry_name: Entry Name object that contains various form of the entry's name. schema: Entry definition schema Returns: An object of class `code_generation_util.FileItem` containing the generated code. """ att_name = schema[SchemaKeywords.attribute_name] att_type = schema[SchemaKeywords.attribute_type] manager: ImportManager = self.import_managers.get( entry_name.module_name) # schema type should be present in the validation tree # TODO: Remove this hack if not manager.is_known_name(att_type): raise TypeNotDeclaredException( f"Attribute type '{att_type}' for the entry " f"'{entry_name.name}' of the attribute '{att_name}' not " f"declared in the ontology") desc = schema.get(SchemaKeywords.description, None) default_val = schema.get(SchemaKeywords.default_value, None) # element type should be present in the validation tree if att_type in COMPOSITES: if att_type == "List": return self.parse_list(manager, schema, entry_name, att_name, att_type, desc) elif att_type == "Dict": return self.parse_dict(manager, schema, entry_name, att_name, att_type, desc) elif att_type in NON_COMPOSITES or manager.is_imported(att_type): self_ref = entry_name.class_name == att_type return self.parse_non_composite( manager, att_name, att_type, desc, default_val, self_ref=self_ref, ) raise UnsupportedTypeException(f"{att_type} is not a supported type.")