a
     d)                     @   s   d dl Z 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mZ e jdddZG dd dZdS )    N)StateURLPath)
Middleware)BaseHTTPMiddleware)ServerErrorMiddleware)ExceptionMiddleware)Request)Response)	BaseRouteRouter)ASGIAppLifespanReceiveScopeSendAppType	Starlette)boundc                   @   sJ  e Zd ZdZd8deejeje  ejeje	  ejej
ejejeegejeeje f f f  ejejej  ejejej  ejed  dd	ddZedd	d
Zeeje dddZeejedddZeeeddddZeejdddZ d9eeeje ddddZ!d:eeeje ddddZ"e#ejddddZ$eje%ej&e f ejddd d!Z'eejdd"d#d$Z(d;eejejeje  eje edd&d'd(Z)d<eejeje dd)d*d+Z*eje%ej&e f ejd,d-d.Z+d=eejeje  eje eejd/d0d1Z,d>eeje ejd2d3d4Z-eejd5d6d7Z.dS )?r   aL  
    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.
    * **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.
    FNr   )	selfdebugroutes
middlewareexception_handlers
on_startupon_shutdownlifespanreturnc                 C   sv   |d u s |d u r|d u s J d|| _ t | _t||||d| _|d u rLi nt|| _|d u rbg nt|| _d | _	d S )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    r#   _/var/www/html/stable-diffusion-webui/venv/lib/python3.9/site-packages/starlette/applications.py__init__/   s     zStarlette.__init__)r   c           
      C   s   | j }d }i }| j D ]"\}}|dtfv r2|}q|||< qtt||dg| j tt||dg }| j}t	|D ]\}}	|f d|i|	}qp|S )Ni  )handlerr   )handlersr   app)
r   r   items	Exceptionr   r   r!   r   r   reversed)
r   r   Zerror_handlerr   keyvaluer   r(   clsoptionsr#   r#   r$   build_middleware_stackR   s*    

z Starlette.build_middleware_stackc                 C   s   | j jS N)r   r   r   r#   r#   r$   r   n   s    zStarlette.routes)_Starlette__namepath_paramsr   c                 K   s   | j j|fi |S r1   )r   url_path_for)r   r3   r4   r#   r#   r$   r5   s   s    zStarlette.url_path_for)scopereceivesendr   c                    s4   | |d< | j d u r|  | _ |  |||I d H  d S )Nr(   )r"   r0   )r   r6   r7   r8   r#   r#   r$   __call__v   s    

zStarlette.__call__)
event_typer   c                 C   s   | j |S r1   )r   on_event)r   r:   r#   r#   r$   r;   |   s    zStarlette.on_event)pathr(   namer   c                 C   s   | j j|||d d S N)r(   r=   )r   mount)r   r<   r(   r=   r#   r#   r$   r?      s    zStarlette.mount)hostr(   r=   r   c                 C   s   | j j|||d d S r>   )r   r@   )r   r@   r(   r=   r#   r#   r$   r@      s    zStarlette.host)middleware_classr/   r   c                 K   s0   | j d urtd| jdt|fi | d S )Nz6Cannot add middleware after an application has startedr   )r"   RuntimeErrorr!   insertr   )r   rA   r/   r#   r#   r$   add_middleware   s    
zStarlette.add_middleware)exc_class_or_status_coder&   r   c                 C   s   || j |< d S r1   )r   )r   rE   r&   r#   r#   r$   add_exception_handler   s    zStarlette.add_exception_handler)r:   funcr   c                 C   s   | j || d S r1   )r   add_event_handler)r   r:   rG   r#   r#   r$   rH      s    zStarlette.add_event_handlerT)r<   routemethodsr=   include_in_schemar   c                 C   s   | j j|||||d d S N)rJ   r=   rK   r   	add_route)r   r<   rI   rJ   r=   rK   r#   r#   r$   rN      s    
zStarlette.add_route)r<   rI   r=   r   c                 C   s   | j j|||d d S N)r=   r   add_websocket_route)r   r<   rI   r=   r#   r#   r$   rQ      s    zStarlette.add_websocket_route)rE   r   c                    s*   t dt tjtj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.rG   r   c                    s     |  | S r1   )rF   rG   rE   r   r#   r$   	decorator   s    z.Starlette.exception_handler.<locals>.decoratorwarningswarnDeprecationWarningtypingCallable)r   rE   rU   r#   rT   r$   exception_handler   s    zStarlette.exception_handler)r<   rJ   r=   rK   r   c                    s0   t dt tjtj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.rR   c                    s   j j|  d | S rL   rM   rS   rK   rJ   r=   r<   r   r#   r$   rU      s    z"Starlette.route.<locals>.decoratorrV   )r   r<   rJ   r=   rK   rU   r#   r]   r$   rI      s     
zStarlette.route)r<   r=   r   c                    s,   t dt tjtj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.rR   c                    s   j j|  d | S rO   rP   rS   r=   r<   r   r#   r$   rU      s    z,Starlette.websocket_route.<locals>.decoratorrV   )r   r<   r=   rU   r#   r^   r$   websocket_route   s    
zStarlette.websocket_route)middleware_typer   c                    s8   t dt |dksJ dtjtj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.httpz/Currently only middleware("http") is supported.rR   c                    s    j t| d | S )N)dispatch)rD   r   rS   r2   r#   r$   rU     s    z'Starlette.middleware.<locals>.decoratorrV   )r   r`   rU   r#   r2   r$   r      s    zStarlette.middleware)FNNNNNN)N)N)NNT)N)NNT)N)/__name__
__module____qualname____doc__boolrZ   OptionalSequencer
   r   MappingAnyr[   r   r*   Unionr	   	Awaitabler   r%   r   r0   propertyListr   strr   r5   r   r   r   r9   r;   r?   r@   typerD   intTyperF   rH   rN   rQ   r\   rI   r_   r   r#   r#   r#   r$   r      s      	    	#  	       ! 
)rZ   rW   Zstarlette.datastructuresr   r   Zstarlette.middlewarer   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   TypeVarr   r   r#   r#   r#   r$   <module>   s   