o
    1 i0                     @   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DashboardHeadModuleZ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 )NZ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_utilsZHTTPStatusCodeINTERNAL_ERROR	traceback
format_excr   dictr   r   _AiohttpCacheValuetimemove_to_endlenpopitem)r   responser   )cachekeymaxsizettl_seconds h/home/app/PaddleOCR-VL-test/.venv_paddleocr/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_METHODSZpath_qsreadr$   r   doner   r#   r	   webr   r   create_taskadd_done_callback)argsreqvaluer.   r   )r(   handlerr*   r+   )r)   r-   _cache_handler?   s$   




z7aiohttp_cache.<locals>._wrapper.<locals>._cache_handlerz[cache ttl=z, max_size=])	functoolswrapsr	   r5   r   __name____qualname__)r;   r<   suffixr(   enabler*   r+   )r;   r-   _wrapper<   s   3zaiohttp_cache.<locals>._wrapper)collectionsOrderedDictinspectiscoroutinefunctiondashboard_constsAIOHTTP_CACHE_TTL_SECONDS)r+   r*   rD   rE   Ztarget_funcr,   rC   r-   aiohttp_cache4   s   
>rL   r9   r   c                 C   s   | j d dS )a4  Checks if a request is made by a browser like user agent.

    This heuristic is very weak, but hard for a browser to bypass- eg,
    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.
    z
User-AgentZMozilla)r   
startswith)r9   r,   r,   r-   is_browser_request   s   rN   c                  C      dt dt fdd} | S )z7Reject any requests that appear to be made by a browserfr   c                    s    t  dtf fdd}|S )Nr9   c                    s,   t |rtdtjjjdS  | |I d H S )NzBrowser requests not allowedtextr   )rN   r   r	   r5   ZHTTPMethodNotAllowedr   )selfr9   rP   r,   r-   	decorator   s   zCdeny_browser_requests.<locals>.decorator_factory.<locals>.decorator)r>   r?   r   rP   rU   r,   rT   r-   decorator_factory   s   z0deny_browser_requests.<locals>.decorator_factoryr   rW   r,   r,   r-   deny_browser_requests   s   rZ   c                  C   rO   )zDDecorator to be used on methods that require being connected to Ray.rP   r   c                    s(   t  dtttf f fdd}|S )NrS   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=Z&RAY_gcs_server_request_timeout_secondsFT)addressZlog_to_driverZconfigure_logging	namespaceZ_skip_env_hookzUnexpected error in handler: rQ   )rayZis_initializedZgcs_addressloggerinfostrrJ   ZGCS_RPC_TIMEOUT_SECONDSosenvironinitr   r   shutdown	exceptionr   r   r    r	   r5   ZHTTPInternalServerErrorr   )rS   r8   kwargsr[   erT   r,   r-   rU      s<   

zKinit_ray_and_catch_exceptions.<locals>.decorator_factory.<locals>.decorator)r>   r?   r   r   r   rV   r,   rT   r-   rW      s
   
 z8init_ray_and_catch_exceptions.<locals>.decorator_factoryrX   rY   r,   r,   r-   init_ray_and_catch_exceptions   s   $rh   )8__doc__asynciorF   r>   rH   loggingra   r#   r   r   typingr   r   Zaiohttp.webr   r   r]   Zray.dashboard.constsZ	dashboardZconstsrJ   Zray.dashboard.utilsutilsr   Zray._private.ray_constantsr   Zray._rayletr   Zray.dashboard.optional_depsr	   r
   Zray.dashboard.routesr   r   r   r   r6   AttributeErrorensure_future	getLoggerr@   r^   ZDashboardHeadRouteTableZDashboardAgentRouteTabler"   ZMETH_GETZMETH_DELETEr2   rK   ZAIOHTTP_CACHE_MAX_SIZEZ%AIOHTTP_CACHE_DISABLE_ENVIRONMENT_KEYrL   boolrN   rZ   rh   r,   r,   r,   r-   <module>   sJ    



N