o
    l¨Êh¨R  ã                   @   s¼  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 zddl	m
Z
 W n ey3   ddlm
Z
 Y nw zddlZW n eyE   dZY nw zddlZW n eyW   dZY nw zddlZereddlmZ W n eyq   dZY nw dZi ad	Zd
ZdZdZdZdZdZ	d(dd„ZG dd„ dƒZG dd„ deƒZG dd„ deƒZG dd„ deƒZG dd„ deƒZ dZ!dZ"dZ#dZ$dZ%G d d!„ d!ej&ƒZ'G d"d#„ d#eƒZ(G d$d%„ d%e'ƒZ)G d&d'„ d'eƒZ*dS ))z/Classes and functions for managing compressors.é    N)ÚLooseVersioné   )Ú_basestringÚPY3_OR_LATER)ÚRLock)ÚLZ4FrameFilezMLZ4 is not installed. Install it with pip: https://python-lz4.readthedocs.io/s   ZFó   xs   ‹s   BZs   ý7zXZs   ] s   "MFc                 C   s    t | tƒstd | ¡ƒ‚t |tƒstd |¡ƒ‚|jdur=t|jdƒr5t|jdƒr5t|jdƒr5t|jdƒs=td |j¡ƒ‚| tv rJ|sJtd	 | ¡ƒ‚|t| < dS )
zÍRegister a new compressor.

    Parameters
    -----------
    compressor_name: str.
        The name of the compressor.
    compressor: CompressorWrapper
        An instance of a 'CompressorWrapper'.
    z/Compressor name should be a string, '{}' given.zHCompressor should implement the CompressorWrapper interface, '{}' given.NÚreadÚwriteÚseekÚtellz^Compressor 'fileobj_factory' attribute should implement the file object interface, '{}' given.z#Compressor '{}' already registered.)Ú
isinstancer   Ú
ValueErrorÚformatÚCompressorWrapperÚfileobj_factoryÚhasattrÚ_COMPRESSORS)Úcompressor_nameÚ
compressorÚforce© r   úC/var/www/html/env/lib/python3.10/site-packages/joblib/compressor.pyÚregister_compressor0   s2   
ÿ
ÿ

ÿ
þ
ý
üþÿr   c                   @   s,   e Zd ZdZddd„Zddd„Zd	d
„ ZdS )r   aö  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.
    ó    Ú c                 C   s   || _ || _|| _d S ©N)r   ÚprefixÚ	extension)ÚselfÚobjr   r   r   r   r   Ú__init__d   s   
zCompressorWrapper.__init__Nc                 C   s$   |du r
|   |d¡S | j |d|d�S ©ú0Returns an instance of a compressor file object.NÚwb)Úcompresslevel©r   ©r   Úfileobjr%   r   r   r   Úcompressor_filei   s
   ÿz!CompressorWrapper.compressor_filec                 C   s   |   |d¡S ©ú2Returns an instance of a decompressor file object.Úrbr&   ©r   r(   r   r   r   Údecompressor_fileq   s   z#CompressorWrapper.decompressor_file)r   r   r   )Ú__name__Ú
__module__Ú__qualname__Ú__doc__r!   r)   r.   r   r   r   r   r   T   s
    

r   c                   @   ó6   e Zd ZeZdZdd„ Zdd„ Zddd„Zd	d
„ Z	dS )ÚBZ2CompressorWrapperz.bz2c                 C   ó   t d ur
t j| _d S d | _d S r   )Úbz2ÚBZ2Filer   ©r   r   r   r   r!   {   ó   
zBZ2CompressorWrapper.__init__c                 C   s   t d u rtdƒ‚d S )Nz;bz2 module is not compiled on your python standard library.)r6   r   r8   r   r   r   Ú_check_versions�   s   ÿz$BZ2CompressorWrapper._check_versionsNc                 C   ó,   |   ¡  |du r|  |d¡S | j|d|d�S r"   ©r:   r   r'   r   r   r   r)   †   ó   ÿz$BZ2CompressorWrapper.compressor_filec                 C   s.   |   ¡  tr|  |d¡}|S |  |jd¡}|S r*   )r:   r   r   Únamer-   r   r   r   r.   �   s   ÿz&BZ2CompressorWrapper.decompressor_filer   )
r/   r0   r1   Ú_BZ2_PREFIXr   r   r!   r:   r)   r.   r   r   r   r   r4   v   s    
	r4   c                   @   s.   e Zd ZeZdZdd„ Zd	dd„Zdd„ ZdS )
ÚLZMACompressorWrapperz.lzmac                 C   r5   r   ©ÚlzmaÚLZMAFiler   r8   r   r   r   r!       r9   zLZMACompressorWrapper.__init__Nc                 C   ó.   |du r| j |dtjd�S | j |dtj|d�S )r#   Nr$   )r   )r   Úpreset)r   rB   ÚFORMAT_ALONEr'   r   r   r   r)   ¦   s   ÿþz%LZMACompressorWrapper.compressor_filec                 C   s4   t rtdurt |d¡S td tjd tjd ¡ƒ‚)r+   Nr,   zFLzma decompression is not supported for this version of python ({}.{})r   r   )r   rB   rC   ÚNotImplementedErrorr   ÚsysÚversion_infor-   r   r   r   r.   °   s   
ÿýz'LZMACompressorWrapper.decompressor_filer   )	r/   r0   r1   Ú_LZMA_PREFIXr   r   r!   r)   r.   r   r   r   r   r@   ›   s    

r@   c                   @   s&   e Zd ZeZdZdd„ Zddd„ZdS )ÚXZCompressorWrapperz.xzc                 C   r5   r   rA   r8   r   r   r   r!   Æ   r9   zXZCompressorWrapper.__init__Nc                 C   rD   )r#   Nr$   )Úcheck)rL   rE   )r   rB   Ú
CHECK_NONEr'   r   r   r   r)   Ì   s
   ÿz#XZCompressorWrapper.compressor_filer   )r/   r0   r1   Ú
_XZ_PREFIXr   r   r!   r)   r   r   r   r   rK   Á   s
    rK   c                   @   r3   )ÚLZ4CompressorWrapperz.lz4c                 C   s    t rtd urt| _d S d | _d S r   )r   Úlz4r   r   r8   r   r   r   r!   Ú   s   

zLZ4CompressorWrapper.__init__c                 C   sT   t stdƒ‚td u rttƒ‚tj}| d¡r|dd … }t|ƒtdƒk r(ttƒ‚d S )Nz0lz4 compression is only available with python3+.Úvr   z0.19)r   r   rP   ÚLZ4_NOT_INSTALLED_ERRORÚ__version__Ú
startswithr   )r   Úlz4_versionr   r   r   r:   à   s   
ÿz$LZ4CompressorWrapper._check_versionsNc                 C   r;   )r#   Nr$   )Úcompression_levelr<   r'   r   r   r   r)   í   r=   z$LZ4CompressorWrapper.compressor_filec                 C   s   |   ¡  |  |d¡S r*   r<   r-   r   r   r   r.   ö   s   z&LZ4CompressorWrapper.decompressor_filer   )
r/   r0   r1   Ú_LZ4_PREFIXr   r   r!   r:   r)   r.   r   r   r   r   rO   Õ   s    
	rO   é   é   i    c                   @   sÄ   e Zd ZdZejZd0dd„Zdd„ Ze	dd	„ ƒZ
d
d„ Zdd„ Zdd„ Zdd„ Zdd„ Zdd„ Zdd„ Zdd„ Zdd„ Zd1dd„Zd1dd „Zd2d"d#„Zd$d%„ Zd&d'„ Zd(d)„ Zd3d+d,„Zd-d.„ Zd/S )4ÚBinaryZlibFileaŽ  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,   rY   c                 C   s  t ƒ | _d | _d| _t| _d| _d| _|| _t	|t
ƒr&d|  kr%dks-n td |¡ƒ‚|dkrBt| _t | j¡| _d| _d| _n|d	krXt| _t | jtj| jtjd¡| _ntd
|f ƒ‚t	|tƒrpt ||¡| _d| _d S t|dƒszt|dƒr|| _d S tdƒ‚)NFr   éÿÿÿÿr   é	   zS'compresslevel' must be an integer between 1 and 9. You provided 'compresslevel={}'r,   r   r$   zInvalid mode: %rTr	   r
   z1filename must be a str or bytes object, or a file)r   Ú_lockÚ_fpÚ_closefpÚ_MODE_CLOSEDÚ_modeÚ_posÚ_sizer%   r   Úintr   r   Ú
_MODE_READÚzlibÚdecompressobjÚwbitsÚ_decompressorÚ_bufferÚ_buffer_offsetÚ_MODE_WRITEÚcompressobjÚDEFLATEDÚDEF_MEM_LEVELÚ_compressorr   ÚioÚopenr   Ú	TypeError)r   ÚfilenameÚmoder%   r   r   r   r!     s:    þþ


zBinaryZlibFile.__init__c                 C   sN  | j �š | jtkr	 W d  ƒ dS zG| jttfv rd| _n| jtkr/| j | j	 
¡ ¡ d| _	W z| jr9| j ¡  W d| _d| _t| _d| _d| _nKd| _d| _t| _d| _d| _w z*| jrt| j ¡  W d| _d| _t| _d| _d| _w W d| _d| _t| _d| _d| _w d| _d| _t| _d| _d| _w W d  ƒ dS 1 s w   Y  dS )z«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.
        NFr   r   )r]   ra   r`   re   Ú_MODE_READ_EOFri   rl   r^   r
   rp   Úflushr_   Úcloserj   rk   r8   r   r   r   rx   D  sZ   
þ
€
€üøùü
"îzBinaryZlibFile.closec                 C   s
   | j tkS )zTrue if this file is closed.)ra   r`   r8   r   r   r   Úclosed^  s   
zBinaryZlibFile.closedc                 C   s   |   ¡  | j ¡ S )z3Return the file descriptor for the underlying file.)Ú_check_not_closedr^   Úfilenor8   r   r   r   r{   c  ó   
zBinaryZlibFile.filenoc                 C   s   |   ¡ o| j ¡ S )z)Return whether the file supports seeking.)Úreadabler^   Úseekabler8   r   r   r   r~   h  s   zBinaryZlibFile.seekablec                 C   s   |   ¡  | jttfv S )z/Return whether the file was opened for reading.)rz   ra   re   rv   r8   r   r   r   r}   l  s   zBinaryZlibFile.readablec                 C   s   |   ¡  | jtkS )z/Return whether the file was opened for writing.)rz   ra   rl   r8   r   r   r   Úwritableq  r|   zBinaryZlibFile.writablec                 C   sB   | j rt| jdd ƒ}d}|d ur|d |¡7 }|d7 }t|ƒ‚d S )Nr>   zI/O operation on closed filez {}Ú.)ry   Úgetattrr^   r   r   )r   ÚfnameÚmsgr   r   r   rz   x  s   úz BinaryZlibFile._check_not_closedc                 C   s$   | j ttfvr|  ¡  t d¡‚d S )NzFile not open for reading)ra   re   rv   rz   rq   ÚUnsupportedOperationr8   r   r   r   Ú_check_can_read�  s   
þzBinaryZlibFile._check_can_readc                 C   s    | j tkr|  ¡  t d¡‚d S )NzFile not open for writing)ra   rl   rz   rq   r„   r8   r   r   r   Ú_check_can_write†  s   

þzBinaryZlibFile._check_can_writec                 C   s8   | j ttfvr|  ¡  t d¡‚| j ¡ st d¡‚d S )Nz3Seeking is only supported on files open for readingz3The underlying file object does not support seeking)ra   re   rv   rz   rq   r„   r^   r~   r8   r   r   r   Ú_check_can_seek‹  s   


ÿzBinaryZlibFile._check_can_seekc                 C   sŠ   | j tkrdS | jt| jƒkrCz| jjp| j t	¡}|st
‚W n t
y0   t| _ | j| _Y dS w | j |¡| _d| _| jt| jƒksdS )NFr   T)ra   rv   rk   Úlenrj   ri   Úunused_datar^   r	   Ú_BUFFER_SIZEÚEOFErrorrb   rc   Ú
decompress)r   Úrawblockr   r   r   Ú_fill_buffer•  s&   

ÿÿüózBinaryZlibFile._fill_bufferTc                 C   sh   | j | jd … | _ d| _g }|  ¡ r+|r| | j ¡ |  jt| j ƒ7  _d| _ |  ¡ s|r2d |¡S d S ©Nr   r   )rj   rk   rŽ   Úappendrb   rˆ   Újoin)r   Úreturn_dataÚblocksr   r   r   Ú	_read_all¬  s   ü
ÿzBinaryZlibFile._read_allc                 C   sö   | j | }|t| jƒkr&| j| j |… }|| _ |  jt|ƒ7  _|r$|S d S | j| j d … | _d| _ g }|dkrr|  ¡ rr|t| jƒk rN| jd |… }|| _ n| j}d| _|r[| |¡ |  jt|ƒ7  _|t|ƒ8 }|dkrr|  ¡ s<|ryd |¡S d S r�   )rk   rˆ   rj   rb   rŽ   r�   r‘   )r   Ún_bytesr’   ÚendÚdatar“   r   r   r   Ú_read_block¼  s.   

ö
ÿzBinaryZlibFile._read_blockr[   c                 C   st   | j �- |  ¡  |dkr	 W d  ƒ dS |dk r$|  ¡ W  d  ƒ S |  |¡W  d  ƒ S 1 s3w   Y  dS )z±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.
        r   Nr   )r]   r…   r”   r˜   )r   Úsizer   r   r   r	   Ø  s   ýû$ùzBinaryZlibFile.readc                 C   s8   | j � tj | |¡W  d  ƒ S 1 sw   Y  dS )z_Read up to len(b) bytes into b.

        Returns the number of bytes read (0 for EOF).
        N)r]   rq   ÚBufferedIOBaseÚreadinto)r   Úbr   r   r   r›   ç  s   $ÿzBinaryZlibFile.readintoc                 C   sv   | j �. |  ¡  t|tƒr| ¡ }| j |¡}| j |¡ |  j	t
|ƒ7  _	t
|ƒW  d  ƒ S 1 s4w   Y  dS )zö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)r]   r†   r   Ú
memoryviewÚtobytesrp   Úcompressr^   r
   rb   rˆ   )r   r—   Ú
compressedr   r   r   r
   ï  s   
$÷zBinaryZlibFile.writec                 C   s8   | j  dd¡ t| _d| _t | j¡| _d| _	d| _
d S r�   )r^   r   re   ra   rb   rf   rg   rh   ri   rj   rk   r8   r   r   r   Ú_rewind  s   
zBinaryZlibFile._rewindr   c                 C   sº   | j �P |  ¡  |dkrn&|dkr| j| }n|dkr,| jdk r&| jdd� | j| }ntd|f ƒ‚|| jk r=|  ¡  n|| j8 }| j|dd� | jW  d  ƒ S 1 sVw   Y  dS )aç  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.
        r   r   rX   F)r’   zInvalid value for whence: %sN)r]   r‡   rb   rc   r”   r   r¡   r˜   )r   ÚoffsetÚwhencer   r   r   r   
  s"   



$çzBinaryZlibFile.seekc                 C   s8   | j � |  ¡  | jW  d  ƒ S 1 sw   Y  dS )z!Return the current file position.N)r]   rz   rb   r8   r   r   r   r   4  s   $þzBinaryZlibFile.tellN)r,   rY   )T)r[   )r   )r/   r0   r1   r2   rf   Ú	MAX_WBITSrh   r!   rx   Úpropertyry   r{   r~   r}   r   rz   r…   r†   r‡   rŽ   r”   r˜   r	   r›   r
   r¡   r   r   r   r   r   r   rZ     s0    
&
	




*rZ   c                   @   ó   e Zd Zdd„ ZdS )ÚZlibCompressorWrapperc                 C   ó   t j| ttdd� d S )Nz.z©r    r   r   )r   r!   rZ   Ú_ZLIB_PREFIXr8   r   r   r   r!   =  ó   
ÿzZlibCompressorWrapper.__init__N©r/   r0   r1   r!   r   r   r   r   r§   ;  ó    r§   c                   @   s   e Zd ZdZdZdS )ÚBinaryGzipFilea  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.
    é   N)r/   r0   r1   r2   rh   r   r   r   r   r®   B  s    r®   c                   @   r¦   )ÚGzipCompressorWrapperc                 C   r¨   )Nz.gzr©   )r   r!   r®   Ú_GZIP_PREFIXr8   r   r   r   r!   U  r«   zGzipCompressorWrapper.__init__Nr¬   r   r   r   r   r°   S  r­   r°   )F)+r2   rH   rq   rf   Údistutils.versionr   Ú_compatr   r   Ú	threadingr   ÚImportErrorÚdummy_threadingr6   rB   rP   Ú	lz4.framer   rR   r   Ú_ZFILE_PREFIXrª   r±   r?   rN   rJ   rW   r   r   r4   r@   rK   rO   r`   re   rv   rl   rŠ   rš   rZ   r§   r®   r°   r   r   r   r   Ú<module>   sp    ÿÿÿ€ÿ
ÿ$"%&)  8