
    2VhC                     B   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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 d dlmZ d dlmZ d Zd	 Zd
 Zd Zd ZddZ ed      	 	 	 	 	 	 	 	 	 	 	 dd       Zd dZd!dZd"dZd Zd Zd Z d#dZ!d Z"d Z#d Z$d Z%d Z&d Z'd Z(y)$    N)urlretrieve)keras_export)config)io_utils)gfile)Progbarc                 d    t        | t        j                        rt        j                  |       S | S )a  Convert `PathLike` objects to their string representation.

    If given a non-string typed path object, converts it to its string
    representation.

    If the object passed to `path` is not among the above, then it is
    returned unchanged. This allows e.g. passthrough of file objects
    through this function.

    Args:
        path: `PathLike` object that represents a path

    Returns:
        A string representation of the path argument, if Python support exists.
    )
isinstanceosPathLikefspathpaths    J/home/dcms/DCMS/lib/python3.12/site-packages/keras/src/utils/file_utils.pypath_to_stringr      s%      $$yyK    c                 z    t         j                  j                  t         j                  j                  |             S N)r   r   realpathabspathr   s    r   resolve_pathr   '   s$    77BGGOOD122r   c                 r    t        t        j                  j                  ||             j	                  |      S r   )r   r   r   join
startswith)r   base_dirs     r   is_path_in_dirr   +   s'    Xt45@@JJr   c                     t        t        j                  j                  |t        j                  j	                  | j
                                    }t        | j                  |      S )N)r   )r   r   r   r   dirnamenamer   linkname)infobasetips      r   is_link_in_dirr$   /   s=    
rww||D"''//$))*DE
FC$--#66r   c              #   "  K   t        d      }| D ]{  }d}t        |j                  |      rd}| n2|j                         s|j	                         rt        ||      rd}| |rWt        j                  d|j                   dd       } y w)N.FTz2Skipping invalid path during archive extraction: 'z'.   
stacklevel)r   r   r   issymislnkr$   warningswarn)membersr   finfo
valid_paths       r   filter_safe_pathsr1   4   s     C H 
%**h/JK[[]ekkmeX.!
MMJJ<r#s   A$B'(Bc                    |y|dk(  rddg}t        |t              r|g}t        |       } t        |      }|D ]  }|dk(  r!t        j                  }t        j
                  }n1|dk(  r!t        j                  }t        j                  }nt        |       ||       sc ||       5 }	 t        j                  |       r|j                  |       n|j                  |t        |             ddd        y y# t        j                  t        t        f$ rk t        j                   j#                  |      rJt        j                   j%                  |      rt        j&                  |        t)        j*                  |        w xY w# 1 sw Y    yxY w)a  Extracts an archive if it matches a support format.

    Supports `.tar`, `.tar.gz`, `.tar.bz`, and `.zip` formats.

    Args:
        file_path: Path to the archive file.
        path: Where to extract the archive file.
        archive_format: Archive format to try for extracting the file.
            Options are `"auto"`, `"tar"`, `"zip"`, and `None`.
            `"tar"` includes `.tar`, `.tar.gz`, and `.tar.bz` files.
            The default `"auto"` uses `["tar", "zip"]`.
            `None` or an empty list will return no matches found.

    Returns:
        `True` if a match was found and an archive extraction was completed,
        `False` otherwise.
    NFautotarzip)r.   T)r
   strr   tarfileopen
is_tarfilezipfileZipFile
is_zipfileNotImplementedError
extractallr1   TarErrorRuntimeErrorKeyboardInterruptr   r   existsisfileremoveshutilrmtree)	file_pathr   archive_formatarchive_typeopen_fnis_match_fnarchives          r   extract_archiverM   G   sh   $ .#&()y)I$D& 5 llG!,,KU"ooG!,,K%l33y!# w)))4**40  ** *;G*D + " 9:   ((,8IJ ww~~d+77>>$/IIdO  #MM$/" s    #F %AC44B	E==F  F
	zkeras.utils.get_filec                    |t        d      |	t        j                         }	|||}d}t        j                  j                  |	      }t        j                  |t        j                        s t        j                  j                  dd      }t        j                  j                  ||      }t        j                  |d       | }t        |       } | sWt        j                  j                  t        j                  j                  |      j                        } | s/t        d| d	      t        j                  | v rt        d
|        |s|r|rd| v rXt        j                  j                  ||       }| d| j!                  d       } t        j                  j                  || dz         }nt        j                  j                  ||       }t        j                  j                  || dz         }ndt        j                  j                  ||       }t        j                  j                  || dz         }n t        j                  j                  ||       }|
rd}nRt        j                  j#                  |      r1d}|/t%        |||      s!t'        j(                  d| d| d       d}nd}|rzt'        j(                  d|         G d d      }d}	 	 t+        || |              	 t        j                  j#                  |      r"| t%        |||      st        d| d| d      |s|r,|rd}tC        ||      }|stE        jF                  dd       |S |S # t        j,                  j.                  $ r5}t1        |j3                  ||j4                  |j6                              d}~wt        j,                  j8                  $ r5}t1        |j3                  ||j:                  |j<                              d}~ww xY w# t0        t>        f$ r6 t        j                  j#                  |      rt        j@                  |        w xY w)uW  Downloads a file from a URL if it not already in the cache.

    By default the file at the url `origin` is downloaded to the
    cache_dir `~/.keras`, placed in the cache_subdir `datasets`,
    and given the filename `fname`. The final location of a file
    `example.txt` would therefore be `~/.keras/datasets/example.txt`.
    Files in `.tar`, `.tar.gz`, `.tar.bz`, and `.zip` formats can
    also be extracted.

    Passing a hash will verify the file after download. The command line
    programs `shasum` and `sha256sum` can compute the hash.

    Example:

    ```python
    path_to_downloaded_file = get_file(
        origin="https://storage.googleapis.com/download.tensorflow.org/example_images/flower_photos.tgz",
        extract=True,
    )
    ```

    Args:
        fname: If the target is a single file, this is your desired
            local name for the file.
            If `None`, the name of the file at `origin` will be used.
            If downloading and extracting a directory archive,
            the provided `fname` will be used as extraction directory
            name (only if it doesn't have an extension).
        origin: Original URL of the file.
        untar: Deprecated in favor of `extract` argument.
            Boolean, whether the file is a tar archive that should
            be extracted.
        md5_hash: Deprecated in favor of `file_hash` argument.
            md5 hash of the file for file integrity verification.
        file_hash: The expected hash string of the file after download.
            The sha256 and md5 hash algorithms are both supported.
        cache_subdir: Subdirectory under the Keras cache dir where the file is
            saved. If an absolute path, e.g. `"/path/to/folder"` is
            specified, the file will be saved at that location.
        hash_algorithm: Select the hash algorithm to verify the file.
            options are `"md5'`, `"sha256'`, and `"auto'`.
            The default 'auto' detects the hash algorithm in use.
        extract: If `True`, extracts the archive. Only applicable to compressed
            archive files like tar or zip.
        archive_format: Archive format to try for extracting the file.
            Options are `"auto'`, `"tar'`, `"zip'`, and `None`.
            `"tar"` includes tar, tar.gz, and tar.bz files.
            The default `"auto"` corresponds to `["tar", "zip"]`.
            None or an empty list will return no matches found.
        cache_dir: Location to store cached files, when None it
            defaults ether `$KERAS_HOME` if the `KERAS_HOME` environment
            variable is set or `~/.keras/`.
        force_download: If `True`, the file will always be re-downloaded
            regardless of the cache state.

    Returns:
        Path to the downloaded file.

    **⚠️ Warning on malicious downloads ⚠️**

    Downloading something from the Internet carries a risk.
    NEVER download a file/archive if you do not trust the source.
    We recommend that you specify the `file_hash` argument
    (if the hash of the source file is known) to make sure that the file you
    are getting is the one you expect.
    NzCPlease specify the "origin" argument (URL of the file to download).md5z/tmpz.kerasT)exist_okz5Can't parse the file name from the origin provided: 'z&'.Please specify the `fname` argument.zPaths are no longer accepted as the `fname` argument. To specify the file's parent directory, use the `cache_dir` argument. Received: fname=r&   
_extracted_archiveF)	algorithmzNA local file was found, but it seems to be incomplete or outdated because the z0 file hash does not match the original value of z! so we will re-download the data.zDownloading data from c                       e Zd ZdZd Zd Zy)get_file.<locals>.DLProgbarz1Manage progress bar state for use in urlretrieve.c                      d | _         d| _        y )NF)progbarfinished)selfs    r   __init__z$get_file.<locals>.DLProgbar.__init__   s    # %r   c                 X   |dk(  rd }| j                   st        |      | _         ||z  }|| j                   j                  |       y ||k  r| j                   j                  |       y | j                  s7| j                   j                  | j                   j                         d| _        y y )NT)rW   r   updaterX   target)rY   	block_num
block_size
total_sizecurrents        r   __call__z$get_file.<locals>.DLProgbar.__call__$  s    #!%J||#*:#6DL#j0%LL''0+++G4!]]++DLL,?,?@(, +r   N)__name__
__module____qualname____doc__rZ   rc    r   r   	DLProgbarrU     s    C&-r   ri   z!URL fetch failure on {}: {} -- {}z+Incomplete or corrupted file detected. The z0 file hash does not match the provided value of r4   zCould not extract archive.r'   r(   )$
ValueErrorr   
keras_homer   r   
expanduseraccessW_OKr   makedirsr   basenameurllibparseurlsplitsepfindrB   validate_filer   	print_msgr   error	HTTPError	ExceptionformatcodemsgURLErrorerrnoreasonrA   rD   rM   r,   r-   )fnameoriginuntarmd5_hash	file_hashcache_subdirhash_algorithmextractrH   	cache_dirforce_downloaddatadir_basedatadirprovided_fnamedownload_targetextraction_dirdownloadri   	error_msgestatuss                        r   get_filer      s   ` ~
 	

 %%'		 1	77%%i0L99\277+ww||FH5ggll<6GKK$'N5!E  !6!6v!>!C!CD8 77  66U?==BGE  %e|"$'',,w">/

30!#gu|7K!L!#gu!="$'',,w
8J"KWW\\'59N ggll7EJ4FGO'',,w6		(  n ""::H9I JF k!BD  3F8<=	- 	-. 8	
	MFOY[A 77>>/*y/D n !)* +#A'  %"N ^^
 MM61EE <<)) I	 0 0 GHH<<(( M	 0 0!(( KLLM,- 	ww~~o.		/*	s1   M O=80N(( O=0O88O==P   AQc                     | dk(  rt        j                         S | dk(  r$|"t        |      dk(  rt        j                         S t        j                         S )z+Returns hash algorithm as hashlib function.sha256r3   @   )hashlibr   lenrO   )rS   r   s     r   resolve_hasherr   ]  sJ    H~~Fy4Y29M~~ ;;=r   c                    t        |t              rt        |      }n|}t        | d      5 t	        fdd      D ]  }|j                  |        	 ddd       |j                         S # 1 sw Y   |j                         S xY w)a  Calculates a file sha256 or md5 hash.

    Example:

    >>> hash_file('/path/to/file.zip')
    'e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855'

    Args:
        fpath: Path to the file being validated.
        algorithm: Hash algorithm, one of `"auto"`, `"sha256"`, or `"md5"`.
            The default `"auto"` detects the hash algorithm in use.
        chunk_size: Bytes to read at a time, important for large files.

    Returns:
        The file hash.
    rbc                  &    j                         S r   )read)
chunk_size
fpath_files   r   <lambda>zhash_file.<locals>.<lambda>  s    *//*"= r   r   N)r
   r6   r   r8   iterr]   	hexdigest)fpathrS   r   hasherchunkr   s     `  @r   	hash_filer   i  s    " )S!	*	eT	 !j=sC 	!EMM% 	!! 	! s   'A--Bc                 b    t        ||      }t        t        | ||            t        |      k(  ryy)a  Validates a file against a sha256 or md5 hash.

    Args:
        fpath: path to the file being validated
        file_hash:  The expected hash string of the file.
            The sha256 and md5 hash algorithms are both supported.
        algorithm: Hash algorithm, one of `"auto"`, `"sha256"`, or `"md5"`.
            The default `"auto"` detects the hash algorithm in use.
        chunk_size: Bytes to read at a time, important for large files.

    Returns:
        Boolean, whether the file is valid.
    TF)r   r6   r   )r   r   rS   r   r   s        r   rv   rv     s0     Iy1F
9UFJ/0C	NBr   c                 D    t        j                  dt        |             ryy)a  
    Determines if a given filepath indicates a remote location.

    This function checks if the filepath represents a known remote pattern
    such as GCS (`/gcs`), CNS (`/cns`), CFS (`/cfs`), HDFS (`/hdfs`), Placer
    (`/placer`), TFHub (`/tfhub`), or a URL (`.*://`).

    Args:
        filepath (str): The path to be checked.

    Returns:
        bool: True if the filepath is a recognized remote path, otherwise False
    z:^(/cns|/cfs|/gcs|/hdfs|/readahead|/placer|/tfhub|.*://).*$TF)rematchr6   )filepaths    r   is_remote_pathr     s"     
xxEH r   c                     t        d|        )Nz]Handling remote paths requires installing TensorFlow (in order to use gfile). Received path: )rj   r   s    r   _raise_if_no_gfiler     s    
	337&	: r   c                     t        |       r0t        j                  rt        j                  |       S t	        |        t
        j                  j                  |       S r   )r   r   	availablerB   r   r   r   r   s    r   rB   rB     s;    d??<<%%t$77>>$r   c                     t        |       r2t        j                  rt        j                  | |      S t	        |        t        | |      S )N)mode)r   r   r   GFiler   r8   )r   r   s     r   Filer     s7    d??;;t$//t$4  r   c                     t        |       r1t        j                  rt        j                  | g| S t	        |        t        j                  j                  | g| S r   )r   r   r   r   r   r   r   )r   pathss     r   r   r     sE    d??::d+U++t$77<<%u%%r   c                     t        |       r0t        j                  rt        j                  |       S t	        |        t
        j                  j                  |       S r   )r   r   r   isdirr   r   r   r   s    r   r   r     s;    d??;;t$$t$77==r   c                     t        |       r0t        j                  rt        j                  |       S t	        |        t        j                  |       S r   )r   r   r   rD   r   r   r   s    r   rD   rD     s6    d??<<%%t$99T?r   c                     t        |       r0t        j                  rt        j                  |       S t	        |        t        j                  |       S r   )r   r   r   rF   r   rE   r   s    r   rF   rF     s7    d??<<%%t$==r   c                     t        |       r0t        j                  rt        j                  |       S t	        |        t        j                  |       S r   )r   r   r   listdirr   r   r   s    r   r   r     s7    d??==&&t$::dr   c                     t        |       st        |      r9t        j                  rt        j                  | |d      S t	        d|  d|        t        j                  | |      S )NT)	overwritezsrc=z dst=)r   r   r   copyr   rE   )srcdsts     r   r   r     sQ    cnS1??::c3$77cU%u56;;sC  r   c                     t        |       r0t        j                  rt        j                  |       S t	        |        t        j                  |       S r   )r   r   r   ro   r   r   r   s    r   ro   ro     s7    d??>>$''t$;;tr   )r&   r3   )NNFNNdatasetsr3   Fr3   NFr   )r     )r3   r   )r))r   r   r   rE   r7   rq   r,   r:   urllib.requestr   keras.src.api_exportr   keras.src.backendr   keras.src.utilsr   keras.src.utils.module_utilsr   keras.src.utils.progbarr   r   r   r   r$   r1   rM   r   r   r   rv   r   r   rB   r   r   r   rD   rF   r   r   ro   rh   r   r   <module>r      s     	 	      & - $ $ . +*3K7
&9x $%

V &Vr	:,2 !&!r   