
    5pj                        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
  \R                  " S5      rS	\S
\4S jr " S S\\5      r\" SS9 " S S5      5       rS!S\R$                  S\S-  S
\S-  4S jjr\
SSS.S\S\S\S\S-  S\S-  S
\4S jj5       r " S S5      r\" 5       rS rS\\\4   S
\\\4   4S jrS  rg)"    N)	dataclass)Enum   )	constants   )validate_hf_hub_argsz^[0-9a-fA-F]{64}$xet_hashreturnc                 >    [        [        R                  U 5      5      $ )zLReturn whether `xet_hash` looks like a real Xet content hash (64 hex chars).)bool_REGEX_XET_HASH	fullmatch)r	   s    V/var/www/html/pdf-tiff/venv/lib/python3.13/site-packages/huggingface_hub/utils/_xet.pyis_valid_xet_hashr      s    ))(344    c                       \ 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   r   r   r   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!   responseendpointc                 "   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_responser7       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)revisionr(   
token_typerepo_id	repo_typer8   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   r2   value)r9   r:   r;   r8   r(   r*   s         r   xet_connection_info_refresh_urlr?   C   s_    4 $/xY5G5GHJeI;b	z7G7G6H
OCH 4
 	8*~Jr   c                   *    \ rS rSrSrS rS rS rSrg)XetSessionHolderh   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__p   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)rG   osgetpidrH   rI   hf_xetrO   )rK   current_pidrO   s      r   getXetSessionHolder.getu   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)rG   rH   sigint_abort	ExceptionrI   rJ   s    r   rW   XetSessionHolder.sigint_abort   s\    ZZ}}(MM..0 !%$(! Z ! 	 Zs-   AAA
AAAA
A+)rG   rH   rI   N)	r   r   r   r   __doc__rL   rT   rW   r   r   r   r   rA   rA   h   s    -
!0	)r   rA   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_HOLDERrT   r   r   r   get_xet_sessionr]      s     !!##r   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.
authorization)itemslower)r,   keyr>   s      r   xet_headers_without_authrc      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\   rW   r   r   r   abort_xet_sessionre      s     ##%r   rD   )rP   rerE   dataclassesr   enumr   httpx r   r   compiler   r%   r   r   r   r!   Responser7   r?   rA   r\   r]   dictrc   re   r   r   r   <module>rn      s=   	 	  !    " **125 5 5
3 
 $  
   #PT*  `knr`r  F   !! ! 	!
 Dj! Dj! 	! !H.) .)b &' $\d38n \c3h \&r   