1# 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# The library version. 11version = __version__ = _brotli.__version__ 12 13# The compression mode. 14MODE_GENERIC = _brotli.MODE_GENERIC 15MODE_TEXT = _brotli.MODE_TEXT 16MODE_FONT = _brotli.MODE_FONT 17 18# The Compressor object. 19Compressor = _brotli.Compressor 20 21# The Decompressor object. 22Decompressor = _brotli.Decompressor 23 24# Compress a byte string. 25def 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 can be MODE_GENERIC (default), 31 MODE_TEXT (for UTF-8 format text input) or MODE_FONT (for WOFF 2.0). 32 quality (int, optional): Controls the compression-speed vs compression- 33 density tradeoff. The higher the quality, the slower the compression. 34 Range is 0 to 11. Defaults to 11. 35 lgwin (int, optional): Base 2 logarithm of the sliding window size. Range 36 is 10 to 24. Defaults to 22. 37 lgblock (int, optional): Base 2 logarithm of the maximum input block size. 38 Range is 16 to 24. If set to 0, the value will be set based on the 39 quality. Defaults to 0. 40 41 Returns: 42 The compressed byte string. 43 44 Raises: 45 brotli.error: If arguments are invalid, or compressor fails. 46 """ 47 compressor = Compressor(mode=mode, quality=quality, lgwin=lgwin, 48 lgblock=lgblock) 49 return compressor.process(string) + compressor.finish() 50 51# Decompress a compressed byte string. 52decompress = _brotli.decompress 53 54# Raised if compression or decompression fails. 55error = _brotli.error 56