o
     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 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							dAd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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 	dBdededeje ddfd d!Z!	dBd"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+d,Z'ded-ejddfd.d/Z(			0dCded1ejd2ejeje  deje d3eddfd4d5Z)	dBded1ejdeje ddfd6d7Z*d)eje%ej&e f dejfd8d9Z+			0dCded2ejeje  deje d3edejf
d:d;Z,	dBdedeje dejfd<d=Z-d>edejfd?d@Z.dS )D	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   sv   |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 | _	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)selfr   r   r   r   r   r   r    r    A/usr/local/lib/python3.10/dist-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   error_handlerr   keyvaluer   r%   clsoptionsr    r    r!   build_middleware_stackO   s*   

z Starlette.build_middleware_stackc                 C   s   | j jS N)r   r   r   r    r    r!   r   k   s   zStarlette.routesnamepath_paramsc                 K   s   | j j|fi |S r/   )r   url_path_for)r   r1   r2   r    r    r!   r3   o   s   zStarlette.url_path_forscopereceivesendc                    s6   | |d< | j d u r|  | _ |  |||I d H  d S )Nr%   )r   r.   )r   r4   r5   r6   r    r    r!   __call__r   s
   

zStarlette.__call__
event_typec                 C   s   | j |S r/   )r   on_event)r   r8   r    r    r!   r9   x   s   zStarlette.on_eventpathr%   c                 C      | j j|||d d S N)r%   r1   )r   mount)r   r:   r%   r1   r    r    r!   r=   {      zStarlette.mounthostc                 C   r;   r<   )r   r?   )r   r?   r%   r1   r    r    r!   r?      r>   zStarlette.hostmiddleware_classr-   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   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   rD   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   r8   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   r1   rJ   r   	add_route)r   r:   rH   rI   r1   rJ   r    r    r!   rM      s   

zStarlette.add_routec                 C   r;   N)r1   r   add_websocket_route)r   r:   rH   r1   r    r    r!   rP      r>   zStarlette.add_websocket_routec                    s,   t dt dtjdtjf 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.rF   r   c                    s     |  | S r/   )rE   rF   rD   r   r    r!   	decorator   s   z.Starlette.exception_handler.<locals>.decoratorwarningswarnDeprecationWarningtypingCallable)r   rD   rS   r    rR   r!   exception_handler   s   zStarlette.exception_handlerc                    s2   t dt dtjdtjf 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.rF   r   c                    s   j j|  d | S rK   rL   rQ   rJ   rI   r1   r:   r   r    r!   rS      s   z"Starlette.route.<locals>.decoratorrT   )r   r:   rI   r1   rJ   rS   r    r[   r!   rH      s   "
zStarlette.routec                    s.   t dt 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)
        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.rF   r   c                    s   j j|  d | S rN   rO   rQ   r1   r:   r   r    r!   rS      s   z,Starlette.websocket_route.<locals>.decoratorrT   )r   r:   r1   rS   r    r\   r!   websocket_route   s   
zStarlette.websocket_routemiddleware_typec                    s:   t dt |dksJ ddtjdtjf 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.rF   r   c                    s    j t| d | S )N)dispatch)rC   r   rQ   r0   r    r!   rS      s   z'Starlette.middleware.<locals>.decoratorrT   )r   r^   rS   r    r0   r!   r      s   zStarlette.middleware)FNNNNNNr/   )NNT)/__name__
__module____qualname____doc__boolrX   OptionalSequencer
   r   MappingAnyrY   r   r'   Unionr	   	AwaitableAsyncContextManagerr"   r   r.   propertyListr   strr   r3   r   r   r   r7   r9   r=   r?   typerC   intTyperE   rG   rM   rP   rZ   rH   r]   r   r    r    r    r!   r      s
   	
%



	



!
r   )rX   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    