
    0-Ph#                         d Z ddlmZ ddlZddlmZ ddlZddlZddlZddl	m
Z
mZ ddl	mZmZ  G d d	e          Z G d
 de          ZddZddZddZd Zd ZdS )z'Data downloading and reading functions
    )logN)path   )appdata_dirresource_dirs)StdoutProgressIndicatorurlopenc                       e Zd ZdZdS )InternetNotAllowedErrorzxPlugins that need resources can just use get_remote_file(), but
    should catch this error and silently ignore it.
    N__name__
__module____qualname____doc__     U/var/www/html/test/jupyter/venv/lib/python3.11/site-packages/imageio/core/fetching.pyr   r      s          	Dr   r   c                       e Zd ZdZdS )NeedDownloadErrorzIs raised when a remote file is requested that is not locally
    available, but which needs to be explicitly downloaded by the user.
    Nr   r   r   r   r   r      s           r   r   FTc                    d}|| z   }t          j        |           }|}|pt          d          }t                      }|                    d|           |D ]}	t          j        |	|          }
t          j        |
          r|s|r||	k    rt          j                            ||          }t          j	        t          j
        |                    s8t          j        t          j        t          j
        |                               t          j        |
|           |c S |
c S t          |t                     rt#          j        |d          }t#          j        t          j        |
                    }||k    r|r||	k    rt          j                            ||          }t          j	        t          j
        |                    s8t          j        t          j        t          j
        |                               t          j        |
|           |c S |
c S t+          d|z              nt          j        dd                                          dv rt1          d	          |st3                      t          j        ||          }
t          j	        t          j
        |
                    s8t          j        t          j        t          j
        |
                               t          j        d
d          r\t5          d          D ]:}	 t7          ||
           |
c S # t8          $ r t#          j        d           Y 7w xY wt7          ||
           |
S t7          ||
           |
S )a  Get a the filename for the local version of a file from the web

    Parameters
    ----------
    fname : str
        The relative filename on the remote data repository to download.
        These correspond to paths on
        ``https://github.com/imageio/imageio-binaries/``.
    directory : str | None
        The directory where the file will be cached if a download was
        required to obtain the file. By default, the appdata directory
        is used. This is also the first directory that is checked for
        a local version of the file. If the directory does not exist,
        it will be created.
    force_download : bool | str
        If True, the file will be downloaded even if a local copy exists
        (and this copy will be overwritten). Can also be a YYYY-MM-DD date
        to ensure a file is up-to-date (modified date of a file on disk,
        if present, is checked).
    auto : bool
        Whether to auto-download the file if its not present locally. Default
        True. If False and a download is needed, raises NeedDownloadError.

    Returns
    -------
    fname : str
        The path to the file on the local system.
    z7https://github.com/imageio/imageio-binaries/raw/master/imageior   z%Y-%m-%dzFile older than %s, updating...IMAGEIO_NO_INTERNET )1trueyeszeWill not download resource from the internet because environment variable IMAGEIO_NO_INTERNET is set.CONTINUOUS_INTEGRATIONF   g      ?)opnormcaser   r   insertjoinisfileosr   isdirdirnamemakedirsabspathshutilcopy
isinstancestrtimestrptimegmtimegetctimeprintgetenvlowerr   r   range_fetch_fileIOErrorsleep)fname	directoryforce_downloadauto	_url_rooturlnfnamegiven_directorydirsdirfilename	filename2ntimeftimeis                  r   get_remote_filerG   !   sU   : JI
e
C[FO9;y#9#9I??DKK9  73''9X 	!  " %#'='= "_f E EI8BJy$9$9:: GBJrz)/D/D$E$EFFFK)444$$$$.#.. njAABK$9$9::E>>& )?c+A+A$&GLL&$I$I	!x
9(=(=>> KK
2:i3H3H(I(IJJJHi888((((#OOO;nLMMME 
y&++11337KKK%*
 
 	
  "!!! wy&))H8BJx(()) 6
BJrz(3344555	y)511 q 	 	A C***      
3  X&&&OC"""s   L,,MMc                    t          dt          j                            |          z             |dz   }d}d}g }t	          d          D ]}	 t          | d          }t          |j        d                                                   }	t          |	          }
t          d	|d
z   | |
fz             t          |d          }t          |||           |j        s|                                 t          j        ||           |du r"t           j                            d|z             	 ||j        s|                                  dS  dS  dS # t&          $ r>}|                    |           t          dt+          |          z             Y d}~nd}~ww xY w	 ||j        s|                                 r# ||j        s|                                 w w w xY wt-          dt          j                            |          z            )a  Load requested file, downloading it if needed or requested

    Parameters
    ----------
    url: string
        The url of file to be downloaded.
    file_name: string
        Name, along with the path, of where downloaded file will be saved.
    print_destination: bool, optional
        If true, destination of where file was saved will be printed after
        download finishes.
    resume: bool, optional
        If true, try to resume partially downloaded files.
    z?Imageio: %r was not found on your computer; downloading it now.z.partNr      g      @)timeoutContent-LengthzTry %i. Download from %s (%s)r   wb)initial_sizeTzFile saved as %s.
zError while fetching file: %s.zhUnable to download %r. Perhaps there is no internet connection? If there is, please report this problem.)r1   r$   r   basenamer4   r	   intheadersstrip_sizeof_fmtopen_chunk_readclosedcloser)   movesysstdoutwrite	Exceptionappendr,   r6   )r=   	file_nameprint_destinationtemp_file_name
local_filerM   errorstriesremote_file	file_sizesize_stres               r   r5   r5      sx   $ 
	 " 0 0 ; ;	<  
 (NJLFq 
 
	'!#s333KK/0@AGGIIJJI"9--H1UQYX4NNOOOnd33JZlKKKK$ #  """K	222 D((
  !6!BCCC
 %!( '$$&&&&&& &%%' ' '  	= 	= 	=MM!2SVV;<<<<<<<<	=<%!( '$$&&& %!( '$$&&&& &' Cgy))*
 
 	
s+   CE
F4FF<FF<<!G    c                 \   |}t          | j        d                                                   }||z  }t          d          }|                    dd|           	 |                     |          }|t          |          z  }|snt          |||           <|                    d           dS )a  Download a file chunk by chunk and show advancement

    Can also be used when resuming downloads over http.

    Parameters
    ----------
    response: urllib.response.addinfourl
        Response to the download request in order to get file size.
    local_file: file
        Hard disk file where data should be written.
    chunk_size: integer, optional
        Size of downloaded chunks. Default: 8192
    initial_size: int, optional
        If resuming, indicate the initial size of the file.
    rK   Downloadingr   bytesTDoneN)	rO   rP   rQ   r   startreadlen_chunk_writefinish)responser`   
chunk_sizerM   bytes_so_far
total_sizeprogresschunks           r   rT   rT      s    &  L X%&67==??@@J,J&}55HNN2w
+++2j))E

" 	UJ1112 OOFr   c                     |                     |            |                    t          |                      t          j        d           dS )z1Write a chunk to file and update the progress barr   N)rZ   increase_progressrn   r-   r7   )rv   r`   ru   s      r   ro   ro      sA    Us5zz***JqMMMMMr   c                 *   g d}g d}	 | dk    r{t          t          t          | d                    t          |          dz
            }t	          |           d|z  z  }||         }||         }d|z  }|                    ||          S | dk    rdndS )	z,Turn number of bytes into human-readable str)rj   kBMBGBTBPB)r   r   r   r   r   r   r   i   z{0:.%sf} {1}r   z0 bytesz1 byte)minrO   r   rn   floatformat)numunitsdecimalsexponentquotientunitnum_decimalsformat_strings           r   rR   rR      s    333E!!!H"
Qwws3sD>>**CJJN;;::h.X)&5##Hd333q99h.r   )NFT)T)rg   r   )r   mathr   r$   r   r   rX   r)   r-   r   r   r   r   r	   r6   r   r   rG   r5   rT   ro   rR   r   r   r   <module>r      sH  
        				       



   ( ( ( ( ( ( ( ( . . . . . . . .	 	 	 	 	g 	 	 	       ` ` ` `F8
 8
 8
 8
v" " " "J  / / / / /r   