ó
‡ˆ\c           @   sI  d  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 y d d l	 m
 Z
 Wn! e k
 r‡ d d l m
 Z
 n Xy d d l Z Wn e k
 r± e Z n Xy d d l Z Wn e k
 rÛ e Z n Xy) d d l Z e rd d l m Z n  Wn e k
 re Z n Xd Z i  a d	 Z d
 Z d Z d Z d Z d Z d Z e d „ Z d f  d „  ƒ  YZ d e f d „  ƒ  YZ d e f d „  ƒ  YZ  d e  f d „  ƒ  YZ! d e f d „  ƒ  YZ" d Z# d Z$ d Z% d Z& d Z' d e j( f d  „  ƒ  YZ) d! e f d" „  ƒ  YZ* d# e) f d$ „  ƒ  YZ+ d% e f d& „  ƒ  YZ, d S('   s/   Classes and functions for managing compressors.iÿÿÿÿN(   t   LooseVersioni   (   t   _basestringt   PY3_OR_LATER(   t   RLock(   t   LZ4FrameFilesM   LZ4 is not installed. Install it with pip: https://python-lz4.readthedocs.io/t   ZFt   xs   ‹t   BZs   ý7zXZs   ] s   "Mc         C   sý   t  |  t ƒ s' t d j |  ƒ ƒ ‚ n  t  | t ƒ sN t d j | ƒ ƒ ‚ n  | j d	 k	 rÄ t | j d ƒ s© t | j d ƒ s© t | j d ƒ s© t | j d ƒ rÄ t d j | j ƒ ƒ ‚ n  |  t k rï | rï t d j |  ƒ ƒ ‚ n  | t |  <d	 S(
   sÍ   Register a new compressor.

    Parameters
    -----------
    compressor_name: str.
        The name of the compressor.
    compressor: CompressorWrapper
        An instance of a 'CompressorWrapper'.
    s/   Compressor name should be a string, '{}' given.sH   Compressor should implement the CompressorWrapper interface, '{}' given.t   readt   writet   seekt   tells^   Compressor 'fileobj_factory' attribute should implement the file object interface, '{}' given.s#   Compressor '{}' already registered.N(	   t
   isinstanceR   t
   ValueErrort   formatt   CompressorWrappert   fileobj_factoryt   Nonet   hasattrt   _COMPRESSORS(   t   compressor_namet
   compressort   force(    (    sB   lib/python2.7/site-packages/sklearn/externals/joblib/compressor.pyt   register_compressor0   s"    				R   c           B   s2   e  Z d  Z d d d „ Z d d „ Z d „  Z RS(   sö  A wrapper around a compressor file object.

    Attributes
    ----------
    obj: a file-like object
        The object must implement the buffer interface and will be used
        internally to compress/decompress the data.
    prefix: bytestring
        A bytestring corresponding to the magic number that identifies the
        file format associated to the compressor.
    extention: str
        The file extension used to automatically select this compressor during
        a dump to a file.
    t    c         C   s   | |  _  | |  _ | |  _ d  S(   N(   R   t   prefixt	   extension(   t   selft   objR   R   (    (    sB   lib/python2.7/site-packages/sklearn/externals/joblib/compressor.pyt   __init__d   s    		c         C   s6   | d k r |  j | d ƒ S|  j | d d | ƒSd S(   s0   Returns an instance of a compressor file object.t   wbt   compresslevelN(   R   R   (   R   t   fileobjR   (    (    sB   lib/python2.7/site-packages/sklearn/externals/joblib/compressor.pyt   compressor_filei   s    c         C   s   |  j  | d ƒ S(   s2   Returns an instance of a decompressor file object.t   rb(   R   (   R   R    (    (    sB   lib/python2.7/site-packages/sklearn/externals/joblib/compressor.pyt   decompressor_fileq   s    N(   t   __name__t
   __module__t   __doc__R   R   R!   R#   (    (    (    sB   lib/python2.7/site-packages/sklearn/externals/joblib/compressor.pyR   T   s   t   BZ2CompressorWrapperc           B   s;   e  Z e Z d  Z d „  Z d „  Z d d „ Z d „  Z	 RS(   s   .bz2c         C   s(   t  d  k	 r t  j |  _ n	 d  |  _ d  S(   N(   t   bz2R   t   BZ2FileR   (   R   (    (    sB   lib/python2.7/site-packages/sklearn/externals/joblib/compressor.pyR   {   s    c         C   s   t  d  k r t d ƒ ‚ n  d  S(   Ns;   bz2 module is not compiled on your python standard library.(   R(   R   R   (   R   (    (    sB   lib/python2.7/site-packages/sklearn/externals/joblib/compressor.pyt   _check_versions   s    c         C   s@   |  j  ƒ  | d k r& |  j | d ƒ S|  j | d d | ƒSd S(   s0   Returns an instance of a compressor file object.R   R   N(   R*   R   R   (   R   R    R   (    (    sB   lib/python2.7/site-packages/sklearn/externals/joblib/compressor.pyR!   †   s
    
c         C   s>   |  j  ƒ  t r% |  j | d ƒ } n |  j | j d ƒ } | S(   s2   Returns an instance of a decompressor file object.R"   (   R*   R   R   t   name(   R   R    (    (    sB   lib/python2.7/site-packages/sklearn/externals/joblib/compressor.pyR#      s
    
N(
   R$   R%   t   _BZ2_PREFIXR   R   R   R*   R   R!   R#   (    (    (    sB   lib/python2.7/site-packages/sklearn/externals/joblib/compressor.pyR'   v   s   			t   LZMACompressorWrapperc           B   s2   e  Z e Z d  Z d „  Z d d „ Z d „  Z RS(   s   .lzmac         C   s(   t  d  k	 r t  j |  _ n	 d  |  _ d  S(   N(   t   lzmaR   t   LZMAFileR   (   R   (    (    sB   lib/python2.7/site-packages/sklearn/externals/joblib/compressor.pyR       s    c         C   sH   | d k r% |  j | d d t j ƒS|  j | d d t j d | ƒSd S(   s0   Returns an instance of a compressor file object.R   R   t   presetN(   R   R   R.   t   FORMAT_ALONE(   R   R    R   (    (    sB   lib/python2.7/site-packages/sklearn/externals/joblib/compressor.pyR!   ¦   s    
	c         C   sL   t  r" t d k	 r" t j | d ƒ St d j t j d t j d ƒ ƒ ‚ d S(   s2   Returns an instance of a decompressor file object.R"   sF   Lzma decompression is not supported for this version of python ({}.{})i    i   N(   R   R.   R   R/   t   NotImplementedErrorR   t   syst   version_info(   R   R    (    (    sB   lib/python2.7/site-packages/sklearn/externals/joblib/compressor.pyR#   °   s
    	
N(	   R$   R%   t   _LZMA_PREFIXR   R   R   R   R!   R#   (    (    (    sB   lib/python2.7/site-packages/sklearn/externals/joblib/compressor.pyR-   ›   s
   	
t   XZCompressorWrapperc           B   s)   e  Z e Z d  Z d „  Z d d „ Z RS(   s   .xzc         C   s(   t  d  k	 r t  j |  _ n	 d  |  _ d  S(   N(   R.   R   R/   R   (   R   (    (    sB   lib/python2.7/site-packages/sklearn/externals/joblib/compressor.pyR   Æ   s    c         C   sH   | d k r% |  j | d d t j ƒS|  j | d d t j d | ƒSd S(   s0   Returns an instance of a compressor file object.R   t   checkR0   N(   R   R   R.   t
   CHECK_NONE(   R   R    R   (    (    sB   lib/python2.7/site-packages/sklearn/externals/joblib/compressor.pyR!   Ì   s    N(   R$   R%   t
   _XZ_PREFIXR   R   R   R   R!   (    (    (    sB   lib/python2.7/site-packages/sklearn/externals/joblib/compressor.pyR6   Á   s   	t   LZ4CompressorWrapperc           B   s;   e  Z e Z d  Z d „  Z d „  Z d d „ Z d „  Z	 RS(   s   .lz4c         C   s+   t  r t d  k	 r t |  _ n	 d  |  _ d  S(   N(   R   t   lz4R   R   R   (   R   (    (    sB   lib/python2.7/site-packages/sklearn/externals/joblib/compressor.pyR   Ú   s    c         C   sO   t  s t d ƒ ‚ n  t d  k s< t t j ƒ t d ƒ k  rK t t ƒ ‚ n  d  S(   Ns0   lz4 compression is only available with python3+.s   0.19(   R   R   R;   R   R    t   __version__t   LZ4_NOT_INSTALLED_ERROR(   R   (    (    sB   lib/python2.7/site-packages/sklearn/externals/joblib/compressor.pyR*   à   s    'c         C   s@   |  j  ƒ  | d k r& |  j | d ƒ S|  j | d d | ƒSd S(   s0   Returns an instance of a compressor file object.R   t   compression_levelN(   R*   R   R   (   R   R    R   (    (    sB   lib/python2.7/site-packages/sklearn/externals/joblib/compressor.pyR!   è   s
    
c         C   s   |  j  ƒ  |  j | d ƒ S(   s2   Returns an instance of a decompressor file object.R"   (   R*   R   (   R   R    (    (    sB   lib/python2.7/site-packages/sklearn/externals/joblib/compressor.pyR#   ñ   s    
N(
   R$   R%   t   _LZ4_PREFIXR   R   R   R*   R   R!   R#   (    (    (    sB   lib/python2.7/site-packages/sklearn/externals/joblib/compressor.pyR:   Õ   s   			i    i   i   i    t   BinaryZlibFilec           B   sã   e  Z d  Z e j Z d d d „ Z d „  Z e d „  ƒ Z	 d „  Z
 d „  Z d „  Z d	 „  Z d
 „  Z d „  Z d „  Z d „  Z d „  Z e d „ Z e d „ Z d d „ Z d „  Z d „  Z d „  Z d d „ Z d „  Z RS(   sŽ  A file object providing transparent zlib (de)compression.

    A BinaryZlibFile can act as a wrapper for an existing file object, or refer
    directly to a named file on disk.

    Note that BinaryZlibFile provides only a *binary* file interface: data read
    is returned as bytes, and data to be written should be given as bytes.

    This object is an adaptation of the BZ2File object and is compatible with
    versions of python >= 2.7.

    If filename is a str or bytes object, it gives the name
    of the file to be opened. Otherwise, it should be a file object,
    which will be used to read or write the compressed data.

    mode can be 'rb' for reading (default) or 'wb' for (over)writing

    If mode is 'wb', compresslevel can be a number between 1
    and 9 specifying the level of compression: 1 produces the least
    compression, and 9 produces the most compression. 3 is the default.
    R"   i   c         C   s…  t  ƒ  |  _ d  |  _ t |  _ t |  _ d |  _ d |  _	 | |  _
 t | t ƒ so d | k oi d k n r‡ t d j | ƒ ƒ ‚ n  | d k rÆ t |  _ t j |  j ƒ |  _ d |  _ d |  _ nU | d k rt |  _ t j |  j
 t j |  j t j d ƒ |  _ n t d	 | f ƒ ‚ t | t ƒ rKt j | | ƒ |  _ t |  _ n6 t | d
 ƒ sit | d ƒ ru| |  _ n t  d ƒ ‚ d  S(   Ni    iÿÿÿÿi   i	   sS   'compresslevel' must be an integer between 1 and 9. You provided 'compresslevel={}'R"   R   R   s   Invalid mode: %rR   R	   s1   filename must be a str or bytes object, or a file(!   R   t   _lockR   t   _fpt   Falset   _closefpt   _MODE_CLOSEDt   _modet   _post   _sizeR   R   t   intR   R   t
   _MODE_READt   zlibt   decompressobjt   wbitst   _decompressort   _buffert   _buffer_offsett   _MODE_WRITEt   compressobjt   DEFLATEDt   DEF_MEM_LEVELt   _compressorR   t   iot   opent   TrueR   t	   TypeError(   R   t   filenamet   modeR   (    (    sB   lib/python2.7/site-packages/sklearn/externals/joblib/compressor.pyR     s6    						-				c         C   sÒ   |  j  Ã |  j t k r d SzY |  j t t f k rA d |  _ n4 |  j t k ru |  j j	 |  j
 j ƒ  ƒ d |  _
 n  Wd z |  j r• |  j j ƒ  n  Wd d |  _ t |  _ t |  _ d |  _ d |  _ XXWd QXd S(   s«   Flush and close the file.

        May be called more than once without error. Once the file is
        closed, any other operation on it will raise a ValueError.
        NR   i    (   RA   RF   RE   RJ   t   _MODE_READ_EOFR   RN   RQ   RB   R	   RU   t   flushRD   t   closeRC   RO   RP   (   R   (    (    sB   lib/python2.7/site-packages/sklearn/externals/joblib/compressor.pyR^   ?  s"    
					c         C   s   |  j  t k S(   s   True if this file is closed.(   RF   RE   (   R   (    (    sB   lib/python2.7/site-packages/sklearn/externals/joblib/compressor.pyt   closedY  s    c         C   s   |  j  ƒ  |  j j ƒ  S(   s3   Return the file descriptor for the underlying file.(   t   _check_not_closedRB   t   fileno(   R   (    (    sB   lib/python2.7/site-packages/sklearn/externals/joblib/compressor.pyRa   ^  s    
c         C   s   |  j  ƒ  o |  j j ƒ  S(   s)   Return whether the file supports seeking.(   t   readableRB   t   seekable(   R   (    (    sB   lib/python2.7/site-packages/sklearn/externals/joblib/compressor.pyRc   c  s    c         C   s   |  j  ƒ  |  j t t f k S(   s/   Return whether the file was opened for reading.(   R`   RF   RJ   R\   (   R   (    (    sB   lib/python2.7/site-packages/sklearn/externals/joblib/compressor.pyRb   g  s    
c         C   s   |  j  ƒ  |  j t k S(   s/   Return whether the file was opened for writing.(   R`   RF   RQ   (   R   (    (    sB   lib/python2.7/site-packages/sklearn/externals/joblib/compressor.pyt   writablel  s    
c         C   sc   |  j  r_ t |  j d d  ƒ } d } | d  k	 rF | d j | ƒ 7} n  | d 7} t | ƒ ‚ n  d  S(   NR+   s   I/O operation on closed files    {}t   .(   R_   t   getattrRB   R   R   R   (   R   t   fnamet   msg(    (    sB   lib/python2.7/site-packages/sklearn/externals/joblib/compressor.pyR`   s  s    	
c         C   s5   |  j  t t f k r1 |  j ƒ  t j d ƒ ‚ n  d  S(   Ns   File not open for reading(   RF   RJ   R\   R`   RV   t   UnsupportedOperation(   R   (    (    sB   lib/python2.7/site-packages/sklearn/externals/joblib/compressor.pyt   _check_can_read|  s    
c         C   s/   |  j  t k r+ |  j ƒ  t j d ƒ ‚ n  d  S(   Ns   File not open for writing(   RF   RQ   R`   RV   Ri   (   R   (    (    sB   lib/python2.7/site-packages/sklearn/externals/joblib/compressor.pyt   _check_can_write  s    
c         C   sV   |  j  t t f k r1 |  j ƒ  t j d ƒ ‚ n  |  j j ƒ  sR t j d ƒ ‚ n  d  S(   Ns3   Seeking is only supported on files open for readings3   The underlying file object does not support seeking(   RF   RJ   R\   R`   RV   Ri   RB   Rc   (   R   (    (    sB   lib/python2.7/site-packages/sklearn/externals/joblib/compressor.pyt   _check_can_seek†  s
    
c         C   s¯   |  j  t k r t Sx• |  j t |  j ƒ k rª y1 |  j j pL |  j j	 t
 ƒ } | s^ t ‚ n  Wn' t k
 rˆ t |  _  |  j |  _ t SX|  j j | ƒ |  _ d |  _ q Wt S(   Ni    (   RF   R\   RC   RP   t   lenRO   RN   t   unused_dataRB   R   t   _BUFFER_SIZEt   EOFErrorRG   RH   t
   decompressRX   (   R   t   rawblock(    (    sB   lib/python2.7/site-packages/sklearn/externals/joblib/compressor.pyt   _fill_buffer  s    	c         C   s†   |  j  |  j |  _  d |  _ g  } xJ |  j ƒ  rn | rJ | j |  j  ƒ n  |  j t |  j  ƒ 7_ d |  _  q% W| r‚ d j | ƒ Sd  S(   Ni    R   (   RO   RP   Rs   t   appendRG   Rm   t   join(   R   t   return_datat   blocks(    (    sB   lib/python2.7/site-packages/sklearn/externals/joblib/compressor.pyt	   _read_all§  s    	c         C   s4  |  j  | } | t |  j ƒ k ra |  j |  j  | !} | |  _  |  j t | ƒ 7_ | r] | Sd  S|  j |  j  |  _ d |  _  g  } x— | d k r|  j ƒ  r| t |  j ƒ k  rÌ |  j |  } | |  _  n |  j } d |  _ | rô | j | ƒ n  |  j t | ƒ 7_ | t | ƒ 8} q† W| r0d j | ƒ Sd  S(   Ni    R   (   RP   Rm   RO   RG   R   Rs   Rt   Ru   (   R   t   n_bytesRv   t   endt   dataRw   (    (    sB   lib/python2.7/site-packages/sklearn/externals/joblib/compressor.pyt   _read_block·  s*    				iÿÿÿÿc         C   sQ   |  j  B |  j ƒ  | d k r$ d S| d k  r: |  j ƒ  S|  j | ƒ SWd QXd S(   s±   Read up to size uncompressed bytes from the file.

        If size is negative or omitted, read until EOF is reached.
        Returns b'' if the file is already at EOF.
        i    R   N(   RA   Rj   Rx   R|   (   R   t   size(    (    sB   lib/python2.7/site-packages/sklearn/externals/joblib/compressor.pyR   Ó  s    


c         C   s'   |  j   t j j |  | ƒ SWd QXd S(   s_   Read up to len(b) bytes into b.

        Returns the number of bytes read (0 for EOF).
        N(   RA   RV   t   BufferedIOBaset   readinto(   R   t   b(    (    sB   lib/python2.7/site-packages/sklearn/externals/joblib/compressor.pyR   â  s    
c         C   s}   |  j  n |  j ƒ  t | t ƒ r2 | j ƒ  } n  |  j j | ƒ } |  j j | ƒ |  j	 t
 | ƒ 7_	 t
 | ƒ SWd QXd S(   sö   Write a byte string to the file.

        Returns the number of uncompressed bytes written, which is
        always len(data). Note that due to buffering, the file on disk
        may not reflect the data written until close() is called.
        N(   RA   Rk   R   t
   memoryviewt   tobytesRU   t   compressRB   R	   RG   Rm   (   R   R{   t
   compressed(    (    sB   lib/python2.7/site-packages/sklearn/externals/joblib/compressor.pyR	   ê  s    

c         C   sP   |  j  j d d ƒ t |  _ d |  _ t j |  j ƒ |  _ d |  _	 d |  _
 d  S(   Ni    R   (   RB   R
   RJ   RF   RG   RK   RL   RM   RN   RO   RP   (   R   (    (    sB   lib/python2.7/site-packages/sklearn/externals/joblib/compressor.pyt   _rewindý  s    			i    c         C   sÝ   |  j  Î |  j ƒ  | d k r# nm | d k r? |  j | } nQ | d k r} |  j d k  rm |  j d t ƒ n  |  j | } n t d | f ƒ ‚ | |  j k  r¬ |  j ƒ  n | |  j 8} |  j | d t ƒ|  j SWd QXd S(   sç  Change the file position.

        The new position is specified by offset, relative to the
        position indicated by whence. Values for whence are:

            0: start of stream (default); offset must not be negative
            1: current stream position
            2: end of stream; offset must not be positive

        Returns the new file position.

        Note that seeking is emulated, so depending on the parameters,
        this operation may be extremely slow.
        i    i   i   Rv   s   Invalid value for whence: %sN(	   RA   Rl   RG   RH   Rx   RC   R   R…   R|   (   R   t   offsett   whence(    (    sB   lib/python2.7/site-packages/sklearn/externals/joblib/compressor.pyR
     s     

c         C   s%   |  j   |  j ƒ  |  j SWd QXd S(   s!   Return the current file position.N(   RA   R`   RG   (   R   (    (    sB   lib/python2.7/site-packages/sklearn/externals/joblib/compressor.pyR   /  s    

(   R$   R%   R&   RK   t	   MAX_WBITSRM   R   R^   t   propertyR_   Ra   Rc   Rb   Rd   R`   Rj   Rk   Rl   Rs   RX   Rx   R|   R   R   R	   R…   R
   R   (    (    (    sB   lib/python2.7/site-packages/sklearn/externals/joblib/compressor.pyR@      s,   	&										
				*t   ZlibCompressorWrapperc           B   s   e  Z d  „  Z RS(   c         C   s#   t  j |  d t d t d d ƒd  S(   NR   R   R   s   .z(   R   R   R@   t   _ZLIB_PREFIX(   R   (    (    sB   lib/python2.7/site-packages/sklearn/externals/joblib/compressor.pyR   8  s    (   R$   R%   R   (    (    (    sB   lib/python2.7/site-packages/sklearn/externals/joblib/compressor.pyRŠ   6  s   t   BinaryGzipFilec           B   s   e  Z d  Z d Z RS(   s  A file object providing transparent gzip (de)compression.

    If filename is a str or bytes object, it gives the name
    of the file to be opened. Otherwise, it should be a file object,
    which will be used to read or write the compressed data.

    mode can be 'rb' for reading (default) or 'wb' for (over)writing

    If mode is 'wb', compresslevel can be a number between 1
    and 9 specifying the level of compression: 1 produces the least
    compression, and 9 produces the most compression. 3 is the default.
    i   (   R$   R%   R&   RM   (    (    (    sB   lib/python2.7/site-packages/sklearn/externals/joblib/compressor.pyRŒ   =  s   t   GzipCompressorWrapperc           B   s   e  Z d  „  Z RS(   c         C   s#   t  j |  d t d t d d ƒd  S(   NR   R   R   s   .gz(   R   R   RŒ   t   _GZIP_PREFIX(   R   (    (    sB   lib/python2.7/site-packages/sklearn/externals/joblib/compressor.pyR   P  s    (   R$   R%   R   (    (    (    sB   lib/python2.7/site-packages/sklearn/externals/joblib/compressor.pyR   N  s   (-   R&   R3   RV   RK   t   distutils.versionR    t   _compatR   R   t	   threadingR   t   ImportErrort   dummy_threadingR(   R   R.   R;   t	   lz4.frameR   R=   R   t   _ZFILE_PREFIXR‹   RŽ   R,   R9   R5   R?   RC   R   R   R'   R-   R6   R:   RE   RJ   R\   RQ   Ro   R~   R@   RŠ   RŒ   R   (    (    (    sB   lib/python2.7/site-packages/sklearn/externals/joblib/compressor.pyt   <module>   s`   


#"%&$ÿ 7