
    jN                         d dl Z 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gZ ej        d          ZdZdded	eez  d
eddfdZdeez  dedefdZ	 	 	 	 ddededz  dedz  dedz  dedz  defdZdS )    N)Path)tqdmdownload_from_urlz-([a-f0-9]*)\.zmindee/doctr   urlfilename
chunk_sizereturnc                    t          |d          5 }t          j                            t          j                            | dt
          i                    5 t          j                  5 }t          fdd          D ]0}|s n+|	                               |
                    |           1d d d            n# 1 swxY w Y   d d d            n# 1 swxY w Y   d d d            d S # 1 swxY w Y   d S )Nwbz
User-Agent)headers)totalc                  .                                    S )N)read)r	   responses   S/var/www/html/Carbon-Document/venv/lib/python3.11/site-packages/doctr/utils/data.py<lambda>z_urlretrieve.<locals>.<lambda>   s    (--
*C*C      )openurllibrequesturlopenRequest
USER_AGENTr   lengthiterupdatewrite)r   r   r	   fhpbarchunkr   s     `   @r   _urlretriever#      s   	h		 $^##FN$:$:3WaHb$:$c$cdd 	$hpHO,,, $!"C"C"C"C"CRHH $ $E  KK
+++HHUOOOO$ $ $ $ $ $ $ $ $ $ $ $ $ $ $	$ 	$ 	$ 	$ 	$ 	$ 	$ 	$ 	$ 	$ 	$ 	$ 	$ 	$ 	$$ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $s[   AC0C/AC5CCCC	CC0C	C0C	 C00C47C4	file_pathhash_prefixc                     t          | d          5 }t          j        |                                                                          }d d d            n# 1 swxY w Y   |d t          |                   |k    S )Nrb)r   hashlibsha256r   	hexdigestlen)r$   r%   fsha_hashs       r   _check_integrityr.   %   s    	i		 8!>!&&((++55778 8 8 8 8 8 8 8 8 8 8 8 8 8 8 &c+&&&';66s   9AAA	file_name	cache_dircache_subdirc           
      l   t          |t                    s4|                     d          d                             d          d         }|it          t          j                            dt          j                            t          j        	                    d          dd	                              n|}|3t                              |          }|r|                    d
          nd}|t          |          nt          ||          }|                    |          }|                                r+|t!          ||          rt#          j        d|            |S 	 |                    dd           nS# t(          $ rF d| }t          j                            dd          r|dz  }n|dz  }t#          j        |            w xY w	 t-          d|  d|            t/          | |           nt# t0          j        j        t4          f$ rV}	| dd         dk    r<|                     dd          } t-          d|  d|            t/          | |           n|	Y d}	~	nd}	~	ww xY wt          |t                    r7t!          ||          s't	          j        |           t;          d|  d          |S )aW  Download a file using its URL

    >>> from doctr.models import download_from_url
    >>> download_from_url("https://yoursource.com/yourcheckpoint-yourhash.zip")

    Args:
        url: the URL of the file to download
        file_name: optional name of the file once downloaded
        hash_prefix: optional expected SHA256 hash of the file
        cache_dir: cache directory
        cache_subdir: subfolder to use in the cache

    Returns:
        the location of the downloaded file

    Note:
        You can change cache directory location by using `DOCTR_CACHE_DIR` environment variable.
    /&r   NDOCTR_CACHE_DIR~z.cachedoctr   z"Using downloaded & verified file: T)parentsexist_okz#Failed creating cache directory at r   z8 using path from 'DOCTR_CACHE_DIR' environment variable.z`. You can change default cache directory using 'DOCTR_CACHE_DIR' environment variable if needed.zDownloading z to    httpszhttps:zhttp:z;Failed download. Trying https -> http instead. Downloading z corrupted download, the hash of z" does not match its expected value)
isinstancestr
rpartitionsplitosenvirongetpathjoin
expanduser
HASH_REGEXsearchgroupr   joinpathis_filer.   logginginfomkdirOSErrorerrorprintr#   r   URLErrorIOErrorreplaceremove
ValueError)
r   r/   r%   r0   r1   rfolder_pathr$   error_messagees
             r   r   r   ,   s   2 i%% :NN3''+11#66q9	  	BJNN,bgll27;M;Mc;R;RT\^e.f.fgghhh  i(($%/aggajjj4%1%9$y///tI|?\?\K$$Y//I  37G	S^7_7_ 3E)EEFFF$6666 	 	 	KkKK:>>+R00 	WWMMrM 	m$$$		1S11i11222S)$$$$L!7+   rr7g++h00CdPSddYbddeeei((((G )((((	 +s## e,<Y,T,T e
	)cCcccddds&   "E: :AG
%G4 4I%AI  I%)r   )NNNN)r(   rM   rB   reurllib.errorr   urllib.requestpathlibr   	tqdm.autor   __all__compilerH   r   r?   intr#   boolr.   r    r   r   <module>rf      st     				 				                    
 RZ)**

$ $c $TCZ $S $D $ $ $ $7d
 7 7 7 7 7 7 !" #M M	MTzM tM Tz	M
 *M 
M M M M M Mr   