o
    i                     @   s   d Z ddlmZmZmZmZ ddlmZ ddlm	Z	m
Z
 ddlmZmZ ddlmZ dd	lmZ G d
d de
ZG dd de	Ze Zdd ZdS )a  Handler resolution logic for SageMaker container standards.

This module provides SageMaker-specific handler resolution using the generic
handler resolver framework.

## Handler Resolution Priority Order

### For Ping Handlers:
1. **Environment variable** specified function (CUSTOM_FASTAPI_PING_HANDLER)
2. **Registry** @custom_ping_handler decorated function
3. **Customer script** def custom_sagemaker_ping_handler function
4. **Default** handler (if any)

### For Invoke Handlers:
1. **Environment variable** specified function (CUSTOM_FASTAPI_INVOCATION_HANDLER)
2. **Registry** @custom_invocation_handler decorated function
3. **Customer script** def custom_sagemaker_invocation_handler function
4. **Default** handler (if any)

## Error Handling

- Environment variable errors are raised immediately (configuration errors)
- Customer script errors are raised if script exists but can't be loaded
- Missing handlers return None (graceful degradation)
    )AnyCallableOptionalUnion   )handler_registry)GenericHandlerResolverHandlerConfig)HandlerFileNotFoundErrorHandlerNotFoundError)logger   )SageMakerFunctionLoaderc                   @   s\   e Zd ZdZdddZdedeedef edf fd	d
Z	dede
edef  fddZdS )SageMakerHandlerConfigz)SageMaker-specific handler configuration.#custom_sagemaker_invocation_handlercustom_sagemaker_ping_handler)invokepinghandler_typereturn.Nc                 C   s$   |dkrt  S |dkrt  S dS )z1Get handler from SageMaker environment variables.r   r   N)r   get_ping_handler_from_envget_invocation_handler_from_env)selfr    r   p/home/ubuntu/.local/lib/python3.10/site-packages/model_hosting_container_standards/sagemaker/handler_resolver.pyget_env_handler-   s
   z&SageMakerHandlerConfig.get_env_handlerc              
   C   s   | j |}|std|  dS z	td| W S  ttfy> } ztd| dt|j	  W Y d}~dS d}~w t
yN   td| d  w )z+Get handler from SageMaker customer script.z#No mapping found for handler type: Nzmodel:zNo customer script z function found: zCustomer script z function failed to load)HANDLER_TYPE_TO_FUNCTION_NAMEgetr   debugr   load_function_from_specr
   r   type__name__	Exceptionerror)r   r   custom_function_nameer   r   r   get_customer_script_handler8   s(   
z2SageMakerHandlerConfig.get_customer_script_handler)r!   
__module____qualname____doc__r   strr   r   r   r   r   r&   r   r   r   r   r   $   s    
r   c                       s"   e Zd ZdZd fddZ  ZS )SageMakerHandlerResolverzMSageMaker-specific handler resolver inheriting from generic resolution logic.r   Nc                    s   t  t  dS )z*Initialize the SageMaker handler resolver.N)super__init__r   )r   	__class__r   r   r-   V   s   z!SageMakerHandlerResolver.__init__)r   N)r!   r'   r(   r)   r-   __classcell__r   r   r.   r   r+   S   s    r+   c                  C   s   dd } | d | d d S )Nc                 S   s"   t | }|rt| | d S d S )N)	_resolverresolve_handlerr   set_handler)r   handlerr   r   r   r3   `   s   
z1register_sagemaker_overrides.<locals>.set_handlerr   r   r   )r3   r   r   r   register_sagemaker_overrides_   s   r5   N)r)   typingr   r   r   r   common.handler.registryr   common.handler.resolverr   r	   
exceptionsr
   r   logging_configr   sagemaker_loaderr   r   r+   r1   r5   r   r   r   r   <module>   s    /	