blob: a88616ef835b68cf20ed348796beb426e3d92981 [file] [log] [blame]
Alex Nicksayf7b5b3d2016-09-28 17:26:00 -04001# Copyright 2016 The Brotli Authors. All rights reserved.
2#
3# Distributed under MIT license.
4# See file LICENSE for detail or copy at https://opensource.org/licenses/MIT
5
6"""Functions to compress and decompress data using the Brotli library."""
7
8import _brotli
9
10
11# The library version.
12__version__ = _brotli.__version__
13
14# The compression mode.
15MODE_GENERIC = _brotli.MODE_GENERIC
16MODE_TEXT = _brotli.MODE_TEXT
17MODE_FONT = _brotli.MODE_FONT
18
19# Compress a byte string.
Alex Nicksay595a5242016-09-29 15:14:16 -040020def compress(string, mode=MODE_GENERIC, quality=11, lgwin=22, lgblock=0,
21 dictionary=''):
22 """Compress a byte string.
23
24 Args:
25 string (bytes): The input data.
26 mode (int, optional): The compression mode can be MODE_GENERIC (default),
27 MODE_TEXT (for UTF-8 format text input) or MODE_FONT (for WOFF 2.0).
28 quality (int, optional): Controls the compression-speed vs compression-
29 density tradeoff. The higher the quality, the slower the compression.
30 Range is 0 to 11. Defaults to 11.
31 lgwin (int, optional): Base 2 logarithm of the sliding window size. Range
32 is 10 to 24. Defaults to 22.
33 lgblock (int, optional): Base 2 logarithm of the maximum input block size.
34 Range is 16 to 24. If set to 0, the value will be set based on the
35 quality. Defaults to 0.
36 dictionary (bytes, optional): Custom dictionary. Only last sliding window
Alex Nicksay56323152016-10-24 07:28:56 -040037 size bytes will be used.
Alex Nicksay595a5242016-09-29 15:14:16 -040038
39 Returns:
40 The compressed byte string.
41
42 Raises:
43 brotli.error: If arguments are invalid, or compressor fails.
44 """
45 compressor = _brotli.Compressor(mode=mode, quality=quality, lgwin=lgwin,
46 lgblock=lgblock, dictionary=dictionary)
Alex Nicksay56323152016-10-24 07:28:56 -040047 return compressor.process(string) + compressor.finish()
Alex Nicksayf7b5b3d2016-09-28 17:26:00 -040048
49# Decompress a compressed byte string.
50decompress = _brotli.decompress
51
52# Raised if compression or decompression fails.
53error = _brotli.error