o
    [h                     @   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mZ d dlmZmZmZmZ G d	d
 d
ZdS )    N)StateURLPath)ExceptionMiddleware)
Middleware)BaseHTTPMiddleware)ServerErrorMiddleware)	BaseRouteRouter)ASGIAppReceiveScopeSendc                   @   s  e Zd ZdZ							dEdedeje deje dej	ej
eeje f ejf dejej d	ejej d
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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ddfd$d%Z dFd&ed#ed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 ded7eddfd8d9Z'	dFd"ed5ejd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 ded7edejf
d>d?Z*dFd"ededejfd@dAZ+dBedejfdCdDZ,dS )H	Starlettean  
    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 occuring anywhere in the entire stack.
    `ExceptionMiddleware` is added as the very innermost middleware, to deal
    with handled exception cases occuring in the routing or endpoints.
    * **exception_handlers** - A dictionary mapping 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    }/home/ubuntu/experiments/live_experiments/Pythonexperiments/Otree/venv/lib/python3.10/site-packages/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   Zerror_handlerr   keyvaluer   r%   clsoptionsr    r    r!   r   D   s(   

z Starlette.build_middleware_stackc                 C   s   | j jS N)r   r   r   r    r    r!   r   ^   s   zStarlette.routesc                 C   s   | j S r-   )r   r.   r    r    r!   r   b   s   zStarlette.debugr*   c                 C   s   || _ |  | _d S r-   )r   r   r   )r   r*   r    r    r!   r   f   s   namepath_paramsc                 K   s   | j j|fi |S r-   )r   url_path_for)r   r/   r0   r    r    r!   r1   k   s   zStarlette.url_path_forscopereceivesendc                    s"   | |d< |  |||I d H  d S )Nr%   )r   )r   r2   r3   r4   r    r    r!   __call__n   s   zStarlette.__call__
event_typec                 C   s   | j |S r-   )r   on_event)r   r6   r    r    r!   r7   t   s   zStarlette.on_eventpathr%   c                 C      | j j|||d d S N)r%   r/   )r   mount)r   r8   r%   r/   r    r    r!   r;   w      zStarlette.mounthostc                 C   r9   r:   )r   r=   )r   r=   r%   r/   r    r    r!   r=   z   r<   zStarlette.hostmiddleware_classr,   c                 K   s(   | j dt|fi | |  | _d S )Nr   )r   insertr   r   r   )r   r>   r,   r    r    r!   add_middleware}   s   zStarlette.add_middlewareexc_class_or_status_coder#   c                 C   s   || j |< |  | _d S r-   )r   r   r   )r   rA   r#   r    r    r!   add_exception_handler   s   
zStarlette.add_exception_handlerfuncc                 C   s   | j || d S r-   )r   add_event_handler)r   r6   rC   r    r    r!   rD      s   zStarlette.add_event_handlerTroutemethodsinclude_in_schemac                 C   s   | j j|||||d d S N)rF   r/   rG   r   	add_route)r   r8   rE   rF   r/   rG   r    r    r!   rJ      s   

zStarlette.add_routec                 C   r9   N)r/   r   add_websocket_route)r   r8   rE   r/   r    r    r!   rM      s   zStarlette.add_websocket_routec                    s    dt jdt jf fdd}|S )NrC   r   c                    s     |  | S r-   )rB   rC   rA   r   r    r!   	decorator   s   z.Starlette.exception_handler.<locals>.decoratortypingCallable)r   rA   rP   r    rO   r!   exception_handler   s   zStarlette.exception_handlerc                    s&   dt jdt jf fdd}|S )NrC   r   c                    s   j j|  d | S rH   rI   rN   rG   rF   r/   r8   r   r    r!   rP      s   z"Starlette.route.<locals>.decoratorrQ   )r   r8   rF   r/   rG   rP   r    rU   r!   rE      s   "
zStarlette.routec                    s"   dt jdt jf fdd}|S )NrC   r   c                    s   j j|  d | S rK   rL   rN   r/   r8   r   r    r!   rP      s   z,Starlette.websocket_route.<locals>.decoratorrQ   )r   r8   r/   rP   r    rV   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 )Nhttpz/Currently only middleware("http") is supported.rC   r   c                    s    j t| d | S )N)dispatch)r@   r   rN   r.   r    r!   rP      s   z'Starlette.middleware.<locals>.decoratorrQ   )r   rX   rP   r    r.   r!   r      s
   
zStarlette.middleware)FNNNNNNr-   )NNT)-__name__
__module____qualname____doc__boolrR   Sequencer   r   DictUnionintTyper'   rS   AsyncGeneratorr"   r
   r   propertyListr   r   setterstrr   r1   r   r   r   r5   r7   r;   r=   typeAnyr@   rB   rD   rJ   rM   rT   rE   rW   r   r    r    r    r!   r      s    

	






r   )rR   Zstarlette.datastructuresr   r   Zstarlette.exceptionsr   Zstarlette.middlewarer   Zstarlette.middleware.baser   Zstarlette.middleware.errorsr   Zstarlette.routingr   r	   Zstarlette.typesr
   r   r   r   r   r    r    r    r!   <module>   s    