o
    +if=)                     @   s   d dl Z d dlZd dlZd dlZd dlZd dlmZ d dlmZ d dl	m
Z
 d dlmZmZ d dlZd dlmZmZ d dlmZ d dlmZmZmZmZmZmZmZmZmZmZ d d	lm Z  	dd
e!fddZ"									 			dd
e!fddZ#dS )    N)contextmanager)partial)Path)urljoinurlparse)DownloadConfigconfig)ExtractManager)
!_raise_if_offline_mode_is_enabledftp_getftp_head"get_authentication_headers_for_urlhash_url_to_filenamehttp_get	http_headis_local_pathis_remote_urllogger)FileLockreturnc                 K   s   |du rt d	i |}|jptj}t|trt|}t| tr#t| } t| rBt| ||j	|j
|j|j|j|j|j|j|j|jd}ntj| rK| }nt| rWtd|  dtd|  d|du re|S |jrtt|jdj||jd}|S )
a  
    Given something that might be a URL (or might be a local path),
    determine which. If it's a URL, download the file and cache it, and
    return the path to the cached file. If it's already a local path,
    make sure the file exists and then return the path.

    Return:
        Local path (string)

    Raises:
        FileNotFoundError: in case of non-recoverable file
            (non-existent or no cache on disk)
        ConnectionError: in case of unreachable url
            and no cache on disk
        ValueError: if it couldn't parse the url or filename correctly
        requests.exceptions.ConnectionError: in case of internet connection issue
    N)	cache_dirforce_downloadproxiesresume_download
user_agentlocal_files_onlyuse_etagmax_retriesuse_auth_tokenignore_url_paramsdownload_desczLocal file z doesn't existzunable to parse z as a URL or as a local path)r   )force_extract )r   r   r   DOWNLOADED_DATASETS_PATH
isinstancer   strr   get_from_cacher   r   r   r   r   r   r   r   r   r    ospathexistsr   FileNotFoundError
ValueErrorextract_compressed_filer	   extractr!   )url_or_filenamedownload_configdownload_kwargsr   output_pathr"   r"   M/var/www/html/corbot_env/lib/python3.10/site-packages/datasets/utils/cache.pycached_path   sD   

r3   Fd   Tc           !         s  |du rt j}t|trt|}tj|dd |r"t| t| j	}n| }d}d}d}d}d}t
|dd}tj	||}tj	|rG|sG|sG|S t| |
d}|durU||d< |s| drat| }zt| d|||	|d	}|jd
kr|ry|jdnd}|j D ]\}}|drd| v r| d| 7 } |j}qd}d| v rd| vr| d7 } nN|jdkrd| v s|jdkrd| v s|jdkrtd| std|js|jdkrd| v rd}td|   n|jdkrt j| v r|
du rtd|  dW n ttjj fy } z|}W Y d}~nd}~ww |sltj	|r|s|S |r)t!d| d|dur;|jdkr;t!d|  t"d |   |durTtd!|  d"t#| d#|duretd!|  d$|j d#td!|  t
||}tj	||}tj	|r|s|S |d% }t$| |r|d&  t% fd'd(}|}tj	 rt& j'}nd)}n
t(t)j*|dd*}d)}| +}t|  d+|j+  | drt,| | nt-| ||||||	|d, W d   n	1 sw   Y  td-|  d.|  t./|j+| td/|  | |d0}|d1 }t0|d2d3d4} t12||  W d   n1 s,w   Y  W d   |S W d   |S 1 sEw   Y  |S )5a  
    Given a URL, look for the corresponding file in the local cache.
    If it's not there, download it. Then return the path to the cached file.

    Return:
        Local path (string)

    Raises:
        FileNotFoundError: in case of non-recoverable file
            (non-existent or no cache on disk)
        ConnectionError: in case of unreachable url
            and no cache on disk
    NT)exist_okF)etag)r   z
user-agentzftp://)allow_redirectsr   timeoutr   headers   ETagdownload_warningzdrive.google.comz	&confirm=zconfirm=z
&confirm=ti  zfirebasestorage.googleapis.comi  i  z7^https?://github.com/.*?/.*?/releases/download/.*?/.*?$z#^https://.*?s3.*?amazonaws.com/.*?$zndownloader.figstatic.comz"Couldn't get ETag version for url i  zUnauthorized for URL z^. Please use the parameter `use_auth_token=True` after logging in with `huggingface-cli login`z6Cannot find the requested files in the cached path at zi and outgoing traffic has been disabled. To enable file online look-ups, set 'local_files_only' to False.i  zCouldn't find file at zTried to reach zCouldn't reach z ()z (error z.lockz.incompletec                  3   s8    t  d} | V  W d    d S 1 sw   Y  d S )Nza+b)open)fincomplete_pathr"   r2   _resumable_file_manager   s   "z/get_from_cache.<locals>._resumable_file_managerr   )dirdeletezB not found in cache or force_download set to True, downloading to )r   resume_sizer9   cookiesr   desczstoring z in cache at zcreating metadata file for )urlr6   z.jsonwzutf-8)encoding)3r   HF_DATASETS_CACHEr$   r   r%   r'   makedirsr   r   r(   r   joinr)   r   
startswithr   r   status_coder9   getrF   itemsrematchrH   r   infoHF_ENDPOINTConnectionErrorOSErrorrequests
exceptionsTimeoutr*   r
   reprr   r   statst_sizer   tempfileNamedTemporaryFilenamer   r   shutilmover>   jsondump)!rH   r   r   r   etag_timeoutr   r   r   r   r   r   r   r    
cached_url	connectedresponserF   r6   
head_errorfilename
cache_pathr9   kve	lock_pathrB   temp_file_managerrE   	temp_filemeta	meta_path	meta_filer"   r@   r2   r&   b   s   











 
/
//r&   )N)NFNr4   FNFTr   NFN)$rc   r'   rR   ra   r^   
contextlibr   	functoolsr   pathlibr   urllib.parser   r   rX   datasetsr   r   datasets.utils.extractr	   datasets.utils.file_utilsr
   r   r   r   r   r   r   r   r   r   datasets.utils.filelockr   r%   r3   r&   r"   r"   r"   r2   <module>   sB    0
F