ó
    pyüij&  ã                   ó~  • % 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S*S\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\\\4   S\S-  S \\\4   S-  S\4S! jj5       r\
  S+S"\S\\\4   S \\\4   S-  S#\S-  S\4
S$ jj5       rS\S-  S\SS4S% jrS*S"\S\\\4   S \\\4   S-  S&\S-  S\4
S' jjrS(\S\4S) jrg),é    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   ó    ÚW/home/mande/repo/quber/.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)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Ñ9¸X¿^¹^ÓKØ$ŸN™N¬9×+YÑ+YÑZÐ[`Ña‰Mà$×,Ñ,¬Y×-]Ñ-]Ñ^‰Mð $Ñ/‰x´Y×5GÑ5G€HØ×Ñ¤	× AÑ A×BÑBØ%×-Ñ-¬i×.OÑ.O×.VÑ.VÐWZÓ.[Ð]e×]lÑ]lÐmpÓ]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   E   sp   € ðØœ9×FÑFÑGˆØœy×NÑNÑOˆÜ # G¬I×,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                 óœ   • 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[        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.
z/api/zs/z/xet-z-tokenÚbucketr/   Ú-)Úcache_key_prefix)r   r6   ÚvaluerE   )rI   rJ   rK   rG   r0   r)   rH   r.   s           r   Ú(fetch_xet_connection_info_from_repo_inforQ   z   sw   € ðJ $Ñ/‰x´Y×5GÑ5G€HØˆJ�e˜I˜; b¨¨	°°z×7GÑ7GÐ6HÈÐ
O€CØ�HÓ Ñ 4ð
 	��8�*ˆ~ÑˆÜ.¨s¸VÐXcÐcdÐelÐdmÐVnÑoÐor   r.   rO   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   rH   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   rH   rO   Ú	cache_keyÚcached_infoÚrespÚmetadataÚkÚvs
             r   rE   rE   ª   sá   € ô< ˜3¨ÑI€IÜ+×/Ñ/°	Ó:€KØÑÜ˜;×'Ñ'ØÐô ˜˜s¸FÑC€DÜ˜Ôä5°d·l±lÓC€HØÑÜÐQÓRÐRô Ô.×4Ñ4Ó6Ö7‰ˆÜ�q�>‹>Ü%×)Ñ)¨!¨TÖ2ñ 8ô
 Ô$Ó%Ô)GÓGÜ!×%Ñ%¤d¬4Ô0IÓ+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.
NrN   Ú|)r   ÚREPO_TYPE_MODELrX   r   Úkeysr7   rZ   )rK   rJ   rS   rc   s       r   Ú(reset_xet_connection_info_cache_for_repori   å   s_   € ð
 ÑÜ×-Ñ-ˆ	Øˆ{˜!˜G˜9 AÐ&€FÜÔ+×0Ñ0Ó2Ö3ˆØ�<‰<˜×ÓÜ%×)Ñ)¨!¨TÖ2ò 4r   rS   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   )Ú.0rc   rd   s      r   Ú	<genexpr>Ú_cache_key.<locals>.<genexpr>ö   s   é € ÐdÒ1c©¨˜Q˜C˜q  �*Ò1cùs   ‚c                 ó   • U S   $ )Nr   r   )Úxs    r   Ú<lambda>Ú_cache_key.<locals>.<lambda>ö   s   € Ð^_Ð`aÒ^br   )Úkeyrf   )rY   ÚlowerrV   ÚjoinÚsorted)	r.   r0   rH   rS   rc   rd   Úlower_headersÚauth_headerÚ
params_strs	            r   rU   rU   ò   s…   € à.5¯m©m¬oÔ>ªo¡d a�Q—W‘W“Y ’\©o€MÑ>Ø×#Ñ# O°RÓ8€KØ—‘Ñd´¸¿À2×8LÑ8LÓ8NÑTbÒ1cÓdÓd€JØˆX�Q�s�e˜1˜[˜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   rW   rW   ú   s$   € à×0Ñ0´C¼¿	º	»Ó4DÔGhÑ4hÑhÐhr   )N)NN)r€   Údataclassesr   Úenumr   Úhttpxrl   r   r   r	   r
   r�   r\   r   Údictr#   r$   r   r   r   ÚResponser;   rB   rF   rQ   rE   ri   rU   ÚboolrW   r   r   r   Ú<module>rˆ      s†  ðÜ Ý !Ý ã å ß EÑ Eð %'Ð !Ø!&Ð Ø<>Ð ˜4 Ð%8Ð 8Ñ9Ó >ô�3˜ô ñ
 �$Ñ÷ð ó ðñ
 �$Ñ÷ð ó ðñ °·±ð  È#ÐPTÉ*ð  Ð`kÐnrÑ`rõ  ðF°D¸¸c¸±Nð ÐGXÐ[_ÑG_ô ð2 ðQàðQð �#�s�(‰^ðQð ó	Qó ðQð6 ð  àØ$(ò,pàð,pð ð,pð ð	,pð
 �D‰jð,pð �#�s�(‰^ð,pð �D‰jð,pð ��c�‰N˜TÑ!ð,pð ô,pó ð,pð^ ð %)Ø#'ñ	7Ø	ð7à�#�s�(‰^ð7ð ��c�‰N˜TÑ!ð7ð ˜D‘jð	7ð
 ô7ó ð7ðt
3¸¸d¹
ð 
3ÈSð 
3ÐUYô 
3ñ8�Cð 8 $ s¨C x¡.ð 8¸$¸sÀC¸x¹.È4Ñ:Oð 8ÐY\Ð_cÑYcð 8Ðorõ 8ðiÐ!2ð i°tõ ir   