B
    ind)                 @   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$   :/tmp/pip-unpacked-wheel-_bom8ime/starlette/applications.py__init__/   s    zStarlette.__init__)r   c       
      C   s   | j }d }i }x0| j D ]"\}}|dtfkr4|}q|||< qW tt||dg| j tt||dg }| j}x&t	|D ]\}}	|f d|i|	}qvW |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 )N)r   url_path_for)r   r2   r3   r$   r$   r%   r4   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   r5   r6   r7   r$   r$   r%   __call__v   s    

zStarlette.__call__)
event_typer   c             C   s   | j |S )N)r   on_event)r   r9   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 )N)r)   r<   )r   r>   )r   r>   r)   r<   r$   r$   r%   r>      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   r?   r0   r$   r$   r%   add_middleware   s    
zStarlette.add_middleware)exc_class_or_status_coder'   r   c             C   s   || j |< d S )N)r   )r   rC   r'   r$   r$   r%   add_exception_handler   s    zStarlette.add_exception_handler)r9   funcr   c             C   s   | j || d S )N)r   add_event_handler)r   r9   rE   r$   r$   r%   rF      s    zStarlette.add_event_handlerT)r;   routemethodsr<   include_in_schemar   c             C   s   | j j|||||d d S )N)rH   r<   rI   )r   	add_route)r   r;   rG   rH   r<   rI   r$   r$   r%   rJ      s    zStarlette.add_route)r;   rG   r<   r   c             C   s   | j j|||d d S )N)r<   )r   add_websocket_route)r   r;   rG   r<   r$   r$   r%   rK      s    zStarlette.add_websocket_route)rC   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.)rE   r   c                s     |  | S )N)rD   )rE   )rC   r   r$   r%   	decorator   s    z.Starlette.exception_handler.<locals>.decorator)warningswarnDeprecationWarningtypingCallable)r   rC   rL   r$   )rC   r   r%   exception_handler   s
    zStarlette.exception_handler)r;   rH   r<   rI   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.)rE   r   c                s   j j|  d | S )N)rH   r<   rI   )r   rJ   )rE   )rI   rH   r<   r;   r   r$   r%   rL      s    z"Starlette.route.<locals>.decorator)rM   rN   rO   rP   rQ   )r   r;   rH   r<   rI   rL   r$   )rI   rH   r<   r;   r   r%   rG      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.)rE   r   c                s   j j|  d | S )N)r<   )r   rK   )rE   )r<   r;   r   r$   r%   rL      s    z,Starlette.websocket_route.<locals>.decorator)rM   rN   rO   rP   rQ   )r   r;   r<   rL   r$   )r<   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.)rE   r   c                s    j t| d | S )N)dispatch)rB   r   )rE   )r   r$   r%   rL     s    z'Starlette.middleware.<locals>.decorator)rM   rN   rO   r   rP   rQ   )r   rT   rL   r$   )r   r%   r      s    
zStarlette.middleware)FNNNNNN)N)N)NNT)N)NNT)N)/__name__
__module____qualname____doc__boolrP   OptionalSequencer
   r   MappingAnyrQ   r   r+   Unionr	   	Awaitabler   r&   r   r1   propertyListr   strr   r4   r   r   r   r8   r:   r=   r>   typerB   intTyperD   rF   rJ   rK   rR   rG   rS   r   r$   r$   r$   r%   r      sP      	        )rP   rM   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   