"""Retrieve a variable from the variables file or definition."""from__future__importannotationsimportloggingfromtypingimportTYPE_CHECKING,Any,ClassVarfrom.baseimportLookupHandlerifTYPE_CHECKING:from...utilsimportMutableMapLOGGER=logging.getLogger(__name__)TYPE_NAME="var"
[docs]classVarLookup(LookupHandler[Any]):"""Variable definition Lookup."""TYPE_NAME:ClassVar[str]="var""""Name that the Lookup is registered as."""
[docs]@classmethoddefhandle(cls,value:str,*_args:Any,variables:MutableMap,**_kwargs:Any)->Any:"""Retrieve a variable from the variable definition. The value is retrieved from the variables passed to Runway using either a variables file or the ``variables`` directive of the config file. Args: value: The value passed to the Lookup. variables: The resolved variables pass to Runway. Raises: ValueError: Unable to find a value for the provided query and a default value was not provided. """query,args=cls.parse(value)result=variables.find(query,default=args.pop("default",""))ifresult!="":# allows for False bool and NoneType resultsreturncls.format_results(result,**args)raiseValueError(f'"{query}" does not exist in the variable definition')