o
    ik(                     @  s   d dl mZ d dlZd dlmZmZmZmZ d dlm	Z	m
Z
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 d d	lmZ d d
l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#m$Z$m%Z% d dl&m'Z' edddZ(e
dZ)G dd dZ*dS )    )annotationsN)	AwaitableCallableMappingSequence)Any	ParamSpecTypeVar)StateURLPath)
Middleware_MiddlewareFactory)BaseHTTPMiddleware)ServerErrorMiddleware)ExceptionMiddleware)Request)Response)	BaseRouteRouter)ASGIAppExceptionHandlerLifespanReceiveScopeSend)	WebSocketAppType	Starlette)boundPc                   @  s   e Zd ZdZ							d]d^ddZd_ddZed`ddZdad"d#Zdbd*d+Z	dcd.d/Z
ddded3d4Zdddfd6d7Zdgd>d?ZdhdDdEZdidGdHZ			IdjdkdOdPZ	dddldRdSZdmdTdUZ			IdjdndVdWZdddodXdYZdpd[d\ZdS )qr   z!Creates an Starlette application.FNselfr   debugboolroutesSequence[BaseRoute] | None
middlewareSequence[Middleware] | Noneexception_handlers%Mapping[Any, ExceptionHandler] | None
on_startup"Sequence[Callable[[], Any]] | Noneon_shutdownlifespanLifespan[AppType] | NonereturnNonec                 C  sv   |du s|du r|du sJ d|| _ t | _t||||d| _|du r&i nt|| _|du r1g nt|| _d| _	dS )a  Initializes the application.

        Parameters:
            debug: Boolean indicating if debug tracebacks should be returned on errors.
            routes: A list of routes to serve incoming HTTP and WebSocket requests.
            middleware: A list of middleware to run for every request. A starlette
                application will always automatically include two middleware classes.
                `ServerErrorMiddleware` is added as the very outermost middleware, to handle
                any uncaught errors occurring anywhere in the entire stack.
                `ExceptionMiddleware` is added as the very innermost middleware, to deal
                with handled exception cases occurring in the routing or endpoints.
            exception_handlers: A mapping of either integer status codes,
                or exception class types onto callables which handle the exceptions.
                Exception handler callables should be of the form
                `handler(request, exc) -> response` and may be either standard functions, or
                async functions.
            on_startup: A list of callables to run on application startup.
                Startup handler callables do not take any arguments, and may be either
                standard functions, or async functions.
            on_shutdown: A list of callables to run on application shutdown.
                Shutdown handler callables do not take any arguments, and may be either
                standard functions, or async functions.
            lifespan: A lifespan context function, which can be used to perform
                startup and shutdown tasks. This is a newer style that replaces the
                `on_startup` and `on_shutdown` handlers. Use one or the other, not both.
        Nz>Use either 'lifespan' or 'on_startup'/'on_shutdown', not both.)r)   r+   r,   )
r!   r
   stater   routerdictr'   listuser_middlewaremiddleware_stack)r    r!   r#   r%   r'   r)   r+   r,    r6   M/home/app/Keep/.python/lib/python3.10/site-packages/starlette/applications.py__init__   s   &
zStarlette.__init__r   c                 C  s   | j }d }i }| j D ]\}}|dtfv r|}q|||< qtt||dg| j tt||dg }| j}t	|D ]\}}	}
||g|	R i |
}q8|S )Ni  )handlerr!   )handlersr!   )
r!   r'   items	Exceptionr   r   r4   r   r1   reversed)r    r!   Zerror_handlerr'   keyvaluer%   appclsargskwargsr6   r6   r7   build_middleware_stackJ   s"   
z Starlette.build_middleware_stacklist[BaseRoute]c                 C  s   | j jS N)r1   r#   r    r6   r6   r7   r#   `   s   zStarlette.routesnamestrpath_paramsr   r   c                K  s   | j j|fi |S rF   )r1   url_path_for)r    rH   rJ   r6   r6   r7   rK   d   s   zStarlette.url_path_forscoper   receiver   sendr   c                   s6   | |d< | j d u r|  | _ |  |||I d H  d S )Nr@   )r5   rD   )r    rL   rM   rN   r6   r6   r7   __call__g   s
   

zStarlette.__call__
event_typer   c                 C  s   | j |S rF   )r1   on_event)r    rP   r6   r6   r7   rQ   m   s   zStarlette.on_eventpathr@   
str | Nonec                 C     | j j|||d d S N)r@   rH   )r1   mount)r    rR   r@   rH   r6   r6   r7   rV   p      zStarlette.mounthostc                 C  rT   rU   )r1   rX   )r    rX   r@   rH   r6   r6   r7   rX   s   rW   zStarlette.hostmiddleware_class_MiddlewareFactory[P]rB   P.argsrC   P.kwargsc                 O  s6   | j d ur	td| jdt|g|R i | d S )Nz6Cannot add middleware after an application has startedr   )r5   RuntimeErrorr4   insertr   )r    rY   rB   rC   r6   r6   r7   add_middlewarev   s   
$zStarlette.add_middlewareexc_class_or_status_codeint | type[Exception]r9   r   c                 C  s   || j |< d S rF   )r'   )r    r`   r9   r6   r6   r7   add_exception_handler   s   zStarlette.add_exception_handlerfuncc                 C  s   | j || d S rF   )r1   add_event_handler)r    rP   rc   r6   r6   r7   rd      s   zStarlette.add_event_handlerTroute3Callable[[Request], Awaitable[Response] | Response]methodslist[str] | Noneinclude_in_schemac                 C  s   | j j|||||d d S N)rg   rH   ri   r1   	add_route)r    rR   re   rg   rH   ri   r6   r6   r7   rl      s   zStarlette.add_route&Callable[[WebSocket], Awaitable[None]]c                 C  rT   N)rH   r1   add_websocket_route)r    rR   re   rH   r6   r6   r7   rp      s   zStarlette.add_websocket_routec                   s    t dt d fdd}|S )NzThe `exception_handler` decorator is deprecated, and will be removed in version 1.0.0. Refer to https://starlette.dev/exceptions/ for the recommended approach.rc   r   r.   c                   s     |  | S rF   )rb   rc   r`   r    r6   r7   	decorator   s   z.Starlette.exception_handler.<locals>.decoratorrc   r   r.   r   warningswarnDeprecationWarning)r    r`   rs   r6   rr   r7   exception_handler   s   zStarlette.exception_handlerc                   s&   t dt d fdd}|S )	z
        We no longer document this decorator style API, and its usage is discouraged.
        Instead you should use the following approach:

        >>> routes = [Route(path, endpoint=...), ...]
        >>> app = Starlette(routes=routes)
        zThe `route` decorator is deprecated, and will be removed in version 1.0.0. Refer to https://starlette.dev/routing/ for the recommended approach.rc   r   r.   c                   s   j j|  d | S rj   rk   rq   ri   rg   rH   rR   r    r6   r7   rs      s   z"Starlette.route.<locals>.decoratorNrt   ru   )r    rR   rg   rH   ri   rs   r6   rz   r7   re      s   
zStarlette.routec                   s"   t dt d fdd}|S )	a  
        We no longer document this decorator style API, and its usage is discouraged.
        Instead you should use the following approach:

        >>> routes = [WebSocketRoute(path, endpoint=...), ...]
        >>> app = Starlette(routes=routes)
        zThe `websocket_route` decorator is deprecated, and will be removed in version 1.0.0. Refer to https://starlette.dev/routing/#websocket-routing for the recommended approach.rc   r   r.   c                   s   j j|  d | S rn   ro   rq   rH   rR   r    r6   r7   rs      s   z,Starlette.websocket_route.<locals>.decoratorNrt   ru   )r    rR   rH   rs   r6   r{   r7   websocket_route   s   zStarlette.websocket_routemiddleware_typec                   s.   t dt |dksJ dd
 fdd}|S )z
        We no longer document this decorator style API, and its usage is discouraged.
        Instead you should use the following approach:

        >>> middleware = [Middleware(...), ...]
        >>> app = Starlette(middleware=middleware)
        zThe `middleware` decorator is deprecated, and will be removed in version 1.0.0. Refer to https://starlette.dev/middleware/#using-middleware for recommended approach.httpz/Currently only middleware("http") is supported.rc   r   r.   c                   s    j t| d | S )N)dispatch)r_   r   rq   rG   r6   r7   rs      s   z'Starlette.middleware.<locals>.decoratorNrt   ru   )r    r}   rs   r6   rG   r7   r%      s   zStarlette.middleware)FNNNNNN)r    r   r!   r"   r#   r$   r%   r&   r'   r(   r)   r*   r+   r*   r,   r-   r.   r/   )r.   r   )r.   rE   )rH   rI   rJ   r   r.   r   )rL   r   rM   r   rN   r   r.   r/   )rP   rI   r.   r   rF   )rR   rI   r@   r   rH   rS   r.   r/   )rX   rI   r@   r   rH   rS   r.   r/   )rY   rZ   rB   r[   rC   r\   r.   r/   )r`   ra   r9   r   r.   r/   )rP   rI   rc   r   r.   r/   )NNT)rR   rI   re   rf   rg   rh   rH   rS   ri   r"   r.   r/   )rR   rI   re   rm   rH   rS   r.   r/   )r`   ra   r.   r   )
rR   rI   rg   rh   rH   rS   ri   r"   r.   r   )rR   rI   rH   rS   r.   r   )r}   rI   r.   r   )__name__
__module____qualname____doc__r8   rD   propertyr#   rK   rO   rQ   rV   rX   r_   rb   rd   rl   rp   ry   re   r|   r%   r6   r6   r6   r7   r      sD    
1







 )+
__future__r   rv   collections.abcr   r   r   r   typingr   r   r	   Zstarlette.datastructuresr
   r   Zstarlette.middlewarer   r   Zstarlette.middleware.baser   Zstarlette.middleware.errorsr   Zstarlette.middleware.exceptionsr   Zstarlette.requestsr   Zstarlette.responsesr   Zstarlette.routingr   r   Zstarlette.typesr   r   r   r   r   r   Zstarlette.websocketsr   r   r   r   r6   r6   r6   r7   <module>   s"     