ó
‡ˆ\c           @   sm  d  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 m Z d d l	 m
 Z
 d d l	 m Z e r e j Z e j Z e Z n e j Z e j Z y d d l Z Wn e k
 rØ d Z n Xy d d l Z Wn e k
 rd Z n Xd Z d
 „  Z d „  Z d „  Z d „  Z d „  Z e j d d „ ƒ Z d d „ Z d Z  d d „ Z! d S(   sF   Utilities for fast persistence of big data, with optional compression.iÿÿÿÿN(   t   closingi   (   t   PY3_OR_LATERt   PY27(   t   _ZFILE_PREFIX(   t   _COMPRESSORSi   i   c         C   s9   t  r( t |  d |  ƒ }  t |  t j ƒ St |  t ƒ Sd S(   s=   Check if fileobj is a raw file object, e.g created with open.t   rawN(   R   t   getattrt
   isinstancet   iot   FileIOt   file(   t   fileobj(    (    sJ   lib/python2.7/site-packages/sklearn/externals/joblib/numpy_pickle_utils.pyt   _is_raw_file+   s    c          C   sE   g  t  j ƒ  D] }  t |  j ƒ ^ q } | t t ƒ g 7} t | ƒ S(   N(   R   t   valuest   lent   prefixR   t   max(   t
   compressort   prefixes(    (    sJ   lib/python2.7/site-packages/sklearn/externals/joblib/numpy_pickle_utils.pyt   _get_prefixes_max_len4   s    (c         C   s   t  ƒ  } t |  d ƒ r* |  j | ƒ } n |  j | ƒ } |  j d ƒ | j t ƒ rY d Sx0 t j ƒ  D]" \ } } | j | j	 ƒ rf | Sqf Wd S(   sÊ   Return the compressor matching fileobj.

    Parameters
    ----------
    fileobj: file object

    Returns
    -------
    str in {'zlib', 'gzip', 'bz2', 'lzma', 'xz', 'compat', 'not-compressed'}
    t   peeki    t   compats   not-compressed(
   R   t   hasattrR   t   readt   seekt
   startswithR   R   t   itemsR   (   R   t   max_prefix_lent   first_bytest   nameR   (    (    sJ   lib/python2.7/site-packages/sklearn/externals/joblib/numpy_pickle_utils.pyt   _detect_compressor=   s    	c         C   s?   t  r( t d k	 r( t |  t j ƒ r( |  St j |  d t ƒSd S(   s0   Return a buffered version of a read file object.t   buffer_sizeN(   R   t   bz2t   NoneR   t   BZ2FileR   t   BufferedReadert   _IO_BUFFER_SIZE(   t   fobj(    (    sJ   lib/python2.7/site-packages/sklearn/externals/joblib/numpy_pickle_utils.pyt   _buffered_read_file]   s    $c         C   sE   t  r. t d k	 r. t |  t j ƒ r. t |  ƒ St j |  d t ƒSd S(   s1   Return a buffered version of a write file object.R   N(	   R   R    R!   R   R"   R    R   t   BufferedWriterR$   (   R%   (    (    sJ   lib/python2.7/site-packages/sklearn/externals/joblib/numpy_pickle_utils.pyt   _buffered_write_fileg   s    $
c         c   s  t  |  ƒ } | d k r: t j d | t d d ƒ| VnÉ | t k rn t | } | j |  ƒ } t | ƒ }  n  | d	 k	 rþ t |  t	 j
 ƒ r© t j d t ƒ  d d ƒqþ | d k rÒ t j d t ƒ  d d ƒqþ t |  ƒ sþ t j d t ƒ  d d ƒqþ n  |  Vd	 S(
   s-  Utility function opening the right fileobject from a filename.

    The magic number is used to choose between the type of file object to open:
    * regular file object (default)
    * zlib file object
    * gzip file object
    * bz2 file object
    * lzma file object (for xz and lzma compressor)

    Parameters
    ----------
    fileobj: file object
    compressor: str in {'zlib', 'gzip', 'bz2', 'lzma', 'xz', 'compat',
                        'not-compressed'}
    filename: str
        filename path corresponding to the fileobj parameter.
    mmap_mode: str
        memory map mode that should be used to open the pickle file. This
        parameter is useful to verify that the user is not trying to one with
        compression. Default: None.

    Returns
    -------
        a file like object

    R   sj   The file '%s' has been generated with a joblib version less than 0.10. Please regenerate this pickle file.t
   stackleveli   su   In memory persistence is not compatible with mmap_mode "%(mmap_mode)s" flag passed. mmap_mode option will be ignored.s   not-compressedst   mmap_mode "%(mmap_mode)s" is not compatible with compressed file %(filename)s. "%(mmap_mode)s" flag will be ignored.sP   "%(fileobj)r" is not a raw file, mmap_mode "%(mmap_mode)s" flag will be ignored.N(   R   t   warningst   warnt   DeprecationWarningR   t   decompressor_fileR&   R!   R   R   t   BytesIOt   localsR   (   R   t   filenamet	   mmap_modeR   t   compressor_wrappert   inst(    (    sJ   lib/python2.7/site-packages/sklearn/externals/joblib/numpy_pickle_utils.pyt   _read_fileobjects   s*    	
			t   zlibi   c         C   sp   | d } | d } | t  j ƒ  k rI t  | j |  d | ƒ} t | ƒ St  d j |  d | ƒ} t | ƒ Sd S(   s6   Return the right compressor file object in write mode.i    i   t   compresslevelR5   N(   R   t   keyst   compressor_fileR(   (   R0   t   compresst   compressmethodR6   t   file_instance(    (    sJ   lib/python2.7/site-packages/sklearn/externals/joblib/numpy_pickle_utils.pyt   _write_fileobject¹   s    




i   s   ran out of datac         C   s¾   t  ƒ  } xp t r{ yO |  j | t | ƒ ƒ } | | 7} t | ƒ d k s\ t | ƒ | k r` Pn  Wq t j k
 rw q Xq Wt | ƒ | k r¶ d } t | | | t | ƒ f ƒ ‚ n | Sd S(   s  Read from file-like object until size bytes are read.

    Raises ValueError if not EOF is encountered before size bytes are read.
    Non-blocking objects only supported if they derive from io objects.

    Required as e.g. ZipExtFile in python 2.6 can return less data than
    requested.

    This function was taken from numpy/lib/format.py in version 1.10.2.

    Parameters
    ----------
    fp: file-like object
    size: int
    error_template: str

    Returns
    -------
    a bytes object
        The data read in bytes.

    i    s)   EOF: reading %s, expected %d bytes got %dN(   t   bytest   TrueR   R   R   t   BlockingIOErrort
   ValueError(   t   fpt   sizet   error_templatet   datat   rt   msg(    (    sJ   lib/python2.7/site-packages/sklearn/externals/joblib/numpy_pickle_utils.pyt   _read_bytesÎ   s    		
$"i   (   R5   i   i   ("   t   __doc__t   pickleR   R*   t
   contextlibR    t   _compatR   R   R   R   R   t
   _Unpicklert	   Unpicklert   _Picklert   Picklert   ranget   xranget   numpyt   npt   ImportErrorR!   R    R$   R   R   R   R&   R(   t   contextmanagerR4   R<   t   BUFFER_SIZERG   (    (    (    sJ   lib/python2.7/site-packages/sklearn/externals/joblib/numpy_pickle_utils.pyt   <module>   sB   					

					 	
	E