39
39
assert "some data" == snappy.uncompress(compressed)
40
40
41
41
"""
42
- from __future__ import absolute_import
42
+ from __future__ import absolute_import , annotations
43
+
44
+ from typing import (
45
+ Optional , Union , IO , BinaryIO , Protocol , Type , Any , overload ,
46
+ )
43
47
44
48
import cramjam
45
49
@@ -57,7 +61,7 @@ class UncompressError(Exception):
57
61
pass
58
62
59
63
60
- def isValidCompressed (data ) :
64
+ def isValidCompressed (data : Union [ str , bytes ]) -> bool :
61
65
if isinstance (data , str ):
62
66
data = data .encode ('utf-8' )
63
67
@@ -69,12 +73,18 @@ def isValidCompressed(data):
69
73
return ok
70
74
71
75
72
- def compress (data , encoding = 'utf-8' ):
76
+ def compress (data : Union [ str , bytes ], encoding : str = 'utf-8' ) -> bytes :
73
77
if isinstance (data , str ):
74
78
data = data .encode (encoding )
75
79
76
80
return bytes (_compress (data ))
77
81
82
+ @overload
83
+ def uncompress (data : bytes ) -> bytes : ...
84
+
85
+ @overload
86
+ def uncompress (data : bytes , decoding : Optional [str ] = None ) -> Union [str , bytes ]: ...
87
+
78
88
def uncompress (data , decoding = None ):
79
89
if isinstance (data , str ):
80
90
raise UncompressError ("It's only possible to uncompress bytes" )
@@ -89,6 +99,16 @@ def uncompress(data, decoding=None):
89
99
90
100
decompress = uncompress
91
101
102
+
103
+ class Compressor (Protocol ):
104
+ def add_chunk (self , data ) -> Any : ...
105
+
106
+
107
+ class Decompressor (Protocol ):
108
+ def decompress (self , data ) -> Any : ...
109
+ def flush (self ): ...
110
+
111
+
92
112
class StreamCompressor ():
93
113
94
114
"""This class implements the compressor-side of the proposed Snappy framing
@@ -109,7 +129,7 @@ class StreamCompressor():
109
129
def __init__ (self ):
110
130
self .c = cramjam .snappy .Compressor ()
111
131
112
- def add_chunk (self , data : bytes , compress = None ):
132
+ def add_chunk (self , data : bytes , compress = None ) -> bytes :
113
133
"""Add a chunk, returning a string that is framed and compressed.
114
134
115
135
Outputs a single snappy chunk; if it is the very start of the stream,
@@ -120,10 +140,10 @@ def add_chunk(self, data: bytes, compress=None):
120
140
121
141
compress = add_chunk
122
142
123
- def flush (self ):
143
+ def flush (self ) -> bytes :
124
144
return bytes (self .c .flush ())
125
145
126
- def copy (self ):
146
+ def copy (self ) -> 'StreamCompressor' :
127
147
"""This method exists for compatibility with the zlib compressobj.
128
148
"""
129
149
return self
@@ -157,7 +177,7 @@ def check_format(fin):
157
177
except :
158
178
return False
159
179
160
- def decompress (self , data : bytes ):
180
+ def decompress (self , data : bytes ) -> bytes :
161
181
"""Decompress 'data', returning a string containing the uncompressed
162
182
data corresponding to at least part of the data in string. This data
163
183
should be concatenated to the output produced by any preceding calls to
@@ -189,15 +209,15 @@ def decompress(self, data: bytes):
189
209
self .c .decompress (data )
190
210
return self .flush ()
191
211
192
- def flush (self ):
212
+ def flush (self ) -> bytes :
193
213
return bytes (self .c .flush ())
194
214
195
- def copy (self ):
215
+ def copy (self ) -> 'StreamDecompressor' :
196
216
return self
197
217
198
218
199
219
class HadoopStreamCompressor ():
200
- def add_chunk (self , data : bytes , compress = None ):
220
+ def add_chunk (self , data : bytes , compress = None ) -> bytes :
201
221
"""Add a chunk, returning a string that is framed and compressed.
202
222
203
223
Outputs a single snappy chunk; if it is the very start of the stream,
@@ -208,11 +228,11 @@ def add_chunk(self, data: bytes, compress=None):
208
228
209
229
compress = add_chunk
210
230
211
- def flush (self ):
231
+ def flush (self ) -> bytes :
212
232
# never maintains a buffer
213
233
return b""
214
234
215
- def copy (self ):
235
+ def copy (self ) -> 'HadoopStreamCompressor' :
216
236
"""This method exists for compatibility with the zlib compressobj.
217
237
"""
218
238
return self
@@ -239,7 +259,7 @@ def check_format(fin):
239
259
except :
240
260
return False
241
261
242
- def decompress (self , data : bytes ):
262
+ def decompress (self , data : bytes ) -> bytes :
243
263
"""Decompress 'data', returning a string containing the uncompressed
244
264
data corresponding to at least part of the data in string. This data
245
265
should be concatenated to the output produced by any preceding calls to
@@ -262,18 +282,18 @@ def decompress(self, data: bytes):
262
282
data = data [8 + chunk_length :]
263
283
return b"" .join (out )
264
284
265
- def flush (self ):
285
+ def flush (self ) -> bytes :
266
286
return b""
267
287
268
- def copy (self ):
288
+ def copy (self ) -> 'HadoopStreamDecompressor' :
269
289
return self
270
290
271
291
272
292
273
- def stream_compress (src ,
274
- dst ,
275
- blocksize = _STREAM_TO_STREAM_BLOCK_SIZE ,
276
- compressor_cls = StreamCompressor ):
293
+ def stream_compress (src : IO ,
294
+ dst : IO ,
295
+ blocksize : int = _STREAM_TO_STREAM_BLOCK_SIZE ,
296
+ compressor_cls : Type [ Compressor ] = StreamCompressor ) -> None :
277
297
"""Takes an incoming file-like object and an outgoing file-like object,
278
298
reads data from src, compresses it, and writes it to dst. 'src' should
279
299
support the read method, and 'dst' should support the write method.
@@ -288,11 +308,11 @@ def stream_compress(src,
288
308
if buf : dst .write (buf )
289
309
290
310
291
- def stream_decompress (src ,
292
- dst ,
293
- blocksize = _STREAM_TO_STREAM_BLOCK_SIZE ,
294
- decompressor_cls = StreamDecompressor ,
295
- start_chunk = None ):
311
+ def stream_decompress (src : IO ,
312
+ dst : IO ,
313
+ blocksize : int = _STREAM_TO_STREAM_BLOCK_SIZE ,
314
+ decompressor_cls : Type [ Decompressor ] = StreamDecompressor ,
315
+ start_chunk = None ) -> None :
296
316
"""Takes an incoming file-like object and an outgoing file-like object,
297
317
reads data from src, decompresses it, and writes it to dst. 'src' should
298
318
support the read method, and 'dst' should support the write method.
@@ -317,10 +337,10 @@ def stream_decompress(src,
317
337
318
338
319
339
def hadoop_stream_decompress (
320
- src ,
321
- dst ,
322
- blocksize = _STREAM_TO_STREAM_BLOCK_SIZE ,
323
- ):
340
+ src : BinaryIO ,
341
+ dst : BinaryIO ,
342
+ blocksize : int = _STREAM_TO_STREAM_BLOCK_SIZE ,
343
+ ) -> None :
324
344
c = HadoopStreamDecompressor ()
325
345
while True :
326
346
data = src .read (blocksize )
@@ -333,10 +353,10 @@ def hadoop_stream_decompress(
333
353
334
354
335
355
def hadoop_stream_compress (
336
- src ,
337
- dst ,
338
- blocksize = _STREAM_TO_STREAM_BLOCK_SIZE ,
339
- ):
356
+ src : BinaryIO ,
357
+ dst : BinaryIO ,
358
+ blocksize : int = _STREAM_TO_STREAM_BLOCK_SIZE ,
359
+ ) -> None :
340
360
c = HadoopStreamCompressor ()
341
361
while True :
342
362
data = src .read (blocksize )
@@ -348,11 +368,11 @@ def hadoop_stream_compress(
348
368
dst .flush ()
349
369
350
370
351
- def raw_stream_decompress (src , dst ) :
371
+ def raw_stream_decompress (src : BinaryIO , dst : BinaryIO ) -> None :
352
372
data = src .read ()
353
373
dst .write (decompress (data ))
354
374
355
375
356
- def raw_stream_compress (src , dst ) :
376
+ def raw_stream_compress (src : BinaryIO , dst : BinaryIO ) -> None :
357
377
data = src .read ()
358
378
dst .write (compress (data ))
0 commit comments