o
    Þ­jN*  ã                   @  s  d dl mZ d dlZd dlZd dlZejdkrd dlmZ nd dlmZ d dlm	Z	m
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mZ d dlmZmZmZmZm Z m!Z! d dl"m#Z# ej$ddd�Z%edƒZ&G dd„ dƒZ'dS )é    )ÚannotationsN)é   é
   )Ú	ParamSpec)ÚStateÚURLPath)Ú
MiddlewareÚ_MiddlewareClass)ÚBaseHTTPMiddleware)ÚServerErrorMiddleware)ÚExceptionMiddleware)ÚRequest)ÚResponse)Ú	BaseRouteÚRouter)ÚASGIAppÚExceptionHandlerÚLifespanÚReceiveÚScopeÚSend)Ú	WebSocketÚAppTypeÚ	Starlette)ÚboundÚPc                   @  sè   e Zd ZdZ							d]d^dd„Zd_dd„Zed`dd„ƒZdad"d#„Zdbd*d+„Z	dcd.d/„Z
ddded3d4„Zdddfd6d7„Zdgd>d?„ZdhdDdE„ZdidGdH„Z			IdjdkdOdP„Z	dddldRdS„ZdmdTdU„Z			IdjdndVdW„ZdddodXdY„Zdpd[d\„ZdS )qr   aC  
    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 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 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 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.
    FNÚselfr   ÚdebugÚboolÚroutesú!typing.Sequence[BaseRoute] | NoneÚ
middlewareú"typing.Sequence[Middleware] | NoneÚexception_handlersú3typing.Mapping[typing.Any, ExceptionHandler] | NoneÚ
on_startupú7typing.Sequence[typing.Callable[[], typing.Any]] | NoneÚon_shutdownÚlifespanúLifespan[AppType] | NoneÚreturnÚNonec                 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)r   r   r   r!   r#   r%   r'   r(   © r2   úS/var/www/html/CropPilot/venv/lib/python3.10/site-packages/starlette/applications.pyÚ__init__9   s   ÿÿÿ
zStarlette.__init__r   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|i|
¤Ž}q8|S )Niô  )Úhandlerr   )Úhandlersr   Úapp)
r   r#   ÚitemsÚ	Exceptionr   r   r0   r   r-   Úreversed)r   r   Úerror_handlerr#   ÚkeyÚvaluer!   r7   ÚclsÚargsÚkwargsr2   r2   r3   Úbuild_middleware_stackT   s*   þ
ÿÿÿþÿ
z Starlette.build_middleware_stackúlist[BaseRoute]c                 C  s   | j jS ©N)r-   r   ©r   r2   r2   r3   r   p   s   zStarlette.routesÚnameÚstrÚpath_paramsú
typing.Anyr   c                K  s   | j j|fi |¤ŽS rC   )r-   Úurl_path_for)r   rE   rG   r2   r2   r3   rI   t   s   zStarlette.url_path_forÚscoper   Úreceiver   Úsendr   c                 Ã  s6   �| |d< | j d u r|  ¡ | _ |   |||¡I d H  d S )Nr7   )r1   rA   )r   rJ   rK   rL   r2   r2   r3   Ú__call__w   s
   €

zStarlette.__call__Ú
event_typeútyping.Callablec                 C  s   | j  |¡S rC   )r-   Úon_event)r   rN   r2   r2   r3   rP   }   s   zStarlette.on_eventÚpathr7   ú
str | Nonec                 C  ó   | j j|||d� d S ©N)r7   rE   )r-   Úmount)r   rQ   r7   rE   r2   r2   r3   rU   €   ó   zStarlette.mountÚhostc                 C  rS   rT   )r-   rW   )r   rW   r7   rE   r2   r2   r3   rW   ƒ   rV   zStarlette.hostÚmiddleware_classútype[_MiddlewareClass[P]]r?   úP.argsr@   úP.kwargsc                 O  s6   | j d ur	tdƒ‚| j dt|g|¢R i |¤Ž¡ d S )Nz6Cannot add middleware after an application has startedr   )r1   ÚRuntimeErrorr0   Úinsertr   )r   rX   r?   r@   r2   r2   r3   Úadd_middleware†   s   
$zStarlette.add_middlewareÚexc_class_or_status_codeúint | type[Exception]r5   r   c                 C  s   || j |< d S rC   )r#   )r   r_   r5   r2   r2   r3   Úadd_exception_handler�   s   zStarlette.add_exception_handlerÚfuncc                 C  s   | j  ||¡ d S rC   )r-   Úadd_event_handler)r   rN   rb   r2   r2   r3   rc   —   s   zStarlette.add_event_handlerTÚrouteúAtyping.Callable[[Request], typing.Awaitable[Response] | Response]Úmethodsúlist[str] | NoneÚinclude_in_schemac                 C  s   | j j|||||d� d S ©N)rf   rE   rh   ©r-   Ú	add_route)r   rQ   rd   rf   rE   rh   r2   r2   r3   rk   ž   s   

ÿzStarlette.add_routeú4typing.Callable[[WebSocket], typing.Awaitable[None]]c                 C  rS   ©N)rE   ©r-   Úadd_websocket_route)r   rQ   rd   rE   r2   r2   r3   ro   ª   s   zStarlette.add_websocket_routec                   s    t  dt¡ d‡ ‡fdd„}|S )Nz¢The `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.rb   rO   r*   c                   s   ˆ  ˆ | ¡ | S rC   )ra   ©rb   ©r_   r   r2   r3   Ú	decorator»   s   z.Starlette.exception_handler.<locals>.decorator©rb   rO   r*   rO   ©ÚwarningsÚwarnÚDeprecationWarning)r   r_   rr   r2   rq   r3   Úexception_handler²   s   ýzStarlette.exception_handlerc                   s&   t  dt¡ 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)
        z“The `route` decorator is deprecated, and will be removed in version 1.0.0. Refer to https://www.starlette.io/routing/ for the recommended approach.rb   rO   r*   c                   s   ˆj jˆ| ˆˆˆ d� | S ri   rj   rp   ©rh   rf   rE   rQ   r   r2   r3   rr   Õ   s   ûz"Starlette.route.<locals>.decoratorNrs   rt   )r   rQ   rf   rE   rh   rr   r2   ry   r3   rd   Á   s   ý
zStarlette.routec                   s"   t  dt¡ 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)
        z¯The `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.rb   rO   r*   c                   s   ˆj jˆ| ˆ d� | S rm   rn   rp   ©rE   rQ   r   r2   r3   rr   ï   s   z,Starlette.websocket_route.<locals>.decoratorNrs   rt   )r   rQ   rE   rr   r2   rz   r3   Úwebsocket_routeá   s   ýzStarlette.websocket_routeÚmiddleware_typec                   s.   t  dt¡ |dksJ dƒ‚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)
        z¨The `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.rb   rO   r*   c                   s   ˆ j t| d� | S )N)Údispatch)r^   r
   rp   rD   r2   r3   rr     s   z'Starlette.middleware.<locals>.decoratorNrs   rt   )r   r|   rr   r2   rD   r3   r!   õ   s   ý
ÿzStarlette.middleware)FNNNNNN)r   r   r   r   r   r    r!   r"   r#   r$   r%   r&   r'   r&   r(   r)   r*   r+   )r*   r   )r*   rB   )rE   rF   rG   rH   r*   r   )rJ   r   rK   r   rL   r   r*   r+   )rN   rF   r*   rO   rC   )rQ   rF   r7   r   rE   rR   r*   r+   )rW   rF   r7   r   rE   rR   r*   r+   )rX   rY   r?   rZ   r@   r[   r*   r+   )r_   r`   r5   r   r*   r+   )rN   rF   rb   rO   r*   r+   )NNT)rQ   rF   rd   re   rf   rg   rE   rR   rh   r   r*   r+   )rQ   rF   rd   rl   rE   rR   r*   r+   )r_   r`   r*   rO   )
rQ   rF   rf   rg   rE   rR   rh   r   r*   rO   )rQ   rF   rE   rR   r*   rO   )r|   rF   r*   rO   )Ú__name__Ú
__module__Ú__qualname__Ú__doc__r4   rA   Úpropertyr   rI   rM   rP   rU   rW   r^   ra   rc   rk   ro   rx   rd   r{   r!   r2   r2   r2   r3   r      sD    ø







úü
û )(Ú
__future__r   ÚsysÚtypingru   Úversion_infor   Útyping_extensionsÚstarlette.datastructuresr   r   Ústarlette.middlewarer   r	   Ústarlette.middleware.baser
   Ústarlette.middleware.errorsr   Ústarlette.middleware.exceptionsr   Ústarlette.requestsr   Ústarlette.responsesr   Ústarlette.routingr   r   Ústarlette.typesr   r   r   r   r   r   Ústarlette.websocketsr   ÚTypeVarr   r   r   r2   r2   r2   r3   Ú<module>   s(    
 