o
    :ήc&                     @   s   d dl 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 d d	lmZmZ d d
lmZmZmZmZ G dd dZdS )    N)StateURLPath)
Middleware)BaseHTTPMiddleware)ServerErrorMiddleware)ExceptionMiddleware)Request)Response)	BaseRouteRouter)ASGIAppReceiveScopeSendc                   @   s  e Zd ZdZ							dEdedejeje  dejeje	  dejej
ejejeegejeeje f f f  dejejej  d	ejejej  d
ejejd gejf  ddfddZdefddZedeje fddZedefddZejdeddfddZdedejdefddZdedede ddfddZ!dedejfd d!Z"	dFd"ed#edeje ddfd$d%Z#	dFd&ed#edeje ddfd'd(Z$d)e%d*ejddfd+d,Z&d-eje'ej(e f d.ejddfd/d0Z)ded1ejddfd2d3Z*			4dGd"ed5ejd6ejeje  deje d7eddfd8d9Z+	dFd"ed5ejdeje ddfd:d;Z,d-eje'ej(e f dejfd<d=Z-			4dGd"ed6ejeje  deje d7edejf
d>d?Z.	dFd"edeje dejfd@dAZ/dBedejfdCdDZ0dS )H	Starletteah  
    Creates an application instance.

    **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 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 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 be either
    standard functions, or async functions.
    FNdebugroutes
middlewareexception_handlers
on_startupon_shutdownlifespanreturnc                 C   sz   |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 S )Nz>Use either 'lifespan' or 'on_startup'/'on_shutdown', not both.)r   r   r   )_debugr   stater   routerdictr   listuser_middlewarebuild_middleware_stackmiddleware_stack)selfr   r   r   r   r   r   r    r"   =/tmp/pip-target-vg8gfxp4/lib/python/starlette/applications.py__init__)   s    zStarlette.__init__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 ]\}}	|dd|i|	}q8|S )Ni  )handlerr   )handlersr   appr"   )
r   r   items	Exceptionr   r   r   r   r   reversed)
r!   r   error_handlerr   keyvaluer   r'   clsoptionsr"   r"   r#   r   N   s*   

z Starlette.build_middleware_stackc                 C   s   | j jS N)r   r   r!   r"   r"   r#   r   j   s   zStarlette.routesc                 C   s   | j S r0   )r   r1   r"   r"   r#   r   n   s   zStarlette.debugr-   c                 C   s   || _ |  | _d S r0   )r   r   r    )r!   r-   r"   r"   r#   r   r   s   namepath_paramsc                 K   s   | j j|fi |S r0   )r   url_path_for)r!   r2   r3   r"   r"   r#   r4   w   s   zStarlette.url_path_forscopereceivesendc                    s"   | |d< |  |||I d H  d S )Nr'   )r    )r!   r5   r6   r7   r"   r"   r#   __call__z   s   zStarlette.__call__
event_typec                 C   s   | j |S r0   )r   on_event)r!   r9   r"   r"   r#   r:      s   zStarlette.on_eventpathr'   c                 C      | j j|||d dS )z
        We no longer document this API, and its usage is discouraged.
        Instead you should use the following approach:

        routes = [
            Mount(path, ...),
            ...
        ]

        app = Starlette(routes=routes)
        r'   r2   N)r   mount)r!   r;   r'   r2   r"   r"   r#   r>         zStarlette.mounthostc                 C   r<   )z
        We no longer document this API, and its usage is discouraged.
        Instead you should use the following approach:

        routes = [
            Host(path, ...),
            ...
        ]

        app = Starlette(routes=routes)
        r=   N)r   r@   )r!   r@   r'   r2   r"   r"   r#   r@      r?   zStarlette.hostmiddleware_classr/   c                 K   s(   | j dt|fi | |  | _d S )Nr   )r   insertr   r   r    )r!   rA   r/   r"   r"   r#   add_middleware   s   zStarlette.add_middlewareexc_class_or_status_coder%   c                 C   s   || j |< |  | _d S r0   )r   r   r    )r!   rD   r%   r"   r"   r#   add_exception_handler   s   
zStarlette.add_exception_handlerfuncc                 C   s   | j || d S r0   )r   add_event_handler)r!   r9   rF   r"   r"   r#   rG      s   zStarlette.add_event_handlerTroutemethodsinclude_in_schemac                 C   s   | j j|||||d d S N)rI   r2   rJ   r   	add_route)r!   r;   rH   rI   r2   rJ   r"   r"   r#   rM      s   

zStarlette.add_routec                 C   s   | j j|||d d S N)r2   r   add_websocket_route)r!   r;   rH   r2   r"   r"   r#   rP      s   zStarlette.add_websocket_routec                    s    dt jdt jf fdd}|S )NrF   r   c                    s     |  | S r0   )rE   rF   rD   r!   r"   r#   	decorator   s   z.Starlette.exception_handler.<locals>.decoratortypingCallable)r!   rD   rS   r"   rR   r#   exception_handler   s   zStarlette.exception_handlerc                    s&   dt jdt jf 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 = [
            Route(path, endpoint=..., ...),
            ...
        ]

        app = Starlette(routes=routes)
        rF   r   c                    s   j j|  d | S rK   rL   rQ   rJ   rI   r2   r;   r!   r"   r#   rS      s   z"Starlette.route.<locals>.decoratorrT   )r!   r;   rI   r2   rJ   rS   r"   rX   r#   rH      s   "
zStarlette.routec                    s"   dt jdt jf 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)
        rF   r   c                    s   j j|  d | S rN   rO   rQ   r2   r;   r!   r"   r#   rS      s   z,Starlette.websocket_route.<locals>.decoratorrT   )r!   r;   r2   rS   r"   rY   r#   websocket_route   s   zStarlette.websocket_routemiddleware_typec                    s.   |dksJ ddt jdt jf 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:

        middleware = [
            Middleware(...),
            ...
        ]

        app = Starlette(middleware=middleware)
        httpz/Currently only middleware("http") is supported.rF   r   c                    s    j t| d | S )N)dispatch)rC   r   rQ   r1   r"   r#   rS     s   z'Starlette.middleware.<locals>.decoratorrT   )r!   r[   rS   r"   r1   r#   r     s   zStarlette.middleware)FNNNNNNr0   )NNT)1__name__
__module____qualname____doc__boolrU   OptionalSequencer
   r   MappingAnyrV   r   r)   Unionr	   	AwaitableAsyncContextManagerr$   r   r   propertyListr   r   setterstrr   r4   r   r   r   r8   r:   r>   r@   typerC   intTyperE   rG   rM   rP   rW   rH   rZ   r   r"   r"   r"   r#   r      s   	
%




	



 
r   )rU   starlette.datastructuresr   r   starlette.middlewarer   starlette.middleware.baser   starlette.middleware.errorsr   starlette.middleware.exceptionsr   starlette.requestsr   starlette.responsesr	   starlette.routingr
   r   starlette.typesr   r   r   r   r   r"   r"   r"   r#   <module>   s    