o
    㥵i@!                     @  s  d dl mZ d dl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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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/m0Z0 ddl1m2Z2 ddl%m3Z3 ddl4m5Z5m6Z6m7Z7 ddl8m9Z9m:Z: ddl;m<Z<m=Z=m>Z>m?Z?m@Z@ ddl'mAZAmBZBmCZCmDZDmEZEmFZF ddl)mZ ddlGmHZH ede:dZIejJG dd dZKG dd dZLd%d#d$ZMdS )&    )annotationsN)PurePath)AsyncGeneratorType)	AnyCallableIterableListMappingNoReturnOptionalTypeTypeVar)FilesHostsPagesSubpaths)Router)URL)ReceiveScopeSend)	BaseModel)Literal   )
CORSConfig)create_json_encoder)AsyncViewType	BaseRouteMiddlewareTypeNoMatchFound)ImmutableAttributeState)context_setter   )
allow_cors)ErrorHandlerTypeExceptionMiddlewareHTTPException)LifespanLifespanCallback)HttpRequest	WebSockethttp_connection_varrequest_varwebsocket_var)FileResponseHttpResponseJSONResponsePlainTextResponseRedirectResponseSendEventResponse)BaseTemplatesLifespanCallbackTypeVar)boundc                   @  s&   e Zd ZU eZded< eZded< dS )FactoryClasszType[HttpRequest]httpzType[WebSocket]	websocketN)__name__
__module____qualname__r*   r9   __annotations__r+   r:    r?   r?   I/home/ubuntu/.local/lib/python3.10/site-packages/kui/asgi/applications.pyr8   9   s   
 r8   c                   @  s   e Zd ZU e Zded< dg g g 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>d2d3Zd>d4d5Zd>d6d7Zd>d8d9ZdS )?KuizImmutableAttribute[State]stateN)	templates
on_startupon_shutdownrouteshttp_middlewaressocket_middlewaresexception_handlerscors_configfactory_classresponse_convertersjson_encoderrC   Optional[BaseTemplates]rD   List[LifespanCallback]rE   rF   Iterable[BaseRoute]rG   List[MiddlewareType]rH   rI   4Mapping[int | Type[BaseException], ErrorHandlerType]rJ   Optional[CORSConfig]rK   r8   rL   *Mapping[type, Callable[..., HttpResponse]]rM   #Mapping[type, Callable[[Any], Any]]returnNonec                C  s   d| _ t | _t|
| _t|  | _|	| _|| _	t
t|t|| _g |}t|| _|| j |d urB|tdi | t|||| _d S )NFr?   )should_exitr!   rB   create_response_converterresponse_converterr   itemsrM   rK   rC   r(   copylifespanr&   exception_middlewareappendr$   r   router)selfrC   rD   rE   rF   rG   rH   rI   rJ   rK   rL   rM   r?   r?   r@   __init__B   s   

zKui.__init__exc_class_or_status_codeint | Type[Exception]handlerr%   c                 C  s   | j || d S N)r^   add_exception_handler)ra   rc   re   r?   r?   r@   rg   d   s   zKui.add_exception_handler.Callable[[ErrorHandlerType], ErrorHandlerType]c                   s   d fdd}|S )Nfuncr%   rV   c                   s     |  | S rf   )rg   )ri   rc   ra   r?   r@   	decoratorn   s   z(Kui.exception_handler.<locals>.decorator)ri   r%   rV   r%   r?   )ra   rc   rk   r?   rj   r@   exception_handlerk   s   zKui.exception_handlerri   r6   c                 C     | j j| |S rf   )r]   rD   r_   ra   ri   r?   r?   r@   rD   t      zKui.on_startupc                 C  rm   rf   )r]   rE   r_   rn   r?   r?   r@   rE   x   ro   zKui.on_shutdownscoper   receiver   sendr   c                   s"   |d }t | ||||I d H S )Ntype)getattr)ra   rp   rq   rr   
scope_typer?   r?   r@   app|   s   zKui.appc           
        s  | j |||}tt| tt| zz| jd|d \}}||d< | I d H }W n" tyO   td}| j	
|}	|	d u rFtd|	|I d H }Y nw t|tr[| j| }n| |}||||I d H W z|jjt  I d H  | I d H  W | I d H  n| I d H  w W  d    W  d    S z|jjt  I d H  | I d H  W | I d H  w | I d H  w 1 sw   Y  W d    d S 1 sw   Y  d S )Nr9   pathpath_paramsi  z1No exception handler found for HTTPException(404))rK   r9   r"   r,   r-   r`   searchr   r'   r^   lookup_handlerRuntimeError
isinstancetuplerZ   
exit_stack	__aexit__sysexc_infobackground_tasksclose)
ra   rp   rq   rr   requestrx   re   responsehttp_exceptionerror_handlerr?   r?   r@   r9      sH   

  4zKui.httpc                   s\  | j |||}tt| tt| zkz| jd|d \}}||d< W n2 tyY   |dI d H  Y W |j	j
t  I d H  | I d H  W  d    W  d    S w | I d H W |j	j
t  I d H  | I d H  W  d    W  d    S |j	j
t  I d H  | I d H  w 1 sw   Y  W d    d S 1 sw   Y  d S )Nr:   rw   rx   i  )rK   r:   r"   r,   r.   r`   ry   r   r   r~   r   r   r   r   )ra   rp   rq   rr   r:   rx   re   r?   r?   r@   r:      s2   
4zKui.websocketc                   s"   | |d< |  |||I d H  d S )Nrv   )rv   )ra   rp   rq   rr   r?   r?   r@   __call__   s   zKui.__call__)rC   rN   rD   rO   rE   rO   rF   rP   rG   rQ   rH   rQ   rI   rR   rJ   rS   rK   r8   rL   rT   rM   rU   rV   rW   )rc   rd   re   r%   rV   rW   )rc   rd   rV   rh   )ri   r6   rV   r6   )rp   r   rq   r   rr   r   rV   rW   )r;   r<   r=   r    rB   r>   r8   rb   rg   rl   rD   rE   rv   r9   r:   r   r?   r?   r?   r@   rA   ?   s,   
 
"

	



#rA   
convertersrT   rV   /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   rV   r0   c                  W  s   t dt| d  )Nz6Cannot find response_converter handler for this type: r   )	TypeErrorrs   )r   r?   r?   r@   rZ      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.)r   )r   r?   r?   r@   _none   s   z(create_response_converter.<locals>._noneNc                 S     | S rf   r?   xr?   r?   r@   <lambda>       z+create_response_converter.<locals>.<lambda>c                 S  r   rf   r?   r   r?   r?   r@   r      r   c                 S  r   rf   r?   r   r?   r?   r@   r      r   c                 S  r   rf   r?   r   r?   r?   r@   r      r   c                 S  r   rf   r?   r   r?   r?   r@   r      r   c                 S  r   rf   r?   r   r?   r?   r@   r      r   c                 S  s   t t| |dS )N)download_name)r/   str)filepathr   r?   r?   r@   r      s    )r   r   rV   r0   )r   r   rV   r
   rf   )	functoolssingledispatchregisterrs   r0   r   r   r   BaizeRouterr   dictr1   listr}   bytesr2   r   r   r4   r   r   r3   r   r[   )r   rZ   r   type_	converterr?   r?   r@   rY      s2   
rY   )r   rT   rV   r   )N
__future__r   r\   dataclassesr   r   pathlibr   typesr   typingr   r   r   r   r	   r
   r   r   r   
baize.asgir   r   r   r   r   r   baize.datastructuresr   baize.typingr   r   r   pydanticr   typing_extensionsr   corsr   	responsesr   routingr   r   r   r   utilsr    r!   utils.contextvarsr"   r$   
exceptionsr%   r&   r'   r]   r(   r)   requestsr*   r+   r,   r-   r.   r/   r0   r1   r2   r3   r4   rC   r5   r6   	dataclassr8   rA   rY   r?   r?   r?   r@   <module>   s>    , ~