U
    &%e)                     @   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 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 )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$   U/var/www/html/Darija-Ai-API/env/lib/python3.8/site-packages/starlette/applications.py__init__/   s&       zStarlette.__init__)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 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|S r2   )r   url_path_for)r   r4   r5   r$   r$   r%   r6   s   s    zStarlette.url_path_for)scopereceivesendr   c                    s4   | |d< | j d kr|  | _ |  |||I d H  d S )Nr)   )r#   r1   )r   r7   r8   r9   r$   r$   r%   __call__v   s    

zStarlette.__call__)
event_typer   c                 C   s   | j |S r2   )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   rA   )r   rA   r)   r>   r$   r$   r%   rA      s    zStarlette.host)middleware_classr0   r   c                 K   s,   | j d k	rtd| jdt|f| d S )Nz6Cannot add middleware after an application has startedr   )r#   RuntimeErrorr"   insertr   )r   rB   r0   r$   r$   r%   add_middleware   s    
zStarlette.add_middleware)exc_class_or_status_coder'   r   c                 C   s   || j |< d S r2   )r   )r   rF   r'   r$   r$   r%   add_exception_handler   s    zStarlette.add_exception_handler)r;   funcr   c                 C   s   | j || d S r2   )r   add_event_handler)r   r;   rH   r$   r$   r%   rI      s    zStarlette.add_event_handlerT)r=   routemethodsr>   include_in_schemar   c                 C   s   | j j|||||d d S N)rK   r>   rL   r   	add_route)r   r=   rJ   rK   r>   rL   r$   r$   r%   rO      s        zStarlette.add_route)r=   rJ   r>   r   c                 C   s   | j j|||d d S N)r>   r   add_websocket_route)r   r=   rJ   r>   r$   r$   r%   rR      s    zStarlette.add_websocket_route)rF   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.rH   r   c                    s     |  | S r2   )rG   rH   rF   r   r$   r%   	decorator   s    z.Starlette.exception_handler.<locals>.decoratorwarningswarnDeprecationWarningtypingCallable)r   rF   rV   r$   rU   r%   exception_handler   s    zStarlette.exception_handler)r=   rK   r>   rL   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.rS   c                    s   j j|  d | S rM   rN   rT   rL   rK   r>   r=   r   r$   r%   rV      s    z"Starlette.route.<locals>.decoratorrW   )r   r=   rK   r>   rL   rV   r$   r^   r%   rJ      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.rS   c                    s   j j|  d | S rP   rQ   rT   r>   r=   r   r$   r%   rV      s    z,Starlette.websocket_route.<locals>.decoratorrW   )r   r=   r>   rV   r$   r_   r%   websocket_route   s    
zStarlette.websocket_route)middleware_typer   c                    s8   t dt |dkst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.rS   c                    s    j t| d | S )N)dispatch)rE   r   rT   r3   r$   r%   rV     s    z'Starlette.middleware.<locals>.decorator)rX   rY   rZ   r   r[   r\   )r   ra   rV   r$   r3   r%   r      s    zStarlette.middleware)FNNNNNN)N)N)NNT)N)NNT)N)/__name__
__module____qualname____doc__boolr[   OptionalSequencer
   r   MappingAnyr\   r   r+   Unionr	   	Awaitabler   r&   r   r1   propertyListr   strr   r6   r   r   r   r:   r<   r@   rA   typerE   intTyperG   rI   rO   rR   r]   rJ   r`   r   r$   r$   r$   r%   r      s      	    	#       	         !  )r[   rX   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   