/usr/lib/python3/dist-packages/lz4/frame/__init__.py is in python3-lz4 0.10.1+dfsg1-0.2.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 | from ._frame import *
from ._frame import __doc__ as _doc
__doc__ = _doc
class LZ4FrameCompressor(object):
"""Create a LZ4 compressor object, which can be used to compress data
incrementally.
Args:
block_size (int): Sepcifies the maximum blocksize to use.
Options:
- lz4.frame.BLOCKSIZE_DEFAULT or 0: the lz4 library default
- lz4.frame.BLOCKSIZE_MAX64KB or 4: 64 kB
- lz4.frame.BLOCKSIZE_MAX256KB or 5: 256 kB
- lz4.frame.BLOCKSIZE_MAX1MB or 6: 1 MB
- lz4.frame.BLOCKSIZE_MAX4MB or 7: 4 MB
If unspecified, will default to lz4.frame.BLOCKSIZE_DEFAULT.
block_mode (int): Specifies whether to use block-linked
compression. Options:
- lz4.frame.BLOCKMODE_LINKED or 0: linked mode
- lz4.frame.BLOCKMODE_INDEPENDENT or 1: disable linked mode
The default is lz4.frame.BLOCKMODE_LINKED.
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 (int): Specifies whether to enable checksumming of
the payload content. Options:
- lz4.frame.CONTENTCHECKSUM_DISABLED or 0: disables checksumming
- lz4.frame.CONTENTCHECKSUM_ENABLED or 1: enables checksumming
The default is CONTENTCHECKSUM_DISABLED.
frame_type (int): Specifies whether user data can be injected between
frames. Options:
- lz4.frame.FRAMETYPE_FRAME or 0: disables user data injection
- lz4.frame.FRAMETYPE_SKIPPABLEFRAME or 1: enables user data
injection
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 True.
"""
def __init__(self,
block_size=BLOCKSIZE_DEFAULT,
block_mode=BLOCKMODE_LINKED,
compression_level=COMPRESSIONLEVEL_MIN,
content_checksum=CONTENTCHECKSUM_DISABLED,
frame_type=FRAMETYPE_FRAME,
auto_flush=True):
self.block_size = block_size
self.block_mode = block_mode
self.compression_level = compression_level
self.content_checksum = content_checksum
self.frame_type = frame_type
self.auto_flush = auto_flush
self._context = create_compression_context()
self._started = False
def __enter__(self):
# All necessary initialization is done in __init__
return self
def __exit__(self, exception_type, exception, traceback):
# The compression context is created with an appropriate destructor, so
# no need to del it here
pass
def compress_begin(self, source_size=0):
"""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.
Args:
data (bytes): data to compress
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.
Returns:
bytes: frame header data
"""
if self._started is False:
result = compress_begin(self._context,
block_size=self.block_size,
block_mode=self.block_mode,
frame_type=self.frame_type,
compression_level=self.compression_level,
content_checksum=self.content_checksum,
auto_flush=self.auto_flush,
source_size=source_size)
self._started = True
return result
else:
raise RuntimeError('compress_begin called when not already initialized')
def compress(self, data):
"""Compress ``data`` (a ``bytes`` object), returning a bytes 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 compress() and 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 (bytes): data to compress
Returns:
bytes: compressed data
"""
if self._context is None:
raise RuntimeError('compress called after flush()')
if self._started is False:
raise RuntimeError('compress called before compress_begin()')
result = compress_update(self._context, data)
return result
def flush(self):
"""Finish the compression process, returning a bytes object containing any data
stored in the compressor's internal buffers and a frame footer.
To use the LZ4FrameCompressor instance after this has been called, it
is necessary to first call the ``reset()`` method.
Returns:
bytes: any remaining buffered compressed data and frame footer.
"""
result = compress_end(self._context)
self._context = None
self._started = False
return result
def reset(self):
"""Reset the LZ4FrameCompressor instance (after a call to ``flush``) allowing
it to be re-used
"""
self._context = create_compression_context()
self._started = False
|