
    ^j                     V   d dl Z d dlZd dlmZ d dlmZ d dlZddlmZ ddlm	Z	  G d d	e
e      Z ed
       G d d             Zddej                  de
dz  dedz  fdZe	ddddede
de
de
dz  de
dz  de
fd       Z G d d      Z e       Zd Zdee
e
f   dee
e
f   fdZd Zy)    N)	dataclass)Enum   )	constants   )validate_hf_hub_argsc                       e Zd ZdZdZy)XetTokenTypereadwriteN)__name__
__module____qualname__READWRITE     e/var/www/ramen.bs-engineer-server.com/venv/lib/python3.12/site-packages/huggingface_hub/utils/_xet.pyr
   r
      s    DEr   r
   T)frozenc                   "    e Zd ZU eed<   eed<   y)XetFileData	file_hashrefresh_routeN)r   r   r   str__annotations__r   r   r   r   r      s    Nr   r   responseendpointreturnc                    | y	 | j                   t        j                     }t        j                  | j                  v r!| j                  t        j                     d   }n| j                   t        j
                     }||nt        j                  }|j                  t        j                        r>|j                  t        j                  j                  d      |j                  d            }t        ||      S # t        $ r Y yw xY w)a^  
    Parse XET file metadata from an HTTP response.

    This function extracts XET file metadata from the HTTP headers or HTTP links
    of a given response object. If the required metadata is not found, it returns `None`.

    Args:
        response (`httpx.Response`):
            The HTTP response object containing headers dict and links dict to extract the XET metadata from.
    Returns:
        `Optional[XetFileData]`:
            An instance of `XetFileData` containing the file hash and refresh route if the metadata
            is found. Returns `None` if the required metadata is missing.
    Nurl/)r   r   )headersr   HUGGINGFACE_HEADER_X_XET_HASH$HUGGINGFACE_HEADER_LINK_XET_AUTH_KEYlinks&HUGGINGFACE_HEADER_X_XET_REFRESH_ROUTEKeyErrorENDPOINT
startswithHUGGINGFACE_CO_URL_HOMEreplacerstripr   )r   r   r   r   s       r   !parse_xet_file_data_from_responser-      s     $$Y%L%LM	99X^^K$NN9+Y+YZ[`aM$,,Y-]-]^M $/xY5G5GH	 A AB%--i.O.O.V.VWZ.[]e]l]lmp]qr#   s   A7C: :	DD)revisionr   
token_typerepo_id	repo_typer.   c                 |    ||nt         j                  }| d| d| d| j                   d}|dk7  s||d| z  }|S )a  
    Build the URL used to fetch or refresh a Xet access token for a given repo.
    Args:
        token_type (`XetTokenType`):
            Type of the token to request: `"read"` or `"write"`.
        repo_id (`str`):
            A namespace (user or an organization) and a repo name separated by a `/`.
        repo_type (`str`):
            Type of the repo (e.g. `"model"`, `"dataset"`, `"space"`, `"bucket"`).
        revision (`str`, `optional`):
            The revision of the repo to get the token for.
        endpoint (`str`, `optional`):
            The endpoint to use for the request. Defaults to the Hub endpoint.
    Returns:
        `str`:
            The fully-qualified URL of the token refresh endpoint.
    z/api/zs/z/xet-z-tokenbucketr!   )r   r(   value)r/   r0   r1   r.   r   r    s         r   xet_connection_info_refresh_urlr5   :   s_    4 $/xY5G5GHJeI;b	z7G7G6H
OCH 4
 	8*~Jr   c                   "    e Zd ZdZd Zd Zd Zy)XetSessionHoldera7  Holds an optional XetSession; supports safe re-creation after sigint_abort or fork.

    Thread-safe: a ``threading.Lock`` guards all state mutations, which matters
    for free-threaded Python (3.14t) where multiple threads can race on ``get()``
    or ``sigint_abort()`` without the GIL serialising them.
    c                 R    t        j                         | _        d | _        d | _        y N)	threadingLock_lock_session_session_pidselfs    r   __init__zXetSessionHolder.__init__g   s    ^^%
(,r   c                    | j                   5  t        j                         }| j                  | j                  |k7  rd| _        | j                  ddlm}  |       | _        || _        | j                  cddd       S # 1 sw Y   yxY w)a  Return the current session, creating one if needed.

        Fork-safe: if the current process PID differs from the PID that created
        the session (i.e. we are in a forked child), the old session is discarded
        and a fresh session is created for this process.
        Nr   )
XetSession)r<   osgetpidr=   r>   hf_xetrC   )r@   current_pidrC   s      r   getzXetSessionHolder.getl   sp     ZZ 	!))+K}}(T->->+-M !%}}$- *$/!==	! 	! 	!s   A'A>>Bc                     | j                   5  | j                  )	 | j                  j                          d| _        d| _        ddd       y# t        $ r Y "w xY w# 1 sw Y   yxY w)zMAbort the current session and clear it so the next get() creates a fresh one.N)r<   r=   sigint_abort	Exceptionr>   r?   s    r   rJ   zXetSessionHolder.sigint_abort   sg    ZZ 	)}}(MM..0 !%$(!	) 	) ! 		) 	)s-   AAA	AAAAA$N)r   r   r   __doc__rA   rH   rJ   r   r   r   r7   r7   _   s    -
!0	)r   r7   c                  *    t         j                         S )a*  Return the global :class:`hf_xet.XetSession`, creating it on first call.

    The session is shared across all calls within a process, just as the HTTP
    client returned by :func:`~huggingface_hub.utils._http.get_session` is shared.
    It is created lazily and is fork-safe and thread-safe.
    )_GLOBAL_XET_HOLDERrH   r   r   r   get_xet_sessionrO      s     !!##r   r"   c                 ~    | j                         D ci c]  \  }}|j                         dk7  s|| c}}S c c}}w )zReturn a copy of headers with the authorization header removed.

    Xet storage requests use a short-lived xet access token for auth, so the
    Hub authorization header must not be forwarded to xet storage endpoints.
    authorization)itemslower)r"   keyr4   s      r   xet_headers_without_authrU      s3     *1[:3CIIK?<ZCJ[[[s   99c                  ,    t         j                          y)zAbort the global xet session after a KeyboardInterrupt.

    Cancels any in-flight Rust operation and clears the session so the next
    call to :func:`get_xet_session` starts fresh (notebook-friendly).
    N)rN   rJ   r   r   r   abort_xet_sessionrW      s     ##%r   r9   )rD   r:   dataclassesr   enumr   httpx r   r   r   r
   r   Responser-   r5   r7   rN   rO   dictrU   rW   r   r   r   <module>r^      s   	  !    "3 
 $  
   #PT*  `knr`r  F   !! ! 	!
 Dj! Dj! 	! !H.) .)b &' $\d38n \c3h \&r   