o
    㥵iR                     @  s  d dl mZ d dlZd dlZd dlZd dlmZ d dlmZ d dl	m
Z
mZmZmZ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mZmZ d d	lmZ d d
lmZ ddl m!Z! ddl"m#Z# ddl$m%Z%m&Z&m'Z'm(Z( ddl)m*Z*m+Z+ ddl,m-Z- ddl m.Z. ddl/m0Z0m1Z1m2Z2 ddl3m4Z4m5Z5m6Z6 ddl"m7Z7m8Z8m9Z9m:Z:m;Z;m<Z< dd	l$mZ ddl=m>Z> ej?G dd dZ@G dd dZAd!dd ZBdS )"    )annotationsN)PurePath)GeneratorType)AnyCallableIterableListMappingNoReturnOptionalType)URL)EnvironStartResponse)FilesHostsPagesSubpaths)Router)	BaseModel   )
CORSConfig)create_json_encoder)	BaseRouteMiddlewareTypeNoMatchFoundSyncViewType)ImmutableAttributeState)context_setter   )
allow_cors)ErrorHandlerTypeExceptionMiddlewareHTTPException)HttpRequesthttp_connection_varrequest_var)FileResponseHttpResponseJSONResponsePlainTextResponseRedirectResponseSendEventResponse)BaseTemplatesc                   @  s   e Zd ZU eZded< dS )FactoryClasszType[HttpRequest]httpN)__name__
__module____qualname__r%   r0   __annotations__ r5   r5   I/home/ubuntu/.local/lib/python3.10/site-packages/kui/wsgi/applications.pyr/   $   s   
 r/   c                	   @  sd   e Zd ZU e Zded< dg g i de i i dd+ddZd,ddZd-d d!Z	d.d'd(Z
d.d)d*ZdS )/KuizImmutableAttribute[State]stateN)	templatesrouteshttp_middlewaresexception_handlerscors_configfactory_classresponse_convertersjson_encoderr9   Optional[BaseTemplates]r:   Iterable[BaseRoute]r;   List[MiddlewareType]r<   4Mapping[int | Type[BaseException], ErrorHandlerType]r=   Optional[CORSConfig]r>   r/   r?   *Mapping[type, Callable[..., HttpResponse]]r@   #Mapping[type, Callable[[Any], Any]]returnNonec          	      C  s   d| _ t | _t|| _t|  | _d| _ || _|| _	g |}t
|| _|| j |d ur9|tdi | t||g | _d S )NFr5   )should_exitr   r8   create_response_converterresponse_converterr   itemsr@   r>   r9   r#   exception_middlewareappendr!   r   router)	selfr9   r:   r;   r<   r=   r>   r?   r@   r5   r5   r6   __init__,   s   

zKui.__init__exc_class_or_status_codeint | Type[Exception]handlerr"   c                 C  s   | j || d S N)rN   add_exception_handler)rQ   rS   rU   r5   r5   r6   rW   K   s   zKui.add_exception_handler.Callable[[ErrorHandlerType], ErrorHandlerType]c                   s   d fdd}|S )Nfuncr"   rH   c                   s     |  | S rV   )rW   )rY   rS   rQ   r5   r6   	decoratorU   s   z(Kui.exception_handler.<locals>.decorator)rY   r"   rH   r"   r5   )rQ   rS   r[   r5   rZ   r6   exception_handlerR   s   zKui.exception_handlerenvironr   start_responser   Iterable[bytes]c           	      c  sx   | j |}tt| tt| zgz| jd|dd\}}||d< | }W n tyI   t	d}| j
|}|d u rCtd||}Y nw t|trU| j| }n| |}|||E d H  W z|jjt   |  W |  n|  w z|jjt   |  W |  w |  w W d    n1 sw   Y  W d    d S W d    d S 1 sw   Y  d S )Nr0   	PATH_INFO PATH_PARAMSi  z1No exception handler found for HTTPException(404))r>   r0   r   r&   r'   rP   searchgetr   r$   rN   lookup_handlerRuntimeError
isinstancetuplerL   
exit_stack__exit__sysexc_infobackground_tasksclose)	rQ   r]   r^   requestpath_paramsrU   responsehttp_exceptionerror_handlerr5   r5   r6   app[   sJ   




PzKui.appc                 C  s   | |d< |  ||S )Nrt   )rt   )rQ   r]   r^   r5   r5   r6   __call__   s   zKui.__call__)r9   rA   r:   rB   r;   rC   r<   rD   r=   rE   r>   r/   r?   rF   r@   rG   rH   rI   )rS   rT   rU   r"   rH   rI   )rS   rT   rH   rX   )r]   r   r^   r   rH   r_   )r1   r2   r3   r   r8   r4   r/   rR   rW   r\   rt   ru   r5   r5   r5   r6   r7   )   s   
 


	%r7   
convertersrF   rH   /functools._SingleDispatchCallable[HttpResponse]c                 C  s  t jddd}dd
d}|td| |tdd  |tdd  |tdd  |tdd  |tdd  |t	dd  |t
t |tt |tt |tt |tt |tt |tddd |tt |tt |  D ]
\}}||| q|S )z2
    Create a converter for convert response.
    argsr   rH   r)   c                  W  s   t dt| d  )Nz6Cannot find response_converter handler for this type: r   )	TypeErrortype)rx   r5   r5   r6   rL      s   z5create_response_converter.<locals>.response_converterret
Type[None]r
   c                 S  s   t d)NzEGet 'None'. Maybe you need to add a return statement to the function.)ry   )r{   r5   r5   r6   _none   s   z(create_response_converter.<locals>._noneNc                 S     | S rV   r5   xr5   r5   r6   <lambda>       z+create_response_converter.<locals>.<lambda>c                 S  r~   rV   r5   r   r5   r5   r6   r      r   c                 S  r~   rV   r5   r   r5   r5   r6   r      r   c                 S  r~   rV   r5   r   r5   r5   r6   r      r   c                 S  r~   rV   r5   r   r5   r5   r6   r      r   c                 S  r~   rV   r5   r   r5   r5   r6   r      r   c                 S  s   t t| |dS )N)download_name)r(   str)filepathr   r5   r5   r6   r      s    )rx   r   rH   r)   )r{   r|   rH   r
   rV   )	functoolssingledispatchregisterrz   r)   r   r   r   BaizeRouterr   dictr*   listrh   bytesr+   r   r   r-   r   r   r,   r   rM   )rv   rL   r}   type_	converterr5   r5   r6   rK      s2   
rK   )rv   rF   rH   rw   )C
__future__r   dataclassesr   rk   pathlibr   typesr   typingr   r   r   r   r	   r
   r   r   baize.datastructuresr   baize.typingr   r   
baize.wsgir   r   r   r   r   r   pydanticr   corsr   	responsesr   routingr   r   r   r   utilsr   r   utils.contextvarsr   r!   
exceptionsr"   r#   r$   requestsr%   r&   r'   r(   r)   r*   r+   r,   r-   r9   r.   	dataclassr/   r7   rK   r5   r5   r5   r6   <module>   s6    ( _