"""Definition class for Architecture-as-Code."""fromattrimportFactory,attrib,attrs,validatorsfromtypingimportAnyfromuuidimportUUID,uuid5,NAMESPACE_DNSimportyamlfromaac.in_out.files.aac_fileimportAaCFilefromaac.context.lexemeimportLexemefromaac.context.utilimportget_python_module_name,get_python_class_name,get_fully_qualified_name
[docs]@attrs(hash=False,eq=False)classDefinition:"""An Architecture-as-Code definition. Attributes: uid (UUID): A unique identifier for selecting the specific definition. name (str): The name of the definition. package (str): The package of the definition. content (str): The original source textual representation of the definition. source (AaCFile): The source document containing the definition. lexemes (list[Lexeme]): A list of lexemes for each item in the parsed definition. structure (dict): The dictionary representation of the definition. instance (Any): A Python class instance of the definition. """uid:UUID=attrib(init=False,validator=validators.instance_of(UUID))name:str=attrib(validator=validators.instance_of(str))package:str=attrib(validator=validators.instance_of(str))content:str=attrib(validator=validators.instance_of(str))source:AaCFile=attrib(validator=validators.instance_of(AaCFile))lexemes:list[Lexeme]=attrib(default=Factory(list),validator=validators.instance_of(list))structure:dict=attrib(default=Factory(dict),validator=validators.instance_of(dict))instance:Any=attrib(default=None)
[docs]def__hash__(self)->int:"""Return the hash of this Definition."""returnhash(self.get_fully_qualified_name())
[docs]def__eq__(self,obj):"""Equals function for the Definition."""defis_equal()->bool:equal=self.get_fully_qualified_name()==obj.get_fully_qualified_name()equal=equalandself.structure==obj.structurereturnequalreturnisinstance(obj,Definition)andis_equal()
[docs]defget_root_key(self)->str:"""Get the root key for the definition. Returns: The root key for the definition. """returnlist(self.structure.keys())[0]
[docs]defis_import(self)->bool:"""Return True if the definition is an import definition."""returnself.get_root_key()=="import"
[docs]defget_python_module_name(self)->str:"""Return the python module name for the definition."""ifself.is_import():return""returnget_python_module_name(self.package)
[docs]defget_python_class_name(self)->str:"""Return the python class name for the definition."""ifself.is_import():return""returnget_python_class_name(self.name)
[docs]defget_fully_qualified_name(self)->str:"""Return the fully qualified name of the definition."""ifself.is_import():return""# this is just the package and name joined with a dotreturnget_fully_qualified_name(self.package,self.name)
[docs]defto_yaml(self)->str:"""Return a yaml string based on the current state of the definition including extensions."""returnyaml.dump(self.structure,sort_keys=False)