Debian Codegraph @

lz4/stream/__init__.py

raw symbols SCIP
1 from ._stream import _create_context, _compress, _decompress, _get_block
2 from ._stream import LZ4StreamError, _compress_bound, _input_bound, LZ4_MAX_INPUT_SIZE # noqa: F401
3
4
5 __doc__ = """\
6 A Python wrapper for the LZ4 stream protocol.
7
8 """
9
10
11 class LZ4StreamDecompressor:
12 """ LZ4 stream decompression context.
13
14 """
15 def __init__(self, strategy, buffer_size, return_bytearray=False, store_comp_size=4, dictionary=""):
16 """ Instantiates and initializes a LZ4 stream decompression context.
17
18 Args:
19 strategy (str): Buffer management strategy. Can be: ``double_buffer``.
20 buffer_size (int): Size of one buffer of the double-buffer used
21 internally for stream decompression in the case of ``double_buffer``
22 strategy.
23
24 Keyword Args:
25 return_bytearray (bool): If ``False`` (the default) then the function
26 will return a ``bytes`` object. If ``True``, then the function will
27 return a ``bytearray`` object.
28 store_comp_size (int): Specify the size in bytes of the following
29 compressed block. Can be: ``0`` (meaning out-of-band block size),
30 ``1``, ``2`` or ``4`` (default: ``4``).
31 dictionary (str, bytes or buffer-compatible object): If specified,
32 perform decompression using this initial dictionary.
33
34 Raises:
35 Exceptions occurring during the context initialization.
36
37 OverflowError: raised if the ``dictionary`` parameter is too large
38 for the LZ4 context.
39 ValueError: raised if some parameters are invalid.
40 MemoryError: raised if some internal resources cannot be allocated.
41 RuntimeError: raised if some internal resources cannot be initialized.
42
43 """
44 return_bytearray = 1 if return_bytearray else 0
45
46 self._context = _create_context(strategy, "decompress", buffer_size,
47 return_bytearray=return_bytearray,
48 store_comp_size=store_comp_size,
49 dictionary=dictionary)
50
51 def __enter__(self):
52 """ Enter the LZ4 stream context.
53
54 """
55 return self
56
57 def __exit__(self, exc_type, exc, exc_tb):
58 """ Exit the LZ4 stream context.
59
60 """
61 pass
62
63 def decompress(self, chunk):
64 """ Decompress streamed compressed data.
65
66 Decompress the given ``chunk``, using the given LZ4 stream context,
67 Raises an exception if any error occurs.
68
69 Args:
70 chunk (str, bytes or buffer-compatible object): Data to decompress
71
72 Returns:
73 bytes or bytearray: Decompressed data.
74
75 Raises:
76 Exceptions occurring during decompression.
77
78 ValueError: raised if the source is inconsistent with a finite LZ4
79 stream block chain.
80 MemoryError: raised if the work output buffer cannot be allocated.
81 OverflowError: raised if the source is too large for being decompressed
82 in the given context.
83 LZ4StreamError: raised if the call to the LZ4 library fails. This can be
84 caused by ``decompressed_size`` being too small, or invalid data.
85
86 """
87 return _decompress(self._context, chunk)
88
89 def get_block(self, stream):
90 """ Return the first LZ4 compressed block from ``stream``.
91
92 Args:
93 stream (str, bytes or buffer-compatible object): LZ4 compressed stream.
94
95 Returns:
96 bytes or bytearray: LZ4 compressed data block.
97
98 Raises:
99 Exceptions occurring while getting the first block from ``stream``.
100
101 BufferError: raised if the function cannot return a complete LZ4
102 compressed block from the stream (i.e. the stream does not hold
103 a complete block).
104 MemoryError: raised if the output buffer cannot be allocated.
105 OverflowError: raised if the source is too large for being handled by
106 the given context.
107 LZ4StreamError: raised if used while in an out-of-band block size record
108 configuration.
109
110 """
111 return _get_block(self._context, stream)
112
113
114 class LZ4StreamCompressor:
115 """ LZ4 stream compressing context.
116
117 """
118 def __init__(self, strategy, buffer_size, mode="default", acceleration=True, compression_level=9,
119 return_bytearray=False, store_comp_size=4, dictionary=""):
120 """ Instantiates and initializes a LZ4 stream compression context.
121
122 Args:
123 strategy (str): Buffer management strategy. Can be: ``double_buffer``.
124 buffer_size (int): Base size of the buffer(s) used internally for stream
125 compression/decompression. In the ``double_buffer`` strategy case,
126 this is the size of each buffer of the double-buffer.
127
128 Keyword Args:
129 mode (str): If ``default`` or unspecified use the default LZ4
130 compression mode. Set to ``fast`` to use the fast compression
131 LZ4 mode at the expense of compression. Set to
132 ``high_compression`` to use the LZ4 high-compression mode at
133 the expense of speed.
134 acceleration (int): When mode is set to ``fast`` this argument
135 specifies the acceleration. The larger the acceleration, the
136 faster the but the lower the compression. The default
137 compression corresponds to a value of ``1``.
138 compression_level (int): When mode is set to ``high_compression`` this
139 argument specifies the compression. Valid values are between
140 ``1`` and ``12``. Values between ``4-9`` are recommended, and
141 ``9`` is the default. Only relevant if ``mode`` is
142 ``high_compression``.
143 return_bytearray (bool): If ``False`` (the default) then the function
144 will return a bytes object. If ``True``, then the function will
145 return a bytearray object.
146 store_comp_size (int): Specify the size in bytes of the following
147 compressed block. Can be: ``0`` (meaning out-of-band block size),
148 ``1``, ``2`` or ``4`` (default: ``4``).
149 dictionary (str, bytes or buffer-compatible object): If specified,
150 perform compression using this initial dictionary.
151
152 Raises:
153 Exceptions occurring during the context initialization.
154
155 OverflowError: raised if the ``dictionary`` parameter is too large
156 for the LZ4 context.
157 ValueError: raised if some parameters are invalid.
158 MemoryError: raised if some internal resources cannot be allocated.
159 RuntimeError: raised if some internal resources cannot be initialized.
160
161 """
162 return_bytearray = 1 if return_bytearray else 0
163
164 self._context = _create_context(strategy, "compress", buffer_size,
165 mode=mode,
166 acceleration=acceleration,
167 compression_level=compression_level,
168 return_bytearray=return_bytearray,
169 store_comp_size=store_comp_size,
170 dictionary=dictionary)
171
172 def __enter__(self):
173 """ Enter the LZ4 stream context.
174
175 """
176 return self
177
178 def __exit__(self, exc_type, exc, exc_tb):
179 """ Exit the LZ4 stream context.
180
181 """
182 pass
183
184 def compress(self, chunk):
185 """ Stream compress given ``chunk`` of data.
186
187 Compress the given ``chunk``, using the given LZ4 stream context,
188 returning the compressed data as a ``bytearray`` or as a ``bytes`` object.
189
190 Args:
191 chunk (str, bytes or buffer-compatible object): Data to compress
192
193 Returns:
194 bytes or bytearray: Compressed data.
195
196 Raises:
197 Exceptions occurring during compression.
198
199 OverflowError: raised if the source is too large for being compressed in
200 the given context.
201 LZ4StreamError: raised if the call to the LZ4 library fails.
202
203 """
204 return _compress(self._context, chunk)