|
| 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 | +""" |
| 7 | +Functions to compress and decompress data using the Brotli library. |
| 8 | +""" |
| 9 | + |
| 10 | +from typing import Union |
| 11 | + |
| 12 | +ByteString = Union[bytes, bytearray, memoryview] |
| 13 | + |
| 14 | +version: str |
| 15 | +__version__: str |
| 16 | + |
| 17 | +MODE_GENERIC: int = 0 |
| 18 | +MODE_TEXT: int = 1 |
| 19 | +MODE_FONT: int = 2 |
| 20 | + |
| 21 | +class Compressor: |
| 22 | + """An object to compress a byte string.""" |
| 23 | + |
| 24 | + def __init__( |
| 25 | + self, mode=MODE_GENERIC, quality: int = 11, lgwin: int = 22, lgblock: int = 0 |
| 26 | + ): |
| 27 | + """ |
| 28 | + Args: |
| 29 | + mode (int, optional): The compression mode can be |
| 30 | + - MODE_GENERIC (default), |
| 31 | + - MODE_TEXT (for UTF-8 format text input) |
| 32 | + - MODE_FONT (for WOFF 2.0) |
| 33 | + quality (int, optional): Controls the compression-speed vs compression-density tradeoff. |
| 34 | + The higher the quality, the slower the compression. |
| 35 | + Range is 0 to 11. |
| 36 | + Defaults to 11. |
| 37 | + lgwin (int, optional): Base 2 logarithm of the sliding window size. |
| 38 | + Range is 10 to 24. |
| 39 | + Defaults to 22. |
| 40 | + lgblock (int, optional): Base 2 logarithm of the maximum input block size. |
| 41 | + Range is 16 to 24. |
| 42 | + If set to 0, the value will be set based on the quality. |
| 43 | + Defaults to 0. |
| 44 | +
|
| 45 | + Raises: |
| 46 | + brotli.error: If arguments are invalid. |
| 47 | + """ |
| 48 | + |
| 49 | + def process(self, string: ByteString) -> bytes: |
| 50 | + """Process "string" for compression, returning a string that contains |
| 51 | + compressed output data. This data should be concatenated to the output |
| 52 | + produced by any preceding calls to the "process()" or "flush()" methods. |
| 53 | + Some or all of the input may be kept in internal buffers for later |
| 54 | + processing, and the compressed output data may be empty until enough input |
| 55 | + has been accumulated. |
| 56 | +
|
| 57 | + Args: |
| 58 | + string (bytes): The input data |
| 59 | +
|
| 60 | + Returns: |
| 61 | + The compressed output data (bytes) |
| 62 | +
|
| 63 | + Raises: |
| 64 | + brotli.error: If compression fails |
| 65 | + """ |
| 66 | + ... |
| 67 | + |
| 68 | + def flush(self) -> bytes: |
| 69 | + """Process all pending input, returning a string containing the remaining |
| 70 | + compressed data. This data should be concatenated to the output produced by |
| 71 | + any preceding calls to the "process()" or "flush()" methods. |
| 72 | +
|
| 73 | + Returns: |
| 74 | + The compressed output data (bytes) |
| 75 | +
|
| 76 | + Raises: |
| 77 | + brotli.error: If compression fails |
| 78 | + """ |
| 79 | + ... |
| 80 | + |
| 81 | + def finish(self) -> bytes: |
| 82 | + """Process all pending input and complete all compression, returning a string |
| 83 | + containing the remaining compressed data. This data should be concatenated |
| 84 | + to the output produced by any preceding calls to the "process()" or "flush()" methods. |
| 85 | + After calling "finish()", the "process()" and "flush()" methods |
| 86 | + cannot be called again, and a new "Compressor" object should be created. |
| 87 | +
|
| 88 | + Returns: |
| 89 | + The compressed output data (bytes) |
| 90 | +
|
| 91 | + Raises: |
| 92 | + brotli.error: If compression fails |
| 93 | + """ |
| 94 | + ... |
| 95 | + |
| 96 | +class Decompressor: |
| 97 | + """An object to decompress a byte string.""" |
| 98 | + |
| 99 | + def __init__(self): ... |
| 100 | + def is_finished(self) -> bool: |
| 101 | + """Checks if decoder instance reached the final state. |
| 102 | +
|
| 103 | + Returns: |
| 104 | + True if the decoder is in a state where it reached the end of the input |
| 105 | + and produced all of the output |
| 106 | + False otherwise |
| 107 | +
|
| 108 | + Raises: |
| 109 | + brotli.error: If decompression fails |
| 110 | + """ |
| 111 | + ... |
| 112 | + |
| 113 | + def process(self, string: ByteString) -> bytes: |
| 114 | + """Process "string" for decompression, returning a string that contains |
| 115 | + decompressed output data. This data should be concatenated to the output |
| 116 | + produced by any preceding calls to the "process()" method. |
| 117 | + Some or all of the input may be kept in internal buffers for later |
| 118 | + processing, and the decompressed output data may be empty until enough input |
| 119 | + has been accumulated. |
| 120 | +
|
| 121 | + Args: |
| 122 | + string (bytes): The input data |
| 123 | +
|
| 124 | + Returns: |
| 125 | + The decompressed output data (bytes) |
| 126 | +
|
| 127 | + Raises: |
| 128 | + brotli.error: If decompression fails |
| 129 | + """ |
| 130 | + ... |
| 131 | + |
| 132 | +def compress( |
| 133 | + string: ByteString, |
| 134 | + mode: int = MODE_GENERIC, |
| 135 | + quality: int = 11, |
| 136 | + lgwin: int = 22, |
| 137 | + lgblock: int = 0, |
| 138 | +): |
| 139 | + """Compress a byte string. |
| 140 | +
|
| 141 | + Args: |
| 142 | + string (bytes): The input data. |
| 143 | + mode (int, optional): The compression mode can be |
| 144 | + - MODE_GENERIC (default), |
| 145 | + - MODE_TEXT (for UTF-8 format text input) |
| 146 | + - MODE_FONT (for WOFF 2.0) |
| 147 | + quality (int, optional): Controls the compression-speed vs compression-density tradeoff. |
| 148 | + The higher the quality, the slower the compression. |
| 149 | + Range is 0 to 11. |
| 150 | + Defaults to 11. |
| 151 | + lgwin (int, optional): Base 2 logarithm of the sliding window size. |
| 152 | + Range is 10 to 24. |
| 153 | + Defaults to 22. |
| 154 | + lgblock (int, optional): Base 2 logarithm of the maximum input block size. |
| 155 | + Range is 16 to 24. |
| 156 | + If set to 0, the value will be set based on the quality. |
| 157 | + Defaults to 0. |
| 158 | +
|
| 159 | + Returns: |
| 160 | + The compressed byte string. |
| 161 | +
|
| 162 | + Raises: |
| 163 | + brotli.error: If arguments are invalid, or compressor fails. |
| 164 | + """ |
| 165 | + ... |
| 166 | + |
| 167 | +def decompress(string: bytes) -> bytes: |
| 168 | + """ |
| 169 | + Decompress a compressed byte string. |
| 170 | +
|
| 171 | + Args: |
| 172 | + string (bytes): The compressed input data. |
| 173 | +
|
| 174 | + Returns: |
| 175 | + The decompressed byte string. |
| 176 | +
|
| 177 | + Raises: |
| 178 | + brotli.error: If decompressor fails. |
| 179 | + """ |
| 180 | + ... |
| 181 | + |
| 182 | +class error(Exception): ... |
0 commit comments