| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
"""Functions to compress and decompress data using the Brotli library.""" |
| 7 |
|
| 8 |
import _brotli |
| 9 |
|
| 10 |
|
| 11 |
version = __version__ = _brotli.__version__ |
| 12 |
|
| 13 |
|
| 14 |
MODE_GENERIC = _brotli.MODE_GENERIC |
| 15 |
MODE_TEXT = _brotli.MODE_TEXT |
| 16 |
MODE_FONT = _brotli.MODE_FONT |
| 17 |
|
| 18 |
|
| 19 |
Compressor = _brotli.Compressor |
| 20 |
|
| 21 |
|
| 22 |
Decompressor = _brotli.Decompressor |
| 23 |
|
| 24 |
|
| 25 |
def compress(string, mode=MODE_GENERIC, quality=11, lgwin=22, lgblock=0): |
| 26 |
"""Compress a byte string. |
| 27 |
|
| 28 |
Args: |
| 29 |
string (bytes): The input data. |
| 30 |
mode (int, optional): The compression mode; value 0 should be used for |
| 31 |
generic input (MODE_GENERIC); value 1 might be beneficial for UTF-8 text |
| 32 |
input (MODE_TEXT); value 2 tunes encoder for WOFF 2.0 data (MODE_FONT). |
| 33 |
Defaults to 0. |
| 34 |
quality (int, optional): Controls the compression-speed vs compression- |
| 35 |
density tradeoff. The higher the quality, the slower the compression. |
| 36 |
Range is 0 to 11. Defaults to 11. |
| 37 |
lgwin (int, optional): Base 2 logarithm of the sliding window size. Range |
| 38 |
is 10 to 24. Defaults to 22. |
| 39 |
lgblock (int, optional): Base 2 logarithm of the maximum input block size. |
| 40 |
Range is 16 to 24. If set to 0, the value will be set based on the |
| 41 |
quality. Defaults to 0. |
| 42 |
|
| 43 |
Returns: |
| 44 |
The compressed byte string. |
| 45 |
|
| 46 |
Raises: |
| 47 |
brotli.error: If arguments are invalid, or compressor fails. |
| 48 |
""" |
| 49 |
compressor = Compressor(mode=mode, quality=quality, lgwin=lgwin, |
| 50 |
lgblock=lgblock) |
| 51 |
return compressor.process(string) + compressor.finish() |
| 52 |
|
| 53 |
|
| 54 |
decompress = _brotli.decompress |
| 55 |
|
| 56 |
|
| 57 |
error = _brotli.error |