Skip to content

Commit 01d7586

Browse files
committed
python: add type stubs
1 parent d4e1064 commit 01d7586

File tree

3 files changed

+206
-22
lines changed

3 files changed

+206
-22
lines changed

python/brotli/__init__.py

Lines changed: 20 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -5,21 +5,26 @@
55

66
"""Functions to compress and decompress data using the Brotli library."""
77

8-
import _brotli
8+
from ._brotli import (
9+
# The library version.
10+
__version__,
11+
# The compression mode.
12+
MODE_GENERIC,
13+
MODE_TEXT,
14+
MODE_FONT,
15+
# The Compressor object.
16+
Compressor,
17+
# The Decompressor object.
18+
Decompressor,
19+
# Decompress a compressed byte string.
20+
decompress,
21+
# Raised if compression or decompression fails.
22+
error,
23+
)
24+
25+
26+
version = __version__
927

10-
# The library version.
11-
version = __version__ = _brotli.__version__
12-
13-
# The compression mode.
14-
MODE_GENERIC = _brotli.MODE_GENERIC
15-
MODE_TEXT = _brotli.MODE_TEXT
16-
MODE_FONT = _brotli.MODE_FONT
17-
18-
# The Compressor object.
19-
Compressor = _brotli.Compressor
20-
21-
# The Decompressor object.
22-
Decompressor = _brotli.Decompressor
2328

2429
# Compress a byte string.
2530
def compress(string, mode=MODE_GENERIC, quality=11, lgwin=22, lgblock=0):
@@ -46,12 +51,5 @@ def compress(string, mode=MODE_GENERIC, quality=11, lgwin=22, lgblock=0):
4651
Raises:
4752
brotli.error: If arguments are invalid, or compressor fails.
4853
"""
49-
compressor = Compressor(mode=mode, quality=quality, lgwin=lgwin,
50-
lgblock=lgblock)
54+
compressor = Compressor(mode=mode, quality=quality, lgwin=lgwin, lgblock=lgblock)
5155
return compressor.process(string) + compressor.finish()
52-
53-
# Decompress a compressed byte string.
54-
decompress = _brotli.decompress
55-
56-
# Raised if compression or decompression fails.
57-
error = _brotli.error

python/brotli/__init__.pyi

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

python/brotli/py.typed

Whitespace-only changes.

0 commit comments

Comments
 (0)