o
    $i                     @   s  d Z ddlZddlZddlZddlZddlZddlZddlZddlZddlm	Z	 ddl
mZmZ ddlmZmZ ddlZddlm  mZ ddlm  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! dd
lm"Z"m#Z# zej$Z$W n e%y   ej&Z$Y nw e'e(Z)e  Z*e  Z+e	dg dZ,ej-ej.hZ/ej0ej1eej2d fddZ3dede4fddZ5defddZ6defddZ7dS )zT
Optional utils module contains utility methods
that require optional dependencies.
    N)
namedtuple)CallableUnion)RequestResponse)env_bool) RAY_INTERNAL_DASHBOARD_NAMESPACE)aiohttphdrs)method_route_table_factoryrest_response)DashboardAgentModuleDashboardHeadModuleAiohttpCacheValue)data
expirationtaskFc                    sF   dksJ t    fdd}tr!}tj||S |S )Nr   c                    s`   r.t  dtjjf fdd}d d d}| j|7  _| j|7  _|S  S )Nreturnc                     s   | d }|j ddkr|  I d H S |jtv r|j n
|j| I d H f  }|d urK  |j rA|j	t

 krKtjjdi |jS  fdd}t|  }|| |d u rh|I d H S tjjdi |jS )Nnocache1c                    s   z|   }W n ty   ttjjt d}Y nw |jt	|j
|jd}t|t  |  <   t krB jdd |S )N)status_codemessage)statusheadersbodyF)last)result	Exceptionr   dashboard_utilsHTTPStatusCodeINTERNAL_ERROR	traceback
format_excr   dictr   r   _AiohttpCacheValuetimemove_to_endlenpopitem)r   responser   )cachekeymaxsizettl_seconds Y/home/ubuntu/veenaModal/venv/lib/python3.10/site-packages/ray/dashboard/optional_utils.py_update_cacheW   s&   

zNaiohttp_cache.<locals>._wrapper.<locals>._cache_handler.<locals>._update_cacher/   )querygetmethod_AIOHTTP_CACHE_NOBODY_METHODSpath_qsreadr'   r   doner   r&   r	   webr   r   create_taskadd_done_callback)argsreqvaluer1   r   )r+   handlerr-   r.   )r,   r0   _cache_handler?   s$   




z7aiohttp_cache.<locals>._wrapper.<locals>._cache_handlerz[cache ttl=z, max_size=])	functoolswrapsr	   r9   r   __name____qualname__)r?   r@   suffixr+   enabler-   r.   )r?   r0   _wrapper<   s   3zaiohttp_cache.<locals>._wrapper)collectionsOrderedDictinspectiscoroutinefunctiondashboard_constsAIOHTTP_CACHE_TTL_SECONDS)r.   r-   rH   rI   target_funcr/   rG   r0   aiohttp_cache4   s   
>rQ   r=   r   c                    s*    j dddpt fdddD S )a_  Best-effort detection if the request was made by a browser.

    Uses three heuristics:
        1) If the `User-Agent` header starts with 'Mozilla'. This heuristic is weak,
        but hard for a browser to bypass e.g., fetch/xhr and friends cannot alter the
        user agent, but requests made with an HTTP library can stumble into this if
        they choose to user a browser-like user agent. At the time of writing, all
        common browsers' user agents start with 'Mozilla'.
        2) If any of the `Sec-Fetch-*` headers are present.
        3) If any of the various CORS headers are present
    z
User-Agent Mozillac                 3   s    | ]}| j v V  qd S )N)r   ).0hr=   r/   r0   	<genexpr>   s
    
z%is_browser_request.<locals>.<genexpr>)RefererOriginzSec-Fetch-ModezSec-Fetch-DestzSec-Fetch-SitezSec-Fetch-UserzAccess-Control-Request-MethodzAccess-Control-Request-Headers)r   r3   
startswithanyrV   r/   rV   r0   is_browser_request   s    r\   c                  C      dt dt fdd} | S )z8Reject any requests that appear to be made by a browser.fr   c                    s    t  dtf fdd}|S )Nr=   c                    s,   t |rtdtjjjdS  | |I d H S )NzBrowser requests not allowedtextr   )r\   r   r	   r9   HTTPMethodNotAllowedr   )selfr=   r^   r/   r0   	decorator   s   zCdeny_browser_requests.<locals>.decorator_factory.<locals>.decorator)rB   rC   r   r^   rd   r/   rc   r0   decorator_factory   s   	z0deny_browser_requests.<locals>.decorator_factoryr   rf   r/   r/   r0   deny_browser_requests   s   ri   c                  C   r]   )zDDecorator to be used on methods that require being connected to Ray.r^   r   c                    s(   t  dtttf f fdd}|S )Nrb   c              
      s   zFt  s9z| j}td|  ttjtj	d< t j
|ddtdd W n ty8 } zt   |d d }~ww  | g|R i |I d H W S  tyl } ztd|  tt tjjjdW  Y d }~S d }~ww )NzConnecting to ray with address=&RAY_gcs_server_request_timeout_secondsFT)addresslog_to_driverconfigure_logging	namespace_skip_env_hookzUnexpected error in handler: r_   )rayis_initializedgcs_addressloggerinfostrrN   GCS_RPC_TIMEOUT_SECONDSosenvironinitr   r   shutdown	exceptionr   r"   r#   r	   r9   HTTPInternalServerErrorr   )rb   r<   kwargsrk   erc   r/   r0   rd      s<   

zKinit_ray_and_catch_exceptions.<locals>.decorator_factory.<locals>.decorator)rB   rC   r   r   r   re   r/   rc   r0   rf      s
   
 z8init_ray_and_catch_exceptions.<locals>.decorator_factoryrg   rh   r/   r/   r0   init_ray_and_catch_exceptions   s   $r   )8__doc__asynciorJ   rB   rL   loggingrw   r&   r"   r   typingr   r   aiohttp.webr   r   rp   ray.dashboard.consts	dashboardconstsrN   ray.dashboard.utilsutilsr   ray._private.ray_constantsr   ray._rayletr   ray.dashboard.optional_depsr	   r
   ray.dashboard.routesr   r   r   r   r:   AttributeErrorensure_future	getLoggerrD   rs   DashboardHeadRouteTableDashboardAgentRouteTabler%   METH_GETMETH_DELETEr5   rO   AIOHTTP_CACHE_MAX_SIZE%AIOHTTP_CACHE_DISABLE_ENVIRONMENT_KEYrQ   boolr\   ri   r   r/   r/   r/   r0   <module>   sJ    



N 