o
    l¨Êhô   ã                   @   s  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r7ejZejZeZnejZejZzddlZW n eyN   dZY nw zddlZW n ey`   dZY nw dZd	d
„ Zdd„ Zdd„ Zdd„ Zdd„ Zejddd„ƒZddd„ZdZddd„Z dS )zFUtilities for fast persistence of big data, with optional compression.é    N)Úclosingé   )ÚPY3_OR_LATERÚPY27)Ú_ZFILE_PREFIX)Ú_COMPRESSORSi   c                 C   s&   t rt| d| ƒ} t| tjƒS t| tƒS )z=Check if fileobj is a raw file object, e.g created with open.Úraw)r   ÚgetattrÚ
isinstanceÚioÚFileIOÚfile)Úfileobj© r   úK/var/www/html/env/lib/python3.10/site-packages/joblib/numpy_pickle_utils.pyÚ_is_raw_file+   s   
r   c                  C   s(   dd„ t  ¡ D ƒ} | ttƒg7 } t| ƒS )Nc                 S   s   g | ]}t |jƒ‘qS r   )ÚlenÚprefix)Ú.0Ú
compressorr   r   r   Ú
<listcomp>6   s    z)_get_prefixes_max_len.<locals>.<listcomp>)r   Úvaluesr   r   Úmax)Úprefixesr   r   r   Ú_get_prefixes_max_len4   s   r   c                 C   sh   t ƒ }t| dƒr|  |¡}n
|  |¡}|  d¡ | t¡rdS t ¡ D ]\}}| |j	¡r1|  S q#dS )zÊReturn the compressor matching fileobj.

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

    Returns
    -------
    str in {'zlib', 'gzip', 'bz2', 'lzma', 'xz', 'compat', 'not-compressed'}
    Úpeekr   Úcompatúnot-compressed)
r   Úhasattrr   ÚreadÚseekÚ
startswithr   r   Úitemsr   )r   Úmax_prefix_lenÚfirst_bytesÚnamer   r   r   r   Ú_detect_compressor=   s   



ÿr&   c                 C   s*   t rtdurt| tjƒr| S tj| td�S )z0Return a buffered version of a read file object.N©Úbuffer_size)r   Úbz2r
   ÚBZ2Filer   ÚBufferedReaderÚ_IO_BUFFER_SIZE©Úfobjr   r   r   Ú_buffered_read_file]   s   r/   c                 C   s.   t rtdurt| tjƒrt| ƒS tj| td�S )z1Return a buffered version of a write file object.Nr'   )r   r)   r
   r*   r   r   ÚBufferedWriterr,   r-   r   r   r   Ú_buffered_write_fileg   s   r1   c                 c   sÀ   � t | ƒ}|dkrtjd| tdd� |V  dS |tv r)t| }| | ¡}t|ƒ} |dur[t| tj	ƒr>tjdt
ƒ  dd� n|dkrMtjdt
ƒ  dd� nt| ƒs[tjd	t
ƒ  dd� | V  dS )
a-  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   zjThe file '%s' has been generated with a joblib version less than 0.10. Please regenerate this pickle file.é   )Ú
stacklevelNzuIn memory persistence is not compatible with mmap_mode "%(mmap_mode)s" flag passed. mmap_mode option will be ignored.r   ztmmap_mode "%(mmap_mode)s" is not compatible with compressed file %(filename)s. "%(mmap_mode)s" flag will be ignored.zP"%(fileobj)r" is not a raw file, mmap_mode "%(mmap_mode)s" flag will be ignored.)r&   ÚwarningsÚwarnÚDeprecationWarningr   Údecompressor_filer/   r
   r   ÚBytesIOÚlocalsr   )r   ÚfilenameÚ	mmap_moder   Úcompressor_wrapperÚinstr   r   r   Ú_read_fileobjects   sB   €þý

ýýýýþþ
r>   ©Úzlibé   c                 C   sP   |d }|d }|t  ¡ v rt | j| |d�}t|ƒS t d j| |d�}t|ƒS )z6Return the right compressor file object in write mode.r   r   )Úcompresslevelr@   )r   ÚkeysÚcompressor_filer1   )r:   ÚcompressÚcompressmethodrB   Úfile_instancer   r   r   Ú_write_fileobject¹   s   ÿÿrH   i   úran out of datac                 C   s„   t ƒ }	 z|  |t|ƒ ¡}||7 }t|ƒdkst|ƒ|kr W nW n
 tjy+   Y nw qt|ƒ|kr@d}t|||t|ƒf ƒ‚|S )a  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.

    Tr   z)EOF: reading %s, expected %d bytes got %d)Úbytesr   r   r   ÚBlockingIOErrorÚ
ValueError)ÚfpÚsizeÚerror_templateÚdataÚrÚmsgr   r   r   Ú_read_bytesÎ   s    ÿÿ÷rS   )N)r?   )rI   )!Ú__doc__Úpickler   r4   Ú
contextlibr   Ú_compatr   r   r   r   r   Ú
_UnpicklerÚ	UnpicklerÚ_PicklerÚPicklerÚrangeÚxrangeÚnumpyÚnpÚImportErrorr)   r,   r   r   r&   r/   r1   Úcontextmanagerr>   rH   ÚBUFFER_SIZErS   r   r   r   r   Ú<module>   sH    ÿÿ		 

E