o
    i                     @   sb   d Z ddl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 G d	d
 d
ZdS )a  SageMaker function loader for loading handlers from environment variables.

Usage:
    # Load handlers from environment variables
    ping_handler = SageMakerFunctionLoader.get_ping_handler_from_env()
    invoke_handler = SageMakerFunctionLoader.get_invocation_handler_from_env()

    # Load specific functions
    handler = SageMakerFunctionLoader.load_function_from_spec("model:predict_fn")
    N)AnyCallableOptionalUnion   )FastAPIEnvVars)HandlerSpecparse_handler_spec   )SageMakerDefaultsSageMakerEnvVarsc                   @   s`  e Zd ZU dZdZee ed< edd Z	eddee
 de
fdd	Zed
e
defddZe	dde
dee
 dee fddZe	dde
dee
 deedef e
df fddZe	ddee
 deedef e
df fddZe	ddee
 deedef e
df fddZede
dee fddZedee fddZedee fddZdS ) SageMakerFunctionLoaderzUtility class for SageMaker function loading from environment variables.

    This class provides class methods for loading handlers and functions
    in SageMaker environments. All methods are class methods - no instantiation needed.
    N_default_function_loaderc                 C   s,   | j du rttjtj}| || _ | j S )z=Get or create the default SageMaker function loader (cached).N)r   osgetenvr   SAGEMAKER_MODEL_PATHr   SCRIPT_PATH_create_function_loader)clsscript_path r   p/home/ubuntu/.local/lib/python3.10/site-packages/model_hosting_container_standards/sagemaker/sagemaker_loader.pyget_function_loader   s   
z+SageMakerFunctionLoader.get_function_loaderdefault_scriptreturnc                 C   s   |pt j}ttj|S )z7Get custom script filename from environment or default.)r   SCRIPT_FILENAMEr   r   r   CUSTOM_SCRIPT_FILENAME)r   r   defaultr   r   r   get_custom_script_filename)   s   
z2SageMakerFunctionLoader.get_custom_script_filenamer   c                 C   s\   t tjtj}|g}t j||}ddlm	} d|i}|||}t j
|r,|| |S )z3Create a function loader for the given script path.r   )FunctionLoadermodel)r   r   r   r   r   r   pathjoin/common.custom_code_ref_resolver.function_loaderr   isfileload_module_from_file)r   r   script_filenamesearch_pathsmodel_file_pathr   module_aliasesfunction_loaderr   r   r   r   /   s   

z/SageMakerFunctionLoader._create_function_loaderspeccustom_script_pathc                 C   s\   t |}|js| sdS ttjtj}|du s||kr$| 	 
|S | |}|
|S )z(Load function from specification string.N)r   is_router_pathis_valid_function_specr   r   r   r   r   r   r   load_functionr   )r   r+   r,   handler_specdefault_script_pathr*   r   r   r   load_function_from_specD   s   

z/SageMakerFunctionLoader.load_function_from_specenv_var.c                 C   s:   t |}t|}|r|jr|jS |jr| |j|S dS )a  Generic method to get handler from environment variable.

        Returns:
        - Callable: When spec is a function/method specification
        - str: When spec is a router URL (starts with "/")
        - None: When environment variable is not set
        N)r   r   r	   r-   router_pathis_functionr2   r+   )r   r3   r,   spec_stringr+   r   r   r   _get_handler_from_env]   s   
z-SageMakerFunctionLoader._get_handler_from_envc                 C      |  tj|S )z9Get custom ping handler from CUSTOM_FASTAPI_PING_HANDLER.)r7   r   CUSTOM_FASTAPI_PING_HANDLERr   r,   r   r   r   get_ping_handler_from_envr      z1SageMakerFunctionLoader.get_ping_handler_from_envc                 C   r8   )zEGet custom invocation handler from CUSTOM_FASTAPI_INVOCATION_HANDLER.)r7   r   !CUSTOM_FASTAPI_INVOCATION_HANDLERr:   r   r   r   get_invocation_handler_from_env{   r<   z7SageMakerFunctionLoader.get_invocation_handler_from_envc                 C   s   t |}t|S )zFGeneric method to get handler specification from environment variable.)r   r   r	   )r   r3   r6   r   r   r   _get_handler_spec   s   
z)SageMakerFunctionLoader._get_handler_specc                 C      |  tjS )z&Get ping handler specification object.)r?   r   r9   r   r   r   r   get_ping_handler_spec      z-SageMakerFunctionLoader.get_ping_handler_specc                 C   r@   )z,Get invocation handler specification object.)r?   r   r=   rA   r   r   r   get_invocation_handler_spec   rC   z3SageMakerFunctionLoader.get_invocation_handler_spec)N)__name__
__module____qualname____doc__r   r   r   __annotations__classmethodr   strr   r   r   r2   r   r7   r;   r>   r   r?   rB   rD   r   r   r   r   r      s^   
 

r   )rH   r   typingr   r   r   r   common.fastapi.configr   common.handler.specr   r	   configr   r   r   r   r   r   r   <module>   s    