
    3jv6                     $   % S SK r S SKrS SKrS SKJr  S SKJr  S SKrSSKJ	r	  SSKJ
r
JrJr  SrS	r0 r\\S
4   \S'    " S S\\5      r\" SS9 " S S5      5       r\" SS9 " S S
5      5       rS1S\R,                  S\S-  S\S-  4S jjrS\\\4   S\S-  4S jr\S\S\\\4   S\4S j5       r\SSS.S\S\S\S\S-  S\S-  S\4S  jj5       r\SSSS!.S\S\S\S\S-  S\\\4   S\S-  S"\\\4   S-  S\4S# jj5       r\  S2S$\S\\\4   S"\\\4   S-  S%\S-  S\4
S& jj5       rS\S-  S\SS4S' jrS1S$\S\\\4   S"\\\4   S-  S(\S-  S\4
S) jjrS*\S\4S+ jr  " S, S-5      r!\!" 5       r"S. r#S\\\4   S\\\4   4S/ jr$S0 r%g)3    N)	dataclass)Enum   )	constants   )hf_raise_for_statushttp_backoffvalidate_hf_hub_args<   i  XetConnectionInfoXET_CONNECTION_INFO_CACHEc                       \ rS rSrSrSrSrg)XetTokenType   readwrite N)__name__
__module____qualname____firstlineno__READWRITE__static_attributes__r       T/home/wildlama/miniconda3/lib/python3.13/site-packages/huggingface_hub/utils/_xet.pyr   r      s    DEr   r   T)frozenc                   *    \ rS rSr% \\S'   \\S'   Srg)XetFileData   	file_hashrefresh_router   N)r   r   r   r   str__annotations__r   r   r   r   r   r      s    Nr   r   c                   4    \ rS rSr% \\S'   \\S'   \\S'   Srg)r      access_tokenexpiration_unix_epochendpointr   N)r   r   r   r   r#   r$   intr   r   r   r   r   r      s    Mr   responser)   returnc                 "   U c  g U R                   [        R                     n[        R                  U R                  ;   a!  U R                  [        R                     S   nOU R                   [        R
                     n Ub  UO[        R                  nUR                  [        R                  5      (       a>  UR                  [        R                  R                  S5      UR                  S5      5      n[        UUS9$ ! [         a     gf = f)a2  
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B%--i.O.O.V.VWZ.[]e]l]lmp]qr#   s   AD "D 
DDr0   c                      U [         R                     nU [         R                     n[        U [         R                     5      n[        UUUS9$ ! [
        [        [        4 a     gf = f)a`  
Parse XET connection info from the HTTP headers or return None if not found.
Args:
    headers (`dict`):
       HTTP headers to extract the XET metadata from.
Returns:
    `XetConnectionInfo` or `None`:
        The information needed to connect to the XET storage service.
        Returns `None` if the headers do not contain the XET connection info.
N)r)   r'   r(   )	r   !HUGGINGFACE_HEADER_X_XET_ENDPOINT%HUGGINGFACE_HEADER_X_XET_ACCESS_TOKENr*   #HUGGINGFACE_HEADER_X_XET_EXPIRATIONr5   
ValueError	TypeErrorr   )r0   r)   r'   r(   s       r   &parse_xet_connection_info_from_headersrB   G   sp    9FFGyNNO #GI,Y,Y$Z [ !3  j), s   AA A'&A'	file_datac                 ^    U R                   c  [        S5      e[        U R                   U5      $ )a  
Utilizes the information in the parsed metadata to request the Hub xet connection information.
This includes the access token, expiration, and XET service URL.
Args:
    file_data: (`XetFileData`):
        The file data needed to refresh the xet connection information.
    headers (`dict[str, str]`):
        Headers to use for the request, including authorization headers and user agent.
Returns:
    `XetConnectionInfo`:
        The connection information needed to make the request to the xet storage service.
Raises:
    [`~utils.HfHubHTTPError`]
        If the Hub API returned an error.
    [`ValueError`](https://docs.python.org/3/library/exceptions.html#ValueError)
        If the Hub API response is improperly formatted.
z>The provided xet metadata does not contain a refresh endpoint.)r"   r@   #_fetch_xet_connection_info_with_url)rC   r0   s     r   refresh_xet_connection_inforF   `   s/    . &YZZ.y/F/FPPr   )revisionr)   
token_typerepo_id	repo_typerG   c                     Ub  UO[         R                  nU SU SU SU R                   S3nUS:w  d  Ub  USU 3-  nU$ )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   r6   value)rH   rI   rJ   rG   r)   r.   s         r   xet_connection_info_refresh_urlrN   |   s_    4 $/xY5G5GHJeI;b	z7G7G6H
OCH 4
 	8*~Jr   )rG   r)   paramsrO   c                 :    [        U UUUUS9n[        XtXb SU 3S9$ )aa  
Uses the repo info to request a xet access token from Hub.
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 to upload to: `"model"`, `"dataset"` or `"space"`.
    revision (`str`, `optional`):
        The revision of the repo to get the token for.
    headers (`dict[str, str]`):
        Headers to use for the request, including authorization headers and user agent.
    endpoint (`str`, `optional`):
        The endpoint to use for the request. Defaults to the Hub endpoint.
    params (`dict[str, str]`, `optional`):
        Additional parameters to pass with the request.
Returns:
    `XetConnectionInfo`:
        The connection information needed to make the request to the xet storage service.
Raises:
    [`~utils.HfHubHTTPError`]
        If the Hub API returned an error.
    [`ValueError`](https://docs.python.org/3/library/exceptions.html#ValueError)
        If the Hub API response is improperly formatted.
)rH   rI   rJ   rG   r)   -)cache_key_prefix)rN   rE   )rH   rI   rJ   rG   r0   r)   rO   r.   s           r   (fetch_xet_connection_info_from_repo_inforS      s<    J *C /sVXccdeldmVnoor   r.   rR   c                    [        XX#S9n[        R                  U5      nUb  [        U5      (       d  U$ [	        SXUS9n[        U5        [        UR                  5      nUc  [        S5      e[        [        R                  5       5       H-  u  p[        U	5      (       d  M  [        R                  US5        M/     [        [        5      [        :  a+  [        R                  [        [        [        5      5      5        U[        U'   U$ )a;  
Requests the xet connection info from the supplied URL. This includes the
access token, expiration time, and endpoint to use for the xet storage service.

Result is cached to avoid redundant requests.

Args:
    url: (`str`):
        The access token endpoint URL.
    headers (`dict[str, str]`):
        Headers to use for the request, including authorization headers and user agent.
    params (`dict[str, str]`, `optional`):
        Additional parameters to pass with the request.
Returns:
    `XetConnectionInfo`:
        The connection information needed to make the request to the xet storage service.
Raises:
    [`~utils.HfHubHTTPError`]
        If the Hub API returned an error.
    [`ValueError`](https://docs.python.org/3/library/exceptions.html#ValueError)
        If the Hub API response is improperly formatted.
)prefixNGET)r0   rO   z6Xet headers have not been correctly set by the server.)
_cache_keyr   get_is_expiredr	   r   rB   r0   r@   listitemspoplenXET_CONNECTION_INFO_CACHE_SIZEnextiter)
r.   r0   rO   rR   	cache_keycached_inforespmetadatakvs
             r   rE   rE      s    < 3II+//	:K;'' sFCD5dllCHQRR .4467q>>%))!T2 8
 $%)GG!%%d40I+J&KL ,4i(Or   c                     U c  [         R                  n U  SU S3n[        [        R	                  5       5       H1  nUR                  U5      (       d  M  [        R                  US5        M3     g)zgReset the XET connection info cache for the given repo type and repo id.

Used when a repo is deleted.
NrQ   |)r   REPO_TYPE_MODELrZ   r   keysr7   r\   )rJ   rI   rU   re   s       r   (reset_xet_connection_info_cache_for_repork     s_    
 --	{!G9A&F+0023<<%))!T2 4r   rU   c                 "   UR                  5        VVs0 s H  u  pEUR                  5       U_M     nnnUR                  SS5      nSR                  S [	        U=(       d    0 R                  5       S S9 5       5      nU SU  SU SU 3$ s  snnf )z;Return a unique cache key for the given request parameters.authorization &c              3   4   #    U  H  u  pU S U 3v   M     g7f)=Nr   ).0re   rf   s      r   	<genexpr>_cache_key.<locals>.<genexpr>  s     d1cQCq*1cs   c                     U S   $ )Nr   r   )xs    r   <lambda>_cache_key.<locals>.<lambda>  s    ^_`a^br   )keyrh   )r[   lowerrX   joinsorted)	r.   r0   rO   rU   re   rf   lower_headersauth_header
params_strs	            r   rW   rW     s    .5mmo>odaQWWY\oM>##OR8Kd28L8L8NTb1cddJXQse1[M:,77 ?s   Bconnection_infoc                 f    U R                   [        [        R                  " 5       5      [        -   :*  $ )z2Check if the given XET connection info is expired.)r(   r*   time!XET_CONNECTION_INFO_SAFETY_PERIOD)r   s    r   rY   rY      s$    00C		4DGh4hhhr   c                   *    \ rS rSrSrS rS rS rSrg)XetSessionHolderi%  a'  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                 T    [         R                  " 5       U l        S U l        S U l        g N)	threadingLock_lock_session_session_pidselfs    r   __init__XetSessionHolder.__init__-  s    ^^%
(,r   c                 $   U R                      [        R                  " 5       nU R                  b  U R                  U:w  a  SU l        U R                  c  SSKJn  U" 5       U l        Xl        U R                  sSSS5        $ ! , (       d  f       g= f)zReturn 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_xetr   )r   current_pidr   s      r   rX   XetSessionHolder.get2  sc     ZZ))+K}}(T->->+-M !%}}$- *$/!== ZZs   A*B
Bc                     U R                      U R                  b)   U R                  R                  5         SU l        SU l        SSS5        g! [         a     N#f = f! , (       d  f       g= f)zMAbort the current session and clear it so the next get() creates a fresh one.N)r   r   sigint_abort	Exceptionr   r   s    r   r   XetSessionHolder.sigint_abortJ  s\    ZZ}}(MM..0 !%$(! Z ! 	 Zs-   AAA
AAAA
A+)r   r   r   N)	r   r   r   r   __doc__r   rX   r   r   r   r   r   r   r   %  s    -
!0	)r   r   c                  *    [         R                  5       $ )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_HOLDERrX   r   r   r   get_xet_sessionr   Y  s     !!##r   c                     U R                  5        VVs0 s H  u  pUR                  5       S:w  d  M  X_M     snn$ s  snnf )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.
rm   )r[   rz   )r0   ry   rM   s      r   xet_headers_without_authr   c  s5     *1[:3CIIK?<ZJCJ[[[s   <<c                  ,    [         R                  5         g)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)r   r   r   r   r   abort_xet_sessionr   l  s     ##%r   r   )NN)&r   r   r   dataclassesr   enumr   httpxrn   r   r   r	   r
   r   r^   r   dictr#   r$   r   r   r   Responser;   rB   rF   rN   rS   rE   rk   rW   boolrY   r   r   r   r   r   r   r   r   <module>r      s1   	   !    E E %' !!& <> 4%8 89 >3 
 $  
 $     #PT*  `knr`r  FDcN GX[_G_ 2 QQ #s(^Q 	Q Q6   !! ! 	!
 Dj! Dj! 	! !H   $(+p+p +p 	+p
 Dj+p #s(^+p Dj+p cNT!+p +p +p\  %)#'	7	7#s(^7 cNT!7 Dj	7
 7 7t
3d
 
3S 
3UY 
38C 8$sCx. 8$sCx.4:O 8Y\_cYc 8or 8i!2 it i
.) .)b &' $\d38n \c3h \&r   