"""Common utility functions for the context package."""fromaac.context.language_errorimportLanguageError
[docs]defget_python_module_name(package_name:str)->str:"""Returns a Python module name, with underscores, from a given package name, which may contain dashes and spaces."""ifnotpackage_nameorpackage_name=="":# we just using default if no package is declared...which really shouldn't happen in core AaCreturn"default"result=package_name.replace(" ","_").replace("-","_").lower()foriteminresult.split("."):# expect name to be dot notationifnotitem[0].isalpha():raiseLanguageError(f"Invalid AaC name: '{item}' within '{package_name}', the first character must be a letter","Unknown location")forsub_iteminitem.split("_"):# and name elements may contain _ifnotsub_item.isalnum():# but things between underscore must be alphanumericraiseLanguageError(f"Invalid AaC package name: '{package_name}', the name must be alphanumeric","Unknown location")returnresult
[docs]defget_python_class_name(name:str)->str:"""Returns a Python class name from a given name, which may contain dashes and underscores."""ifnotnameorname=="":raiseLanguageError(f"Invalid AaC package name: '{name}', the name must not be empty","Unknown location")ifnotname[0].isalpha():raiseLanguageError(f"Invalid AaC name: '{name}', the first character must be a letter","Unknown location")result=name.replace("-"," ").replace("_"," ").replace(" ","")ifnotresult.isalnum():raiseLanguageError(f"Invalid AaC name: '{name}', the name must be alphanumeric","Unknown location")returnresult
[docs]defget_fully_qualified_name(package_name:str,class_name:str)->str:"""Returns the fully qualified name of a class, given the package name and class name."""returnf"{get_python_module_name(package_name)}.{get_python_class_name(class_name)}"