o
    `s                     @   s4  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mZmZm	Z	m
Z
mZmZmZmZmZmZmZmZmZmZmZ eZzd dlZW n eyO   ddlmZ Y nw eZ	 eZ	 eZ	 eZ	 eZ	 d Z	 dZ 	 dZ!	 G dd de"Z#G d	d
 d
e"Z$d Z%dZ&dZ'G dd dej(Z)ddddededdddd fddZ*dS )    N   )compress
decompresscreate_compression_contextcompress_begincompress_chunkcompress_flushcreate_decompression_contextreset_decompression_contextdecompress_chunkget_frame_infoBLOCKSIZE_DEFAULTBLOCKSIZE_MAX64KBBLOCKSIZE_MAX256KBBLOCKSIZE_MAX1MBBLOCKSIZE_MAX4MB__doc__)_compression      c                   @   sZ   e Zd ZdZededdddfddZdd Zdd	 ZdddZ	dd Z
dd Zdd ZdS )LZ4FrameCompressora  Create a LZ4 frame compressor object.

    This object can be used to compress data incrementally.

    Args:
        block_size (int): Specifies the maximum blocksize to use.
            Options:

            - `lz4.frame.BLOCKSIZE_DEFAULT`: the lz4 library default
            - `lz4.frame.BLOCKSIZE_MAX64KB`: 64 kB
            - `lz4.frame.BLOCKSIZE_MAX256KB`: 256 kB
            - `lz4.frame.BLOCKSIZE_MAX1MB`: 1 MB
            - `lz4.frame.BLOCKSIZE_MAX4MB`: 4 MB

            If unspecified, will default to `lz4.frame.BLOCKSIZE_DEFAULT` which
            is equal to `lz4.frame.BLOCKSIZE_MAX64KB`.
        block_linked (bool): Specifies whether to use block-linked
            compression. If ``True``, the compression ratio is improved,
            especially for small block sizes. If ``False`` the blocks are
            compressed independently. The default is ``True``.
        compression_level (int): Specifies the level of compression used.
            Values between 0-16 are valid, with 0 (default) being the
            lowest compression (0-2 are the same value), and 16 the highest.
            Values above 16 will be treated as 16.
            Values between 4-9 are recommended. 0 is the default.
            The following module constants are provided as a convenience:

            - `lz4.frame.COMPRESSIONLEVEL_MIN`: Minimum compression (0)
            - `lz4.frame.COMPRESSIONLEVEL_MINHC`: Minimum high-compression (3)
            - `lz4.frame.COMPRESSIONLEVEL_MAX`: Maximum compression (16)

        content_checksum (bool): Specifies whether to enable checksumming of
            the payload content. If ``True``, a checksum of the uncompressed
            data is stored at the end of the compressed frame which is checked
            during decompression. The default is ``False``.
        block_checksum (bool): Specifies whether to enable checksumming of
            the content of each block. If ``True`` a checksum of the
            uncompressed data in each block in the frame is stored at the end
            of each block. If present, these checksums will be used to
            validate the data during decompression. The default is ``False``,
            meaning block checksums are not calculated and stored. This
            functionality is only supported if the underlying LZ4 library has
            version >= 1.8.0. Attempting to set this value to ``True`` with a
            version of LZ4 < 1.8.0 will cause a ``RuntimeError`` to be raised.
        auto_flush (bool): When ``False``, the LZ4 library may buffer data
            until a block is full. When ``True`` no buffering occurs, and
            partially full blocks may be returned. The default is ``False``.
        return_bytearray (bool): When ``False`` a ``bytes`` object is returned
            from the calls to methods of this class. When ``True`` a
            ``bytearray`` object will be returned. The default is ``False``.

    TFc                 C   sR   || _ || _|| _|| _|rt dk rtd|| _|| _|| _	d | _
d| _d S )Ni0*  zEAttempt to set block_checksum to True with LZ4 libraryversion < 10800F)
block_sizeblock_linkedcompression_levelcontent_checksumlz4Zlibrary_version_numberRuntimeErrorblock_checksum
auto_flushreturn_bytearray_context_started)selfr   r   r   r   r   r   r    r#   4/usr/lib/python3/dist-packages/lz4/frame/__init__.py__init__   s   
zLZ4FrameCompressor.__init__c                 C      | S Nr#   r"   r#   r#   r$   	__enter__      zLZ4FrameCompressor.__enter__c                 C   s:   d | _ d | _d | _d | _d | _d | _d | _d | _d| _d S )NF)	r   r   r   r   r   r   r   r    r!   r"   Zexception_typeZ	exception	tracebackr#   r#   r$   __exit__   s   
zLZ4FrameCompressor.__exit__r   c                 C   sN   | j du r#t | _t| j| j| j| j| j| j| j	| j
|d	}d| _ |S td)aM  Begin a compression frame.

        The returned data contains frame header information. The data returned
        from subsequent calls to ``compress()`` should be concatenated with
        this header.

        Keyword Args:
            source_size (int): Optionally specify the total size of the
                uncompressed data. If specified, will be stored in the
                compressed frame header as an 8-byte field for later use
                during decompression. Default is 0 (no size stored).

        Returns:
            bytes or bytearray: frame header data

        F)r   r   r   r   r   r   r   source_sizeTz;LZ4FrameCompressor.begin() called after already initialized)r!   r   r    r   r   r   r   r   r   r   r   r   )r"   r.   resultr#   r#   r$   begin   s$   
zLZ4FrameCompressor.beginc                 C   s:   | j du r	td| jdu rtdt| j || jd}|S )a  Compresses data and returns it.

        This compresses ``data`` (a ``bytes`` object), returning a bytes or
        bytearray object containing compressed data the input.

        If ``auto_flush`` has been set to ``False``, some of ``data`` may be
        buffered internally, for use in later calls to
        `LZ4FrameCompressor.compress()` and `LZ4FrameCompressor.flush()`.

        The returned data should be concatenated with the output of any
        previous calls to `compress()` and a single call to
        `compress_begin()`.

        Args:
            data (str, bytes or buffer-compatible object): data to compress

        Returns:
            bytes or bytearray: compressed data

        Nzcompress called after flush()Fz'compress called before compress_begin())r   )r    r   r!   r   r   )r"   datar/   r#   r#   r$   r      s   

zLZ4FrameCompressor.compressc                 C   s"   t | jd| jd}d| _d| _|S )a  Finish the compression process.

        This returns a ``bytes`` or ``bytearray`` object containing any data
        stored in the compressor's internal buffers and a frame footer.

        The LZ4FrameCompressor instance may be re-used after this method has
        been called to create a new frame of compressed data.

        Returns:
            bytes or bytearray: compressed data and frame footer.

        T)Z	end_framer   NF)r   r    r   r!   )r"   r/   r#   r#   r$   flush	  s   zLZ4FrameCompressor.flushc                 C   s   d| _ d| _dS )zReset the `LZ4FrameCompressor` instance.

        This allows the `LZ4FrameCompression` instance to be re-used after an
        error.

        NF)r    r!   r(   r#   r#   r$   reset  s   
zLZ4FrameCompressor.resetN)r   )__name__
__module____qualname__r   r   COMPRESSIONLEVEL_MINr%   r)   r-   r0   r   r2   r3   r#   r#   r#   r$   r   e   s     6

&"r   c                   @   s<   e Zd ZdZdddZdd Zdd Zd	d
 ZdddZdS )LZ4FrameDecompressora  Create a LZ4 frame decompressor object.

    This can be used to decompress data incrementally.

    For a more convenient way of decompressing an entire compressed frame at
    once, see `lz4.frame.decompress()`.

    Args:
        return_bytearray (bool): When ``False`` a bytes object is returned from
            the calls to methods of this class. When ``True`` a bytearray
            object will be returned. The default is ``False``.

    Attributes:
        eof (bool): ``True`` if the end-of-stream marker has been reached.
            ``False`` otherwise.
        unused_data (bytes): Data found after the end of the compressed stream.
            Before the end of the frame is reached, this will be ``b''``.
        needs_input (bool): ``False`` if the ``decompress()`` method can
            provide more decompressed data before requiring new uncompressed
            input. ``True`` otherwise.

    Fc                 C   s*   t  | _d| _d| _d | _d| _|| _d S )NFT    )r	   r    eofneeds_inputunused_data_unconsumed_data_return_bytearray)r"   r   r#   r#   r$   r%   B  s   
zLZ4FrameDecompressor.__init__c                 C   r&   r'   r#   r(   r#   r#   r$   r)   J  r*   zLZ4FrameDecompressor.__enter__c                 C   s(   d | _ d | _d | _d | _d | _d | _d S r'   )r    r:   r;   r<   r=   r>   r+   r#   r#   r$   r-   N  s   
zLZ4FrameDecompressor.__exit__c                 C   s&   t | j d| _d| _d| _d| _dS )zwReset the decompressor state.

        This is useful after an error occurs, allowing re-use of the instance.

        FTNr9   )r
   r    r:   r;   r<   r=   r(   r#   r#   r$   r3   V  s
   

zLZ4FrameDecompressor.resetc                 C   s|   | j r| j | }t| j||| jd\}}}|t|k r0|r%||d | _n||d | _ d| _n	d| _ d| _d| _|| _|S )a#  Decompresses part or all of an LZ4 frame of compressed data.

        The returned data should be concatenated with the output of any
        previous calls to `decompress()`.

        If ``max_length`` is non-negative, returns at most ``max_length`` bytes
        of decompressed data. If this limit is reached and further output can
        be produced, the `needs_input` attribute will be set to ``False``. In
        this case, the next call to `decompress()` may provide data as
        ``b''`` to obtain more of the output. In all cases, any unconsumed data
        from previous calls will be prepended to the input data.

        If all of the input ``data`` was decompressed and returned (either
        because this was less than ``max_length`` bytes, or because
        ``max_length`` was negative), the `needs_input` attribute will be set
        to ``True``.

        If an end of frame marker is encountered in the data during
        decompression, decompression will stop at the end of the frame, and any
        data after the end of frame is available from the `unused_data`
        attribute. In this case, the `LZ4FrameDecompressor` instance is reset
        and can be used for further decompression.

        Args:
            data (str, bytes or buffer-compatible object): compressed data to
                decompress

        Keyword Args:
            max_length (int): If this is non-negative, this method returns at
                most ``max_length`` bytes of decompressed data.

        Returns:
            bytes: Uncompressed data

        )
max_lengthr   NFr9   T)r=   r   r    r>   lenr<   r;   r:   )r"   r1   r@   ZdecompressedZ
bytes_readZeoframer#   r#   r$   r   b  s$   %
zLZ4FrameDecompressor.decompressN)Fr?   )	r4   r5   r6   r   r%   r)   r-   r3   r   r#   r#   r#   r$   r8   *  s    
r8   c                
   @   s   e Zd ZdZddededddddf
ddZd	d
 Zedd Z	dd Z
dd Zdd Zdd Zd$ddZd$ddZd$ddZd$ddZdd Zejfd d!Zd"d# ZdS )%LZ4FrameFilea  A file object providing transparent LZ4F (de)compression.

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

    Note that LZ4FFile provides a *binary* file interface - data read is
    returned as bytes, and data to be written must be given as bytes.

    When opening a file for writing, the settings used by the compressor can be
    specified. The underlying compressor object is
    `lz4.frame.LZ4FrameCompressor`. See the docstrings for that class for
    details on compression options.

    Args:
        filename(str, bytes, PathLike, file object): can be either an actual
            file name (given as a str, bytes, or
            PathLike object), in which case the named file is opened, or it
            can be an existing file object to read from or write to.

    Keyword Args:
        mode(str): mode can be ``'r'`` for reading (default), ``'w'`` for
            (over)writing, ``'x'`` for creating exclusively, or ``'a'``
            for appending. These can equivalently be given as ``'rb'``,
            ``'wb'``, ``'xb'`` and ``'ab'`` respectively.
        return_bytearray (bool): When ``False`` a bytes object is returned from
            the calls to methods of this class. When ``True`` a ``bytearray``
            object will be returned. The default is ``False``.
        source_size (int): Optionally specify the total size of the
            uncompressed data. If specified, will be stored in the compressed
            frame header as an 8-byte field for later use during decompression.
            Default is ``0`` (no size stored). Only used for writing
            compressed files.
        block_size (int): Compressor setting. See
            `lz4.frame.LZ4FrameCompressor`.
        block_linked (bool): Compressor setting. See
            `lz4.frame.LZ4FrameCompressor`.
        compression_level (int): Compressor setting. See
            `lz4.frame.LZ4FrameCompressor`.
        content_checksum (bool): Compressor setting. See
            `lz4.frame.LZ4FrameCompressor`.
        block_checksum (bool): Compressor setting. See
            `lz4.frame.LZ4FrameCompressor`.
        auto_flush (bool): Compressor setting. See
            `lz4.frame.LZ4FrameCompressor`.

    NrTFr   c              	   C   s0  d | _ d| _t| _|dv rt}n|dv r&t}t|||||||	d| _d| _nt	d
|tjdkr<t|tttjf}nt|ttf}|du r]d	|vrO|d	7 }t||| _ d| _|| _nt|d
sgt|drn|| _ || _ntd| jtkrt| j t}t|| _| jtkr| j | jj|
d d S d S )NF)rD   rb)wwbaabxxb)r   r   r   r   r   r   r   r   zInvalid mode: {!r})r      Tbreadwritez6filename must be a str, bytes, file or PathLike object)r.   )_fp_closefp_MODE_CLOSED_mode
_MODE_READ_MODE_WRITEr   _compressor_pos
ValueErrorformatsysversion_info
isinstancestrbytesosPathLikebuiltinsopenhasattr	TypeErrorr   DecompressReaderr8   ioBufferedReader_bufferrO   r0   )r"   filenamemoder   r   r   r   r   r   r   r.   	mode_codeZ	path_testrawr#   r#   r$   r%     sR   
	


zLZ4FrameFile.__init__c                 C   s   | j tkrdS zK| j tkr| j  d| _n| j tkr'| j| j	  d| _W z | j
r=| j  W d| _d| _
t| _ dS W d| _d| _
t| _ dS d| _d| _
t| _ w z| j
rg| j  W d| _d| _
t| _ w W d| _d| _
t| _ w d| _d| _
t| _ w )zFlush 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.
        NF)rS   rR   rT   rh   closerU   rP   rO   rV   r2   rQ   r(   r#   r#   r$   rm     sH   





zLZ4FrameFile.closec                 C   s
   | j tkS )zReturns ``True`` if this file is closed.

        Returns:
            bool: ``True`` if the file is closed, ``False`` otherwise.

        )rS   rR   r(   r#   r#   r$   closed*  s   
zLZ4FrameFile.closedc                 C   s   |    | j S )zReturn the file descriptor for the underlying file.

        Returns:
            file object: file descriptor for file.

        )_check_not_closedrP   filenor(   r#   r#   r$   rp   4  s   
zLZ4FrameFile.filenoc                 C   s   |   o| j S )zReturn whether the file supports seeking.

        Returns:
            bool: ``True`` if the file supports seeking, ``False`` otherwise.

        )readablerh   seekabler(   r#   r#   r$   rr   >  s   zLZ4FrameFile.seekablec                 C      |    | jtkS )zReturn whether the file was opened for reading.

        Returns:
            bool: ``True`` if the file was opened for reading, ``False``
                otherwise.

        )ro   rS   rT   r(   r#   r#   r$   rq   G     
zLZ4FrameFile.readablec                 C   rs   )zReturn whether the file was opened for writing.

        Returns:
            bool: ``True`` if the file was opened for writing, ``False``
                otherwise.

        )ro   rS   rU   r(   r#   r#   r$   writableR  rt   zLZ4FrameFile.writabler?   c                 C      |    | j|S )zReturn buffered data without advancing the file position.

        Always returns at least one byte of data, unless at EOF. The exact
        number of bytes returned is unspecified.

        Returns:
            bytes: uncompressed data

        )_check_can_readrh   peekr"   sizer#   r#   r$   rx   ]  s   
zLZ4FrameFile.peekc                 C   rv   )a  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``.

        Args:
            size(int): If non-negative, specifies the maximum number of
                uncompressed bytes to return.

        Returns:
            bytes: uncompressed data

        )rw   rh   rN   ry   r#   r#   r$   rN   l  s   zLZ4FrameFile.readc                 C   s"   |    |dk rtj}| j|S )a  Read up to ``size`` uncompressed bytes.

        This method tries to avoid making multiple reads from the underlying
        stream.

        This method reads up to a buffer's worth of data if ``size`` is
        negative.

        Returns ``b''`` if the file is at EOF.

        Args:
            size(int): If non-negative, specifies the maximum number of
                uncompressed bytes to return.

        Returns:
            bytes: uncompressed data

        r   )rw   rf   DEFAULT_BUFFER_SIZErh   read1ry   r#   r#   r$   r|   }  s   zLZ4FrameFile.read1c                 C   rv   )a  Read a line of uncompressed bytes from the file.

        The terminating newline (if present) is retained. If size is
        non-negative, no more than size bytes will be read (in which case the
        line may be incomplete). Returns b'' if already at EOF.

        Args:
            size(int): If non-negative, specifies the maximum number of
                uncompressed bytes to return.

        Returns:
            bytes: uncompressed data

        )rw   rh   readlinery   r#   r#   r$   r}     s   zLZ4FrameFile.readlinec                 C   s:   |    | j|}| j| |  jt|7  _t|S )a  Write a bytes object 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.

        Args:
            data(bytes): uncompressed data to compress and write to the file

        Returns:
            int: the number of uncompressed bytes written to the file

        )_check_can_writerV   r   rP   rO   rW   rA   )r"   r1   
compressedr#   r#   r$   rO     s
   zLZ4FrameFile.writec                 C   s   |    | j||S )aE  Change the file position.

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

        - ``io.SEEK_SET`` or 0: start of stream (default): offset must not be
          negative
        - ``io.SEEK_CUR`` or 1: current stream position
        - ``io.SEEK_END`` or 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.

        Args:
            offset(int): new position in the file
            whence(int): position with which ``offset`` is measured. Allowed
                values are 0, 1, 2. The default is 0 (start of stream).

        Returns:
            int: new file position

        )_check_can_seekrh   seek)r"   offsetwhencer#   r#   r$   r     s   zLZ4FrameFile.seekc                 C   s"   |    | jtkr| j S | jS )z|Return the current file position.

        Args:
            None

        Returns:
            int: file position

        )ro   rS   rT   rh   tellrW   r(   r#   r#   r$   r     s   


zLZ4FrameFile.tellrB   )r4   r5   r6   r   r   r7   r%   rm   propertyrn   rp   rr   rq   ru   rx   rN   r|   r}   rO   rf   SEEK_SETr   r   r#   r#   r#   r$   rC     s4    /
;
	
	



rC   rE   TFc                 C   s   d|v rd|v rt d|f n|durt d|dur t d|dur(t d|dd}t| ||||||	|
|d		}d|v rGt||||S |S )
a	
  Open an LZ4Frame-compressed file in binary or text mode.

    ``filename`` can be either an actual file name (given as a str, bytes, or
    PathLike object), in which case the named file is opened, or it can be an
    existing file object to read from or write to.

    The ``mode`` argument can be ``'r'``, ``'rb'`` (default), ``'w'``,
    ``'wb'``, ``'x'``, ``'xb'``, ``'a'``, or ``'ab'`` for binary mode, or
    ``'rt'``, ``'wt'``, ``'xt'``, or ``'at'`` for text mode.

    For binary mode, this function is equivalent to the `LZ4FrameFile`
    constructor: `LZ4FrameFile(filename, mode, ...)`.

    For text mode, an `LZ4FrameFile` object is created, and wrapped in an
    ``io.TextIOWrapper`` instance with the specified encoding, error handling
    behavior, and line ending(s).

    Args:
        filename (str, bytes, os.PathLike): file name or file object to open

    Keyword Args:
        mode (str): mode for opening the file
        encoding (str): the name of the encoding that will be used for
            encoding/deconging the stream. It defaults to
            ``locale.getpreferredencoding(False)``. See ``io.TextIOWrapper``
            for further details.
        errors (str): specifies how encoding and decoding errors are to be
            handled. See ``io.TextIOWrapper`` for further details.
        newline (str): controls how line endings are handled. See
            ``io.TextIOWrapper`` for further details.
        return_bytearray (bool): When ``False`` a bytes object is returned
            from the calls to methods of this class. When ``True`` a bytearray
            object will be returned. The default is ``False``.
        source_size (int): Optionally specify the total size of the
            uncompressed data. If specified, will be stored in the compressed
            frame header as an 8-byte field for later use during decompression.
            Default is 0 (no size stored). Only used for writing compressed
            files.
        block_size (int): Compressor setting. See
            `lz4.frame.LZ4FrameCompressor`.
        block_linked (bool): Compressor setting. See
            `lz4.frame.LZ4FrameCompressor`.
        compression_level (int): Compressor setting. See
            `lz4.frame.LZ4FrameCompressor`.
        content_checksum (bool): Compressor setting. See
            `lz4.frame.LZ4FrameCompressor`.
        block_checksum (bool): Compressor setting. See
            `lz4.frame.LZ4FrameCompressor`.
        auto_flush (bool): Compressor setting. See
            `lz4.frame.LZ4FrameCompressor`.

    trM   zInvalid mode: %rNz0Argument 'encoding' not supported in binary modez.Argument 'errors' not supported in binary modez/Argument 'newline' not supported in binary mode )rj   r   r   r   r   r   r   r   )rX   replacerC   rf   TextIOWrapper)ri   rj   encodingerrorsnewliner   r   r   r   r   r   r   r.   rS   binary_filer#   r#   r$   rb     s6   @rb   )+r   rf   r_   ra   rZ   Z_framer   r   r   r   r   r   r	   r
   r   r   r   Z_BLOCKSIZE_DEFAULTr   Z_BLOCKSIZE_MAX64KBr   Z_BLOCKSIZE_MAX256KBr   Z_BLOCKSIZE_MAX1MBr   Z_BLOCKSIZE_MAX4MBr   Z_docr   ImportErrorr   r7   ZCOMPRESSIONLEVEL_MINHCZCOMPRESSIONLEVEL_MAXobjectr   r8   rR   rT   rU   
BaseStreamrC   rb   r#   r#   r#   r$   <module>   sd    H	 Fw  B