"""Register test handlers."""from__future__importannotationsimportloggingfromtypingimportAny,castfrom..utilsimportload_object_from_stringfrom.handlers.baseimportLookupHandlerfrom.handlers.cfnimportCfnLookupfrom.handlers.ecrimportEcrLookupfrom.handlers.envimportEnvLookupfrom.handlers.random_stringimportRandomStringLookupfrom.handlers.ssmimportSsmLookupfrom.handlers.varimportVarLookupRUNWAY_LOOKUP_HANDLERS:dict[str,type[LookupHandler[Any]]]={}LOGGER=logging.getLogger(__name__)
[docs]defregister_lookup_handler(lookup_type:str,handler_or_path:str|type[LookupHandler[Any]])->None:"""Register a lookup handler. Args: lookup_type: Name to register the handler under handler_or_path: a function or a path to a handler """handler=handler_or_pathhandler=(cast("type",load_object_from_string(handler_or_path))ifisinstance(handler_or_path,str)elsehandler_or_path)try:ifissubclass(handler,LookupHandler):RUNWAY_LOOKUP_HANDLERS[lookup_type]=handlerreturnexceptException:# noqa: BLE001LOGGER.debug("failed to validate lookup handler",exc_info=True)raiseTypeError(f"lookup {handler_or_path} must be a subclass of runway.lookups.handlers.base.LookupHandler")
[docs]defunregister_lookup_handler(lookup_type:str)->None:"""Unregister the specified test type. This is useful when testing various lookup types if you want to unregister the lookup type after the test runs. Args: lookup_type: Name of the lookup type to unregister """RUNWAY_LOOKUP_HANDLERS.pop(lookup_type,None)