U
    nËj´)  ã                   @  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)Ú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.   ú:/tmp/pip-unpacked-wheel-pzfrzqh9/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   )Ú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@   rA   r.   r.   r/   rB   i   s    zStarlette.url_path_forr   r   r   )ÚscopeÚreceiveÚsendr&   c                 Ã  s4   | |d< | j d kr|  ¡ | _ |   |||¡I d H  d S )Nr8   )r-   r<   )r   rC   rD   rE   r.   r.   r/   Ú__call__l   s    

zStarlette.__call__útyping.Callable)Ú
event_typer&   c                 C  s   | j  |¡S r=   )r)   Úon_event)r   rH   r.   r.   r/   rI   r   s    zStarlette.on_eventz
str | None)Úpathr8   r@   r&   c                 C  s   | j j|||d� d S ©N)r8   r@   )r)   Úmount)r   rJ   r8   r@   r.   r.   r/   rL   u   s    zStarlette.mount)Úhostr8   r@   r&   c                 C  s   | j j|||d� d S rK   )r)   rM   )r   rM   r8   r@   r.   r.   r/   rM   x   s    zStarlette.hostz_MiddlewareFactory[P]zP.argszP.kwargs)Úmiddleware_classr:   r;   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   rN   r:   r;   r.   r.   r/   Úadd_middleware{   s    
zStarlette.add_middlewarezint | type[Exception]r   )Úexc_class_or_status_coder1   r&   c                 C  s   || j |< d S r=   )r"   )r   rR   r1   r.   r.   r/   Úadd_exception_handler…   s    zStarlette.add_exception_handler)rH   Úfuncr&   c                 C  s   | j  ||¡ d S r=   )r)   Úadd_event_handler)r   rH   rT   r.   r.   r/   rU   Œ   s    zStarlette.add_event_handlerTzAtyping.Callable[[Request], typing.Awaitable[Response] | Response]zlist[str] | None)rJ   ÚrouteÚmethodsr@   Úinclude_in_schemar&   c                 C  s   | j j|||||d� d S ©N)rW   r@   rX   ©r)   Ú	add_route)r   rJ   rV   rW   r@   rX   r.   r.   r/   r[   “   s    zStarlette.add_routez4typing.Callable[[WebSocket], typing.Awaitable[None]])rJ   rV   r@   r&   c                 C  s   | j j|||d� d S ©N)r@   ©r)   Úadd_websocket_route)r   rJ   rV   r@   r.   r.   r/   r^   �   s    zStarlette.add_websocket_route)rR   r&   c                   s&   t  dt¡ dddœ‡ ‡fdd„}|S )Nz¢The `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.rG   ©rT   r&   c                   s   ˆ  ˆ | ¡ | S r=   )rS   ©rT   ©rR   r   r.   r/   Ú	decorator¬   s    z.Starlette.exception_handler.<locals>.decorator©ÚwarningsÚwarnÚDeprecationWarning)r   rR   rb   r.   ra   r/   Úexception_handler¥   s    ýzStarlette.exception_handler)rJ   rW   r@   rX   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)
        z“The `route` decorator is deprecated, and will be removed in version 1.0.0. Refer to https://www.starlette.io/routing/ for the recommended approach.rG   r_   c                   s   ˆj jˆ| ˆˆˆ d� | S rY   rZ   r`   ©rX   rW   r@   rJ   r   r.   r/   rb   Æ   s    ûz"Starlette.route.<locals>.decoratorrc   )r   rJ   rW   r@   rX   rb   r.   rh   r/   rV   ²   s    ý
zStarlette.route)rJ   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)
        z¯The `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.rG   r_   c                   s   ˆj jˆ| ˆ d� | S r\   r]   r`   ©r@   rJ   r   r.   r/   rb   à   s    z,Starlette.websocket_route.<locals>.decoratorrc   )r   rJ   r@   rb   r.   ri   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)
        z¨The `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.Úhttpz/Currently only middleware("http") is supported.rG   r_   c                   s   ˆ j t| d� | S )N)Údispatch)rQ   r
   r`   r>   r.   r/   rb   õ   s    z'Starlette.middleware.<locals>.decorator)rd   re   rf   r'   )r   rk   rb   r.   r>   r/   r!   æ   s    ýzStarlette.middleware)FNNNNNN)N)N)NNT)N)NNT)N)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r0   r<   Úpropertyr    rB   rF   rI   rL   rM   rQ   rS   rU   r[   r^   rg   rV   rj   r!   r.   r.   r.   r/   r      sB          ø 1
   ú ü   û )(Ú
__future__r   ÚsysÚtypingrd   Ú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   ÚTypeVarr   r   r   r.   r.   r.   r/   Ú<module>   s&   
 