U
    ?h#                     @   s   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	 d	eZG d
d deZdddZdddZdddZdd Zdd ZdS )z'Data downloading and reading functions
    )logN)path   )appdata_dirresource_dirs)StdoutProgressIndicatorurlopenc                   @   s   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__ r   r   G/var/www/html/venv/lib/python3.8/site-packages/imageio/core/fetching.pyr	      s   r	   c                   @   s   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   FTc              	   C   s0  d}||  }t | }|}|p$td}t }|d| |D ]}	t |	|}
t |
r<|s|r||	krtj||}t 	t 
|stt t 
| t|
| |  S |
  S t|tr<t|d}tt |
}||krJ|rB||	krBtj||}t 	t 
|s.tt t 
| t|
| |  S |
  S td|   q^q<tdd dkr|td	|st t ||}
t 	t 
|
stt t 
|
 td
drtdD ]<}zt||
 |
W   S  tk
r
   td Y nX qt||
 |
S t||
 |
S d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/Zimageior   z%Y-%m-%dzFile older than %s, updating...ZIMAGEIO_NO_INTERNET )1trueyeszeWill not download resource from the internet because environment variable IMAGEIO_NO_INTERNET is set.ZCONTINUOUS_INTEGRATIONF   g      ?N)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	directoryZforce_downloadautoZ	_url_rooturlZnfnameZgiven_directorydirsdirfilename	filename2ZntimeZftimeir   r   r   get_remote_file!   sf    








r9   c                 C   sF  t dtj|  |d }d}d}g }tdD ]}zzt| dd}t|j	d 
 }	t|	}
t d	|d
 | |
f  t|d}t|||d |js|  t|| |dkrtjd|  W W L qBW n> tk
r
 } z|| t dt|  W 5 d}~X Y nX W 5 |dk	r*|js*|  X q0tdtj| dS )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.)r)   r   r   basenamer,   closedcloser   intheadersstrip_sizeof_fmtopen_chunk_readr!   movesysstdoutwrite	Exceptionappendr$   r.   )r3   	file_nameZprint_destinationZtemp_file_name
local_filer>   errorstriesZremote_file	file_sizesize_strer   r   r   r-      sD    


&

r-       c                 C   sl   |}t | jd  }||7 }td}|dd| | |}|t|7 }|sPq^t||| q4|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.
    r<   ZDownloadingr   bytesDoneN)	rB   rC   rD   r   startreadlen_chunk_writefinish)responserO   
chunk_sizer>   Zbytes_so_farZ
total_sizeprogresschunkr   r   r   rG      s    
rG   c                 C   s&   | |  |t|  td dS )z1Write a chunk to file and update the progress barr   N)rK   Zincrease_progressrZ   r%   r/   )r`   rO   r_   r   r   r   r[      s    
r[   c                 C   s   ddddddg}dddd	d	d	g}| dkrxt tt| d
t|d }t| d
|  }|| }|| }d| }|||S | dkrdS dS )z,Turn number of bytes into human-readable strrV   kBMBGBTBPBr   r   r   i   z{0:.%sf} {1}z0 bytesz1 byte)minrB   r   rZ   floatformat)numZunitsZdecimalsexponentZquotientunitZnum_decimalsformat_stringr   r   r   rE      s    rE   )NFT)T)rU   r   )r   mathr   r   r   r   rI   r!   r%   r   r   r   r   r   r.   r	   r   r9   r-   rG   r[   rE   r   r   r   r   <module>   s   
c
;
%