ó
¾÷Xc           @@ s  d  Z  d d l m Z d d l m 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 e j d d k rÓ d d d
 „ Z n d d l
 m Z e d d d „ Z d „  Z d S(   s(   Utilities for file download and caching.i    (   t   absolute_import(   t   print_functionN(   t   urlopen(   t   URLError(   t	   HTTPErrori   (   t   Progbarc         C@ sd   d d d „ } t |  | ƒ } t | d ƒ 1 } x' | | d | ƒD] } | j | ƒ qC WWd QXd S(   sâ  Replacement for `urlretrive` for Python 2.

        Under Python 2, `urlretrieve` relies on `FancyURLopener` from legacy
        `urllib` module, known to have issues with proxy management.

        # Arguments
            url: url to retrieve.
            filename: where to store the retrieved data locally.
            reporthook: a hook function that will be called once
                on establishment of the network connection and once
                after each block read thereafter.
                The hook will be passed three arguments;
                a count of blocks transferred so far,
                a block size in bytes, and the total size of the file.
            data: `data` argument passed to `urlopen`.
        i    c         s@ sˆ   |  j  ƒ  j d ƒ j ƒ  } t | ƒ } d } xT |  j | ƒ } | d 7} | sc | | | | ƒ Pn  | r| | | | | ƒ n  | Vq0 d  S(   Ns   Content-Lengthi    i   (   t   infot   gett   stript   intt   read(   t   responset
   chunk_sizet
   reporthookt
   total_sizet   countt   chunk(    (    s5   /tmp/pip-build-isqEY4/keras/keras/utils/data_utils.pyt
   chunk_read$   s    
t   wbR   N(   t   NoneR   t   opent   write(   t   urlt   filenameR   t   dataR   R   t   fdR   (    (    s5   /tmp/pip-build-isqEY4/keras/keras/utils/data_utils.pyt   urlretrieve   s
    (   R   t   datasetsc         C@ sÚ  t  j j t  j j d d ƒ ƒ } t  j | t  j ƒ sN t  j j d d ƒ } n  t  j j | | ƒ } t  j j | ƒ s… t  j | ƒ n  | r­ t  j j | |  ƒ } | d } n t  j j | |  ƒ } t }	 t  j j | ƒ r| d k	 rt
 | | ƒ st d ƒ t }	 qqn t }	 |	 rt d | ƒ d }
 d d „ } d } y‘ y# t | | t j | d	 |
 ƒƒ Wng t k
 r›} t | j | | j | j ƒ ƒ ‚ n4 t k
 rÎ} t | j | | j | j ƒ ƒ ‚ n XWn> t t f k
 r} t  j j | ƒ r
t  j | ƒ n  ‚  n Xd }
 n  | rÖt  j j | ƒ sÒt d
 ƒ t j | d ƒ } y | j d | ƒ Wn` t t f k
 rÄ} t  j j | ƒ r¾t  j j | ƒ r®t  j | ƒ q¾t j | ƒ n  ‚  n X| j  ƒ  n  | S| S(   sñ  Downloads a file from a URL if it not already in the cache.

    Passing the MD5 hash will verify the file after download
    as well as if it is already present in the cache.

    # Arguments
        fname: name of the file
        origin: original URL of the file
        untar: boolean, whether the file should be decompressed
        md5_hash: MD5 hash of the file for verification
        cache_subdir: directory being used as the cache

    # Returns
        Path to the downloaded file
    t   ~s   .kerass   /tmps   .tar.gzsB   A local file was found, but it seems to be incomplete or outdated.s   Downloading data fromc         S@ s0   | d  k r t | ƒ } n | j |  | ƒ d  S(   N(   R   R   t   update(   R   t
   block_sizeR   t   progbar(    (    s5   /tmp/pip-build-isqEY4/keras/keras/utils/data_utils.pyt   dl_progressg   s    s!   URL fetch failure on {}: {} -- {}R   s   Untaring file...s   r:gzt   pathN(!   t   osR!   t
   expandusert   joint   accesst   W_OKt   existst   makedirst   FalseR   t   validate_filet   printt   TrueR   t	   functoolst   partialR   t	   Exceptiont   formatt   errnot   reasonR   t   codet   msgt   KeyboardInterruptt   removet   tarfileR   t
   extractallt   isfilet   shutilt   rmtreet   close(   t   fnamet   origint   untart   md5_hasht   cache_subdirt   datadir_baset   datadirt   untar_fpatht   fpatht   downloadR   R    t	   error_msgt   et   tfile(    (    s5   /tmp/pip-build-isqEY4/keras/keras/utils/data_utils.pyt   get_file:   sd    !
	$)	
c         C@ sg   t  j ƒ  } t |  d ƒ   } | j ƒ  } | j | ƒ Wd QXt | j ƒ  ƒ t | ƒ k r_ t St Sd S(   sÒ   Validates a file against a MD5 hash.

    # Arguments
        fpath: path to the file being validated
        md5_hash: the MD5 hash being validated against

    # Returns
        Whether the file is valid
    t   rbN(	   t   hashlibt   md5R   R
   R   t   strt	   hexdigestR,   R)   (   RE   R@   t   hashert   ft   buf(    (    s5   /tmp/pip-build-isqEY4/keras/keras/utils/data_utils.pyR*      s    
(   t   __doc__t
   __future__R    R   R-   R7   R"   t   sysR:   RL   t   six.moves.urllib.requestR   t   six.moves.urllib.errorR   R   t   utils.generic_utilsR   t   version_infoR   R   R)   RJ   R*   (    (    (    s5   /tmp/pip-build-isqEY4/keras/keras/utils/data_utils.pyt   <module>   s$   $T