wL iddlmZddlmZddlmZmZddlZddlm Z ddlm Z m Z m Z Gd d e eZed Gd dZed GddZ d"dej"dee deefdZdee e fdeefdZe dedee e fdefdZe dddddede de dee dee e fdee deee e fdefdZe d"d e dee e fdeee e fdefd!Zy)#) dataclass)Enum)DictOptionalN) constants) get_sessionhf_raise_for_statusvalidate_hf_hub_argsceZdZdZdZy) XetTokenTypereadwriteN)__name__ __module__ __qualname__READWRITE`/mnt/ssd/data/python-lab/Trading/venv/lib/python3.12/site-packages/huggingface_hub/utils/_xet.pyrr s D ErrT)frozenc"eZdZUeed<eed<y) XetFileData file_hash refresh_routeN)rrrstr__annotations__rrrrrs Nrrc,eZdZUeed<eed<eed<y)XetConnectionInfo access_tokenexpiration_unix_epochendpointN)rrrrrintrrrr!r!sMrr!responser$returnc|y |jtj}tj|jvr!|jtjd}n|jtj }||ntj}|jtjr>|jtjjd|jd}t||S#t $rYywxYw)aa 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. Nurl/)rr) headersrHUGGINGFACE_HEADER_X_XET_HASH$HUGGINGFACE_HEADER_LINK_XET_AUTH_KEYlinks&HUGGINGFACE_HEADER_X_XET_REFRESH_ROUTEKeyErrorENDPOINT startswithHUGGINGFACE_CO_URL_HOMEreplacerstripr)r&r$rrs r!parse_xet_file_data_from_responser6s"$$Y%L%LM  9 9X^^ K$NN9+Y+YZ[`aM$,,Y-]-]^M$/xY5G5GH A AB%--i.O.O.V.VWZ.[]e]l]lmp]qr #  sA7C:: DDr+c |tj}|tj}t|tj}t|||S#t t tf$rYywxYw)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_EXPIRATIONr0 ValueError TypeErrorr!)r+r$r"r#s r&parse_xet_connection_info_from_headersr=Bsp9FFGyNNO #GI,Y,Y$Z [ !3  j) ,sAAA)(A) file_datac\|j tdt|j|S)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.)rr;#_fetch_xet_connection_info_with_url)r>r+s rrefresh_xet_connection_inforA[s/.&YZZ .y/F/F PPr)revisionr$params token_typerepo_id repo_typerBrCc x||ntj}|d|d|d|jd| }t|||S)a 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/zs/z/xet-z-token/)rr1valuer@)rDrErFrBr+r$rCr)s r(fetch_xet_connection_info_from_repo_inforIwsPJ$/xY5G5GH JeI;b z7G7G6HPXz ZC .sGV DDrr)ctj|||}t|t|j}| t d|S)aS 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. )r+r)rCz6Xet headers have not been correctly set by the server.)r getr r=r+r;)r)r+rCrespmetadatas rr@r@sJ2 =  W#f  ED5dllCHQRR Or)N) dataclassesrenumrtypingrrrequestsrr r r rrrr!Responser6r=rArIr@rrrrTs!!DD3  $  $<@""+3C=" k"JDcNxPaGb2QQ#s(^Q QQ6 #"'+&E&E&E &E sm &E #s(^ &Esm&E T#s(^ $&E&E&ER(,  #s(^ T#s(^ $ r