
    *#h=)                         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Z"	 	 	 	 	 	 	 	 	 	 	 	 dd
e!fdZ#y)    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                    |t        di |}|j                  xs t        j                  }t	        |t
              rt        |      }t	        | t
              rt        |       } t        |       r|t        | ||j                  |j                  |j                  |j                  |j                  |j                  |j                  |j                   |j"                  |j$                        }nKt&        j(                  j+                  |       r| }n)t-        |       rt/        d|  d      t1        d|  d      ||S |j2                  r1t5        |j                        j7                  ||j8                        }|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
    )	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_paths        Q/var/www/html/sandstorm/venv/lib/python3.12/site-packages/datasets/utils/cache.pycached_pathr5      s\   , (;?;))LV-L-LI)T"	N	/4(o._%$*99#+++;;&11,==$--'33*99-??)77
 
	(%		'+o->n MNN +O+<<XYZZ..$/H/HIQQ'D'D R 
     c                 j
  ! |t         j                  }t        |t              rt	        |      }t        j                  |d       |r t        | t        |       j                        }n| }d}d}d}d}d}t        |d      }t
        j                  j                  ||      }t
        j                  j                  |      r|s|s|S t        | |
      }|||d<   |s| j                  d      rt        |       }	 t!        | d|||	|	      }|j"                  d
k(  r||r|j$                  j'                  d      nd}|j(                  j+                         D ]0  \  }}|j                  d      sd| v s| d|z   z  } |j(                  }2 d}d| v rd| vr| dz  } n|j"                  dk(  rd| v sk|j"                  dk(  rd| v sX|j"                  dk(  r6t-        j.                  d|       s3t-        j.                  d|j0                        s|j"                  dk(  rd| v rd}t3        j4                  d|         n2|j"                  dk(  r#t         j6                  | v r|
t9        d|  d      |st
        j                  j                  |      r|s|S |rtC        d| d      ||j"                  dk(  rtC        d|        tE        d |         |t9        d!|  d"tG        |       d#      |t9        d!|  d$|j"                   d#      t9        d!|        t        ||      }t
        j                  j                  ||      }t
        j                  j                  |      r|s|S |d%z   }tI        |      5  |rW|d&z   !tJ        !fd'       }|}t
        j                  j                  !      r t        jL                  !      jN                  }n!d(}ntQ        tR        jT                  |d)      }d(} |       5 }t3        j4                  |  d*|jV                          | j                  d      rtY        | |       nt[        | ||||||	|+       ddd       t3        j4                  d,|  d-|        t]        j^                  jV                  |       t3        j4                  d.|        | |d/}|d0z   }ta        |d1d23      5 } tc        jd                  ||        ddd       ddd       |S # t:        t<        j>                  j@                  f$ r}|}Y d}~d}~ww xY w# 1 sw Y   xY w# 1 sw Y   RxY w# 1 sw Y   |S xY w)4a  
    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   X   K   t        d      5 } |  d d d        y # 1 sw Y   y xY ww)Nza+b)open)fincomplete_paths    r4   _resumable_file_managerz/get_from_cache.<locals>._resumable_file_manager   s,     /51 QG  s   *	*'*r   )dirdeletezB not found in cache or force_download set to True, downloading to )r   resume_sizer<   cookiesr   desczstoring z in cache at zcreating metadata file for )urlr9   z.jsonwzutf-8)encoding)3r	   HF_DATASETS_CACHEr&   r   r'   r)   makedirsr   r   r*   r   joinr+   r   
startswithr   r   status_coder<   getrI   itemsrematchrK   r   infoHF_ENDPOINTConnectionErrorOSErrorrequests
exceptionsTimeoutr,   r   reprr   r   statst_sizer   tempfileNamedTemporaryFilenamer   r   shutilmoverB   jsondump)"rK   r   r   r   etag_timeoutr   r   r   r   r   r    r!   r"   
cached_url	connectedresponserI   r9   
head_errorfilename
cache_pathr<   kve	lock_pathrE   temp_file_managerrH   	temp_filemeta	meta_path	meta_filerD   s"                                    @r4   r(   r(   b   sO   8 ,,	)T"	N	KK	D)S(3-"4"45

IHGDJ $JT:Hi2J	ww~~j!. 1^TG * >>(# I*	  $$'H ##s*7?x''++F3T$,,224 3DAq||$67<NRU<U{Q."*"2"2	3
 !	%,31F<'C %%,1QUX1X((C/4F#4M((C/![]`a88$JHLLY ((C/4OSV4V 	@FG%%,1C1Cs1J~Oe%+C5  1O  P  77>>*%n#H U^ ^  !h&:&:c&A#&<SE$BCC)OC5*AB!!OC54
;K:LA"NOO!!OC5AUAU@VVW"XYY!OC5"9:: $J5Hi2J	ww~~j!. W$I	)	 -'(=8O  !8ww~~o. ggo6>> '(C(C[` aK   	IKK3%abkbpbpaqrs ~~h'Y'# +## +&		$ 	hse==>INNJ/1*>?D)(	)S73 	'yIIdI&	'Y-'^ e ,,445 	J	n	 	0	' 	'Y-'^ se   :A0S +S 0C1S ,A?T(+ATA3T(4TT(#T TTT	T(T%	!T((T2)N)NFNd   FNFTr   NFN)$rf   r)   rU   rd   ra   
contextlibr   	functoolsr   pathlibr   urllib.parser   r   r[   datasetsr   r	   datasets.utils.extractr
   datasets.utils.file_utilsr   r   r   r   r   r   r   r   r   r   datasets.utils.filelockr   r'   r5   r(   r$   r6   r4   <module>r      s     	 	   %   *  + 1   -
 A 		AL y 	yr6   