U
    Rh)                     @  s  d dl mZ d dlZd dlZd dlZejdkr<d dlmZ nd dl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j$dddZ%edZ&G dd dZ'dS )    )annotationsN)   
   )	ParamSpec)StateURLPath)
Middleware_MiddlewareFactory)BaseHTTPMiddleware)ServerErrorMiddleware)ExceptionMiddleware)Request)Response)	BaseRouteRouter)ASGIAppExceptionHandlerLifespanReceiveScopeSend)	WebSocketAppType	Starlette)ZboundPc                   @  sf  e Zd ZdZdQdddddd	d	d
dd	ddZddddZeddddZddddddZdddddd d!Z	dd"d#d$d%Z
dRddd&dd'd(d)ZdSddd&dd*d+d,Zd-d.d/dd0d1d2Zd3d4dd5d6d7Zdd"dd8d9d:ZdTdd<d=d&ddd>d?d@ZdUddAd&ddBdCdDZd3d"dEdFdGZdVdd=d&dd"dHdIdJZdWdd&d"dKdLdMZdd"dNdOdPZdS )Xr   z!Creates an Starlette application.FNr   boolz!typing.Sequence[BaseRoute] | Nonez"typing.Sequence[Middleware] | Nonez3typing.Mapping[typing.Any, ExceptionHandler] | Nonez7typing.Sequence[typing.Callable[[], typing.Any]] | NonezLifespan[AppType] | NoneNone)	selfdebugroutes
middlewareexception_handlers
on_startupon_shutdownlifespanreturnc                 C  sv   |dks |dkr|dks t d|| _t | _t||||d| _|dkrLi nt|| _|dkrbg 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$   )AssertionErrorr   r   stater   routerdictr!   listuser_middlewaremiddleware_stack)r   r   r   r    r!   r"   r#   r$    r-   <./venv/lib/python3.8/site-packages/starlette/applications.py__init__   s    &zStarlette.__init__r   )r%   c                 C  s   | j }d }i }| j D ]"\}}|dtfkr2|}q|||< qtt||dg| j tt||dg }| j}t	|D ]\}}	}
||f|	|
}qp|S )Ni  )handlerr   )Zhandlersr   )
r   r!   items	Exceptionr   r   r+   r   r(   reversed)r   r   Zerror_handlerr!   keyvaluer    appclsargskwargsr-   r-   r.   build_middleware_stackO   s"    
z Starlette.build_middleware_stackzlist[BaseRoute]c                 C  s   | j jS N)r(   r   r   r-   r-   r.   r   e   s    zStarlette.routesstrz
typing.Anyr   )namepath_paramsr%   c                K  s   | j j|f|S r;   )r(   url_path_for)r   r>   r?   r-   r-   r.   r@   i   s    zStarlette.url_path_forr   r   r   )scopereceivesendr%   c                   s4   | |d< | j d kr|  | _ |  |||I d H  d S )Nr6   )r,   r:   )r   rA   rB   rC   r-   r-   r.   __call__l   s    

zStarlette.__call__typing.Callable)
event_typer%   c                 C  s   | j |S r;   )r(   on_event)r   rF   r-   r-   r.   rG   r   s    zStarlette.on_eventz
str | None)pathr6   r>   r%   c                 C  s   | j j|||d d S N)r6   r>   )r(   mount)r   rH   r6   r>   r-   r-   r.   rJ   u   s    zStarlette.mount)hostr6   r>   r%   c                 C  s   | j j|||d d S rI   )r(   rK   )r   rK   r6   r>   r-   r-   r.   rK   x   s    zStarlette.hostz_MiddlewareFactory[P]zP.argszP.kwargs)middleware_classr8   r9   r%   c                 O  s0   | j d k	rtd| jdt|f|| d S )Nz6Cannot add middleware after an application has startedr   )r,   RuntimeErrorr+   insertr   )r   rL   r8   r9   r-   r-   r.   add_middleware{   s    
zStarlette.add_middlewarezint | type[Exception]r   )exc_class_or_status_coder0   r%   c                 C  s   || j |< d S r;   )r!   )r   rP   r0   r-   r-   r.   add_exception_handler   s    zStarlette.add_exception_handler)rF   funcr%   c                 C  s   | j || d S r;   )r(   add_event_handler)r   rF   rR   r-   r-   r.   rS      s    zStarlette.add_event_handlerTzAtyping.Callable[[Request], typing.Awaitable[Response] | Response]zlist[str] | None)rH   routemethodsr>   include_in_schemar%   c                 C  s   | j j|||||d d S N)rU   r>   rV   r(   	add_route)r   rH   rT   rU   r>   rV   r-   r-   r.   rY      s    zStarlette.add_routez4typing.Callable[[WebSocket], typing.Awaitable[None]])rH   rT   r>   r%   c                 C  s   | j j|||d d S N)r>   r(   add_websocket_route)r   rH   rT   r>   r-   r-   r.   r\      s    zStarlette.add_websocket_route)rP   r%   c                   s&   t dt ddd fdd}|S )NzThe `exception_handler` decorator is deprecated, and will be removed in version 1.0.0. Refer to https://www.starlette.io/exceptions/ for the recommended approach.rE   rR   r%   c                   s     |  | S r;   )rQ   rR   rP   r   r-   r.   	decorator   s    z.Starlette.exception_handler.<locals>.decoratorwarningswarnDeprecationWarning)r   rP   r`   r-   r_   r.   exception_handler   s    zStarlette.exception_handler)rH   rU   r>   rV   r%   c                   s,   t dt d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:

        >>> 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://www.starlette.io/routing/ for the recommended approach.rE   r]   c                   s   j j|  d | S rW   rX   r^   rV   rU   r>   rH   r   r-   r.   r`      s    z"Starlette.route.<locals>.decoratorra   )r   rH   rU   r>   rV   r`   r-   rf   r.   rT      s    
zStarlette.route)rH   r>   r%   c                   s(   t dt dd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://www.starlette.io/routing/#websocket-routing for the recommended approach.rE   r]   c                   s   j j|  d | S rZ   r[   r^   r>   rH   r   r-   r.   r`      s    z,Starlette.websocket_route.<locals>.decoratorra   )r   rH   r>   r`   r-   rg   r.   websocket_route   s    zStarlette.websocket_route)middleware_typer%   c                   s4   t dt |dkstdd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://www.starlette.io/middleware/#using-middleware for recommended approach.Zhttpz/Currently only middleware("http") is supported.rE   r]   c                   s    j t| d | S )N)dispatch)rO   r
   r^   r<   r-   r.   r`      s    z'Starlette.middleware.<locals>.decorator)rb   rc   rd   r&   )r   ri   r`   r-   r<   r.   r       s    zStarlette.middleware)FNNNNNN)N)N)NNT)N)NNT)N)__name__
__module____qualname____doc__r/   r:   propertyr   r@   rD   rG   rJ   rK   rO   rQ   rS   rY   r\   re   rT   rh   r    r-   r-   r-   r.   r      sB           1
        )(Z
__future__r   systypingrb   version_infor   Ztyping_extensionsZ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   ZTypeVarr   r   r   r-   r-   r-   r.   <module>   s&   
 