ó
;c›]c           @` s  d  Z  d d l m Z m Z m 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 m Z d	 e f d
 „  ƒ  YZ d e f d „  ƒ  YZ d e e d „ Z e d „ Z d d d „ Z d „  Z d „  Z d S(   s'   Data downloading and reading functions
i    (   t   absolute_importt   print_functiont   division(   t   logN(   t   pathi   (   t   appdata_dirt   resource_dirs(   t   StdoutProgressIndicatort   string_typest   urlopent   InternetNotAllowedErrorc           B` s   e  Z d  Z RS(   sy    Plugins that need resources can just use get_remote_file(), but
    should catch this error and silently ignore it.
    (   t   __name__t
   __module__t   __doc__(    (    (    s4   lib/python2.7/site-packages/imageio/core/fetching.pyR
      s   t   NeedDownloadErrorc           B` s   e  Z d  Z RS(   s‹    Is raised when a remote file is requested that is not locally
    available, but which needs to be explicitly downloaded by the user.
    (   R   R   R   (    (    (    s4   lib/python2.7/site-packages/imageio/core/fetching.pyR      s   c         C` sç  d } | |  } t  j |  ƒ } | } | p4 t d ƒ } t ƒ  } | j d | ƒ x| D]y}	 t  j |	 | ƒ }
 t  j |
 ƒ rW | sı | rù | |	 k rù t j j | | ƒ } t  j	 t  j
 | ƒ ƒ så t j t  j t  j
 | ƒ ƒ ƒ n  t j |
 | ƒ | S|
 St | t ƒ rĞt j | d ƒ } t j t  j |
 ƒ ƒ } | | k r»| r·| |	 k r·t j j | | ƒ } t  j	 t  j
 | ƒ ƒ s£t j t  j t  j
 | ƒ ƒ ƒ n  t j |
 | ƒ | S|
 St d | ƒ PqĞqW qW Wt j d d ƒ j ƒ  d k rt d ƒ ‚ n  | st ƒ  ‚ n  t  j | | ƒ }
 t  j	 t  j
 |
 ƒ ƒ s_t j t  j t  j
 |
 ƒ ƒ ƒ n  t j d t ƒ rÒxo t d ƒ D]< } y t | |
 ƒ |
 SWq~t k
 r¹t j d ƒ q~Xq~Wt | |
 ƒ |
 Sn t | |
 ƒ |
 Sd S(   s   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.
    s7   https://github.com/imageio/imageio-binaries/raw/master/t   imageioi    s   %Y-%m-%ds   File older than %s, updating...t   IMAGEIO_NO_INTERNETt    t   1t   truet   yesse   Will not download resource from the internet because environment variable IMAGEIO_NO_INTERNET is set.t   CONTINUOUS_INTEGRATIONi   g      à?N(   R   R   R   (   t   opt   normcaseR   R   t   insertt   joint   isfilet   osR   t   isdirt   dirnamet   makedirst   abspatht   shutilt   copyt
   isinstanceR   t   timet   strptimet   gmtimet   getctimet   printt   getenvt   lowerR
   R   t   Falset   ranget   _fetch_filet   IOErrort   sleep(   t   fnamet	   directoryt   force_downloadt   autot	   _url_roott   urlt   nfnamet   given_directoryt   dirst   dirt   filenamet	   filename2t   ntimet   ftimet   i(    (    s4   lib/python2.7/site-packages/imageio/core/fetching.pyt   get_remote_file#   sd    
	"""c         C` s“  t  d t j j | ƒ ƒ | d } d } d } g  } xVt d ƒ D],} zı yÂ t |  d d ƒ} t | j d j	 ƒ  ƒ }	 t
 |	 ƒ }
 t  d | d	 |  |
 f ƒ t | d
 ƒ } t | | d | ƒ| j sÙ | j ƒ  n  t j | | ƒ | t k rt j j d | ƒ n  PWn4 t k
 rD} | j | ƒ t  d t | ƒ ƒ n XWd | d k	 rn| j sn| j ƒ  qnn  XqC Wt d t j j | ƒ ƒ ‚ d S(   sÛ  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.
    s?   Imageio: %r was not found on your computer; downloading it now.s   .parti    i   t   timeoutg      @s   Content-Lengths   Try %i. Download from %s (%s)i   t   wbt   initial_sizes   File saved as %s.
s   Error while fetching file: %s.Nsj   Unable to download %r. Perhaps there is a no internet connection? If there is, please report this problem.(   R'   R   R   t   basenamet   NoneR+   R	   t   intt   headerst   stript   _sizeof_fmtt   opent   _chunk_readt   closedt   closeR    t   movet   Truet   syst   stdoutt   writet	   Exceptiont   appendt   strR-   (   R4   t	   file_namet   print_destinationt   temp_file_namet
   local_fileRA   t   errorst   triest   remote_filet	   file_sizet   size_strt   e(    (    s4   lib/python2.7/site-packages/imageio/core/fetching.pyR,   †   s>    
 		i    c         C` sŸ   | } t  |  j d j ƒ  ƒ } | | 7} t d ƒ } | j d d | ƒ xC t r |  j | ƒ } | t | ƒ 7} | sz Pn  t | | | ƒ qK W| j	 d ƒ d S(   sö  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.
    s   Content-Lengtht   DownloadingR   t   bytest   DoneN(
   RD   RE   RF   R   t   startRM   t   readt   lent   _chunk_writet   finish(   t   responseRW   t
   chunk_sizeRA   t   bytes_so_fart
   total_sizet   progresst   chunk(    (    s4   lib/python2.7/site-packages/imageio/core/fetching.pyRI   Á   s    
	c         C` s1   | j  |  ƒ | j t |  ƒ ƒ t j d ƒ d S(   s1   Write a chunk to file and update the progress bari    N(   RP   t   increase_progressRc   R#   R.   (   Rk   RW   Rj   (    (    s4   lib/python2.7/site-packages/imageio/core/fetching.pyRd   æ   s    c         C` sº   d d d d d d g } d d d d	 d	 d	 g } |  d k r¦ t  t t |  d
 ƒ ƒ t | ƒ d ƒ } t |  ƒ d
 | } | | } | | } d | } | j | | ƒ S|  d k r¶ d Sd S(   s,   Turn number of bytes into human-readable strR_   t   kBt   MBt   GBt   TBt   PBi    i   i   i   s   {0:.%sf} {1}s   0 bytess   1 byte(   t   minRD   R   Rc   t   floatt   format(   t   numt   unitst   decimalst   exponentt   quotientt   unitt   num_decimalst   format_string(    (    s4   lib/python2.7/site-packages/imageio/core/fetching.pyRG   í   s    (


(   R   t
   __future__R    R   R   t   mathR   R   R   R   RN   R    R#   R   R   R   R   R   R	   R-   R
   R   RC   R*   RM   R>   R,   RI   Rd   RG   (    (    (    s4   lib/python2.7/site-packages/imageio/core/fetching.pyt   <module>   s    c;%	