
    RX9i                        S SK Jr  S SKJr  S SKJrJr  S SKrSSKJ	r	  SSKJ
r
JrJr   " S	 S
\\5      r\" SS9 " S S5      5       r\" SS9 " S S5      5       r S"S\R"                  S\\   S\\   4S jjrS\\\4   S\\   4S jr\S\S\\\4   S\4S j5       r\SSSS.S\S\S\S\\   S\\\4   S\\   S\\\\4      S\4S jj5       r\ S"S \S\\\4   S\\\\4      S\4S! jj5       rg)#    )	dataclass)Enum)DictOptionalN   )	constants   )get_sessionhf_raise_for_statusvalidate_hf_hub_argsc                       \ 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       Z/var/www/html/land-doc-ocr/venv/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)XetConnectionInfo   access_tokenexpiration_unix_epochendpointr   N)r   r   r   r   r"   r#   intr   r   r   r   r%   r%      s    Mr   r%   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)a5  
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 (`requests.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   B   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)   params
token_typerepo_id	repo_typerG   rH   c           	      x    Ub  UO[         R                  nU SU SU SU R                   SU 3	n[        XtU5      $ )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.
z/api/zs/z/xet-z-token/)r   r6   valuerE   )rI   rJ   rK   rG   r0   r)   rH   r.   s           r   (fetch_xet_connection_info_from_repo_inforN   w   sN    J $/xY5G5GHJeI;b	z7G7G6HPXz
ZC.sVDDr   r.   c                     [        5       R                  XUS9n[        U5        [        UR                  5      nUc  [        S5      e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.
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.
)r0   r.   rH   z6Xet headers have not been correctly set by the server.)r
   getr   rB   r0   r@   )r.   r0   rH   respmetadatas        r   rE   rE      sH    2 =WfED5dllCHQRROr   )N)dataclassesr   enumr   typingr   r   requests r   r
   r   r   r"   r   r   r%   Responser;   rB   rF   rN   rE   r   r   r   <module>rY      s   !  !   D D3 
 $  
 $   <@""+3C="k"JDcN xPaGb 2 QQ #s(^Q 	Q Q6  #"'+&E&E &E 	&E
 sm&E #s(^&E sm&E T#s(^$&E &E &ER  (,	#s(^ T#s(^$ 	 r   