126fa459cSmrg# Copyright 2016 The Brotli Authors. All rights reserved. 226fa459cSmrg# 326fa459cSmrg# Distributed under MIT license. 426fa459cSmrg# See file LICENSE for detail or copy at https://opensource.org/licenses/MIT 526fa459cSmrg 626fa459cSmrg"""Functions to compress and decompress data using the Brotli library.""" 726fa459cSmrg 826fa459cSmrgimport _brotli 926fa459cSmrg 1026fa459cSmrg 1126fa459cSmrg# The library version. 1226fa459cSmrg__version__ = _brotli.__version__ 1326fa459cSmrg 1426fa459cSmrg# The compression mode. 1526fa459cSmrgMODE_GENERIC = _brotli.MODE_GENERIC 1626fa459cSmrgMODE_TEXT = _brotli.MODE_TEXT 1726fa459cSmrgMODE_FONT = _brotli.MODE_FONT 1826fa459cSmrg 1926fa459cSmrg# The Compressor object. 2026fa459cSmrgCompressor = _brotli.Compressor 2126fa459cSmrg 2226fa459cSmrg# The Decompressor object. 2326fa459cSmrgDecompressor = _brotli.Decompressor 2426fa459cSmrg 2526fa459cSmrg# Compress a byte string. 2626fa459cSmrgdef compress(string, mode=MODE_GENERIC, quality=11, lgwin=22, lgblock=0): 2726fa459cSmrg """Compress a byte string. 2826fa459cSmrg 2926fa459cSmrg Args: 3026fa459cSmrg string (bytes): The input data. 3126fa459cSmrg mode (int, optional): The compression mode can be MODE_GENERIC (default), 3226fa459cSmrg MODE_TEXT (for UTF-8 format text input) or MODE_FONT (for WOFF 2.0). 3326fa459cSmrg quality (int, optional): Controls the compression-speed vs compression- 3426fa459cSmrg density tradeoff. The higher the quality, the slower the compression. 3526fa459cSmrg Range is 0 to 11. Defaults to 11. 3626fa459cSmrg lgwin (int, optional): Base 2 logarithm of the sliding window size. Range 3726fa459cSmrg is 10 to 24. Defaults to 22. 3826fa459cSmrg lgblock (int, optional): Base 2 logarithm of the maximum input block size. 3926fa459cSmrg Range is 16 to 24. If set to 0, the value will be set based on the 4026fa459cSmrg quality. Defaults to 0. 4126fa459cSmrg 4226fa459cSmrg Returns: 4326fa459cSmrg The compressed byte string. 4426fa459cSmrg 4526fa459cSmrg Raises: 4626fa459cSmrg brotli.error: If arguments are invalid, or compressor fails. 4726fa459cSmrg """ 4826fa459cSmrg compressor = Compressor(mode=mode, quality=quality, lgwin=lgwin, 4926fa459cSmrg lgblock=lgblock) 5026fa459cSmrg return compressor.process(string) + compressor.finish() 5126fa459cSmrg 5226fa459cSmrg# Decompress a compressed byte string. 5326fa459cSmrgdecompress = _brotli.decompress 5426fa459cSmrg 5526fa459cSmrg# Raised if compression or decompression fails. 5626fa459cSmrgerror = _brotli.error 57