Source code for aac.plugins.generate.helpers.python_helpers
"""Python helpers for the generate plugin that can be passed into Jinja2 and used in templates."""fromaac.context.language_contextimportLanguageContextfromaacimport__version__fromtypingimportAny# build out jinja2 utility functions
[docs]defget_python_name(name:str)->str:"""Returns the python name for an AaC name."""ifname.startswith("--"):name=name[2:]returnname.replace(" ","_").replace("-","_").lower()
[docs]defget_python_primitive(aac_primitive_name:str)->str:"""Returns the python primitive for an AaC primitive."""context=LanguageContext()aac_primitives=context.get_primitives()clean_name=aac_primitive_nameifclean_name.endswith("[]"):clean_name=clean_name[:-2]ifclean_name.find("(")>-1:clean_name=clean_name[:clean_name.find("(")]foraac_primitiveinaac_primitives:ifclean_name==aac_primitive.name:returnaac_primitive.instance.python_typereturn"None"
[docs]defget_python_type(aac_type_name:str)->str:"""Returns the python type for an AaC type."""context=LanguageContext()ifaac_type_name.endswith("[]"):aac_type_name=aac_type_name[:-2]returnf"list[{get_python_type(aac_type_name)}]"elifaac_type_namein[primitive.nameforprimitiveincontext.get_primitives()]:returncontext.get_python_type_from_primitive(aac_type_name)else:returnaac_type_name
[docs]defget_package_from_plugin(plugin_name:str)->str:"""Returns the package for an AaC plugin."""context=LanguageContext()plugin_runners=context.get_plugin_runners()forrunnerinplugin_runners:ifplugin_name==runner.plugin_definition.name:returnrunner.plugin_definition.instance.packagereturnf"aac.plugins.{get_python_name(plugin_name)}"
[docs]defget_package_from_aac_definition(item_name:str)->str:"""Returns the package for an AaC definition."""context=LanguageContext()definitions=context.get_definitions()fordefinitionindefinitions:ifdefinition.name==item_name:returndefinition.instance.packagereturn""
[docs]defget_path_from_package(package:str)->str:"""Converts a package to a path."""returnpackage.replace(".","/")
[docs]defaac_version()->str:"""Returns the current version of AaC."""return__version__
[docs]defschema_to_test_dict(name:str,omit_optional_fields:bool=False)->dict:"""Generate test data for a schema to a dictionary based on its fields."""context:LanguageContext=LanguageContext()schema_definitions=context.get_definitions_by_name(name)iflen(schema_definitions)!=1:raiseException(f"Could not find unique schema definition: {name}")schema_definition=schema_definitions[0]result:dict={}# we need to avoid the case where a schema is defining an AaC type other than Schemaifschema_definition.get_root_key()=="enum":# need to return a valid enum valueforvalueinschema_definition.instance.values:returnvalueforfieldinschema_definition.instance.fields:ifomit_optional_fieldsandnotfield.is_required:continueclean_field_type=get_python_primitive(field.type)ifclean_field_type=="None":# this is a schema typeiffield.type.endswith("[]"):# this is an array of schema typesresult[field.name]=[schema_to_test_dict(field.type[:-2],omit_optional_fields),schema_to_test_dict(field.type[:-2],omit_optional_fields),]else:result[field.name]=schema_to_test_dict(field.type,omit_optional_fields)else:# this is a primitive typeiffield.type.endswith("[]"):# this is an array of primitive typesresult[field.name]=[_generate_test_data_for_primitive(clean_field_type),_generate_test_data_for_primitive(clean_field_type),]else:result[field.name]=_generate_test_data_for_primitive(clean_field_type)returnresult