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