U
    ccq.                     @   s   d dl Z d dlmZmZ ddlmZ ddlmZmZm	Z	m
Z
 ddlmZ ddlmZmZ e jrd dl mZmZmZ dd	lmZ ed
ZedZG dd deZdS )    N)ABCMetaabstractmethod   )RuntimeConfigurationBuilder)AbstractRequestHandlerAbstractRequestInterceptorAbstractResponseInterceptorAbstractExceptionHandler)SkillBuilderException)AbstractTemplateLoaderAbstractTemplateRenderer)CallableTypeVarList)AbstractSkillTInputc                   @   s   e Zd ZdZeZdd Zdd Zdd Zdd	 Z	d
d Z
dd Zdd Zdd Zdd Zdd Zdd Zdd Zedd ZdS )AbstractSkillBuilderzAbstract Skill Builder with helper functions for building
    :py:class:`ask_sdk_runtime.skill.AbstractSkill` object.

    Domain SDKs has to implement the `create` method that returns
    an instance of the skill implementation for the domain type.
    c                 C   s   t  | _d S N)r   runtime_configuration_builderself r   A/tmp/pip-unpacked-wheel-yhqslwpj/ask_sdk_runtime/skill_builder.py__init__/   s    zAbstractSkillBuilder.__init__c                 C   s   | j | dS )a  Register input to the request handlers list.

        :param request_handler: Request Handler instance to be
            registered.
        :type request_handler: ask_sdk_runtime.dispatch_components.request_components.AbstractRequestHandler
        :return: None
        N)r   add_request_handler)r   request_handlerr   r   r   r   3   s    	z(AbstractSkillBuilder.add_request_handlerc                 C   s   | j | dS )a  Register input to the exception handlers list.

        :param exception_handler: Exception Handler instance to be
            registered.
        :type exception_handler: ask_sdk_runtime.dispatch_components.request_components.AbstractExceptionHandler
        :return: None
        N)r   add_exception_handler)r   exception_handlerr   r   r   r   ?   s    	z*AbstractSkillBuilder.add_exception_handlerc                 C   s   | j | dS )a+  Register input to the global request interceptors list.

        :param request_interceptor: Request Interceptor instance to be
            registered.
        :type request_interceptor: ask_sdk_runtime.dispatch_components.request_components.AbstractRequestInterceptor
        :return: None
        N)r   add_global_request_interceptor)r   request_interceptorr   r   r   r   K   s    	z3AbstractSkillBuilder.add_global_request_interceptorc                 C   s   | j | dS )a0  Register input to the global response interceptors list.

        :param response_interceptor: Response Interceptor instance to
            be registered.
        :type response_interceptor: ask_sdk_runtime.dispatch_components.request_components.AbstractResponseInterceptor
        :return: None
        N)r   add_global_response_interceptor)r   response_interceptorr   r   r   r!   W   s    	z4AbstractSkillBuilder.add_global_response_interceptorc                 C   s   | j | dS )zRegister input to the loaders list.

        :param loaders: List of loaders
        :type loaders:  :py:class:`ask_sdk_runtime.view_resolvers.AbstractTemplateLoader`
        N)r   add_loaders)r   loadersr   r   r   r#   c   s    z AbstractSkillBuilder.add_loadersc                 C   s   | j | dS )zRegister input to loaders list.

        :param loader: Loader to load template from a specific data source
        :type loader: :py:class:`ask_sdk_runtime.view_resolvers.AbstractTemplateLoader`
        N)r   
add_loader)r   loaderr   r   r   r%   l   s    zAbstractSkillBuilder.add_loaderc                 C   s   | j | dS )zRegister renderer to generate template responses.

        :param renderer: Renderer to render the template
        :type renderer:  :py:class:`ask_sdk_runtime.view_resolvers.AbstractTemplateRenderer`
        N)r   add_renderer)r   Zrendererr   r   r   r'   u   s    z!AbstractSkillBuilder.add_rendererc                    s    fdd}|S )aa  Decorator that can be used to add request handlers easily to
        the builder.

        The can_handle_func has to be a Callable instance, which takes
        a single parameter and no varargs or kwargs. This is because
        of the RequestHandler class signature restrictions. The
        returned wrapper function can be applied as a decorator on any
        function that returns a response object by the skill. The
        function should follow the signature of the handle function in
        :py:class:`ask_sdk_runtime.dispatch_components.request_components.AbstractRequestHandler`
        class.

        :param can_handle_func: The function that validates if the
            request can be handled.
        :type can_handle_func: Callable[[Input], bool]
        :return: Wrapper function that can be decorated on a handle
            function.
        c                    sf   t rt  stdfdd fddd}td j ddtf|}j| d	  S )
NzSRequest Handler can_handle_func and handle_func input parameters should be callablec                    s    |S r   r   r   handler_inputcan_handle_funcr   r   <lambda>   s   zGAbstractSkillBuilder.request_handler.<locals>.wrapper.<locals>.<lambda>c                    s    |S r   r   r(   handle_funcr   r   r,      s   Z
can_handlehandlezRequestHandler{}_ )r   )	callabler
   typeformat__name__titlereplacer   r   )r.   class_attributesZrequest_handler_classr+   r   r-   r   wrapper   s     

 z5AbstractSkillBuilder.request_handler.<locals>.wrapperr   r   r+   r;   r   r:   r   r   ~   s    z$AbstractSkillBuilder.request_handlerc                    s    fdd}|S )a  Decorator that can be used to add exception handlers easily
        to the builder.

        The can_handle_func has to be a Callable instance, which takes
        two parameters and no varargs or kwargs. This is because of the
        ExceptionHandler class signature restrictions. The returned
        wrapper function can be applied as a decorator on any function
        that processes the exception raised during dispatcher and
        returns a response object by the skill. The function should
        follow the signature of the handle function in
        :py:class:`ask_sdk_runtime.dispatch_components.exception_components.AbstractExceptionHandler`
        class.

        :param can_handle_func: The function that validates if the
            exception can be handled.
        :type can_handle_func: Callable[[Input, Exception], bool]
        :return: Wrapper function that can be decorated on a handle
            function.
        c                    sf   t rt  stdfdd fddd}td j ddtf|}j| d	  S )
NzUException Handler can_handle_func and handle_func input parameters should be callablec                    s
    ||S r   r   r   r)   	exceptionr*   r   r   r,      s    zIAbstractSkillBuilder.exception_handler.<locals>.wrapper.<locals>.<lambda>c                    s
    ||S r   r   r=   r-   r   r   r,      s    r/   zExceptionHandler{}r1   r2   )r   )	r3   r
   r4   r5   r6   r7   r8   r	   r   )r.   r9   Zexception_handler_classr:   r-   r   r;      s$    

 z7AbstractSkillBuilder.exception_handler.<locals>.wrapperr   r<   r   r:   r   r      s    z&AbstractSkillBuilder.exception_handlerc                    s    fdd}|S )a  Decorator that can be used to add global request
        interceptors easily to the builder.

        The returned wrapper function can be applied as a decorator on
        any function that processes the input. The function should
        follow the signature of the process function in
        :py:class:`ask_sdk_runtime.dispatch_components.request_components.AbstractRequestInterceptor`
        class.

        :return: Wrapper function that can be decorated on a
            interceptor process function.
        c                    sT   t  stdd fddi}td j ddtf|}j| d  S )	NzJGlobal Request Interceptor process_func input parameter should be callableprocessc                    s    |S r   r   r(   process_funcr   r   r,      s   zRAbstractSkillBuilder.global_request_interceptor.<locals>.wrapper.<locals>.<lambda>zRequestInterceptor{}r1   r2   )r    )	r3   r
   r4   r5   r6   r7   r8   r   r   )rA   r9   r    r   r@   r   r;      s$     
 z@AbstractSkillBuilder.global_request_interceptor.<locals>.wrapperr   r   r;   r   r   r   global_request_interceptor   s    z/AbstractSkillBuilder.global_request_interceptorc                    s    fdd}|S )aA  Decorator that can be used to add global
        response interceptors easily to the builder.

        The returned wrapper function can be applied as a decorator
        on any function that processes the input and the response
        generated by the request handler. The function should follow
        the signature of the process function in
        :py:class:`ask_sdk_runtime.dispatch_components.request_components.AbstractResponseInterceptor`
        class.

        :return: Wrapper function that can be decorated on a
            interceptor process function.
        c                    sT   t  stdd fddi}td j ddtf|}j| d  S )	NzKGlobal Response Interceptor process_func input parameter should be callabler?   c                    s
    ||S r   r   )r   r)   responser@   r   r   r,     s    zSAbstractSkillBuilder.global_response_interceptor.<locals>.wrapper.<locals>.<lambda>zResponseInterceptor{}r1   r2   )r"   )	r3   r
   r4   r5   r6   r7   r8   r   r!   )rA   r9   r"   r   r@   r   r;     s$    
 zAAbstractSkillBuilder.global_response_interceptor.<locals>.wrapperr   rB   r   r   r   global_response_interceptor   s    z0AbstractSkillBuilder.global_response_interceptorc                 C   s   t dS )zCreate a skill object using the registered components.

        :return: a skill object that can be used for invocation.
        :rtype: AbstractSkill
        N)NotImplementedErrorr   r   r   r   create  s    zAbstractSkillBuilder.createN)r6   
__module____qualname____doc__r   __metaclass__r   r   r   r   r!   r#   r%   r'   r   r   rC   rE   r   rG   r   r   r   r   r   &   s    			*-#%r   )typingabcr   r   Zskillr   Zdispatch_componentsr   r   r   r	   
exceptionsr
   Zview_resolversr   r   TYPE_CHECKINGr   r   r   r   r   r   objectr   r   r   r   r   <module>   s   