jpayne@68
|
1 """Python 'zlib_codec' Codec - zlib compression encoding.
|
jpayne@68
|
2
|
jpayne@68
|
3 This codec de/encodes from bytes to bytes.
|
jpayne@68
|
4
|
jpayne@68
|
5 Written by Marc-Andre Lemburg (mal@lemburg.com).
|
jpayne@68
|
6 """
|
jpayne@68
|
7
|
jpayne@68
|
8 import codecs
|
jpayne@68
|
9 import zlib # this codec needs the optional zlib module !
|
jpayne@68
|
10
|
jpayne@68
|
11 ### Codec APIs
|
jpayne@68
|
12
|
jpayne@68
|
13 def zlib_encode(input, errors='strict'):
|
jpayne@68
|
14 assert errors == 'strict'
|
jpayne@68
|
15 return (zlib.compress(input), len(input))
|
jpayne@68
|
16
|
jpayne@68
|
17 def zlib_decode(input, errors='strict'):
|
jpayne@68
|
18 assert errors == 'strict'
|
jpayne@68
|
19 return (zlib.decompress(input), len(input))
|
jpayne@68
|
20
|
jpayne@68
|
21 class Codec(codecs.Codec):
|
jpayne@68
|
22 def encode(self, input, errors='strict'):
|
jpayne@68
|
23 return zlib_encode(input, errors)
|
jpayne@68
|
24 def decode(self, input, errors='strict'):
|
jpayne@68
|
25 return zlib_decode(input, errors)
|
jpayne@68
|
26
|
jpayne@68
|
27 class IncrementalEncoder(codecs.IncrementalEncoder):
|
jpayne@68
|
28 def __init__(self, errors='strict'):
|
jpayne@68
|
29 assert errors == 'strict'
|
jpayne@68
|
30 self.errors = errors
|
jpayne@68
|
31 self.compressobj = zlib.compressobj()
|
jpayne@68
|
32
|
jpayne@68
|
33 def encode(self, input, final=False):
|
jpayne@68
|
34 if final:
|
jpayne@68
|
35 c = self.compressobj.compress(input)
|
jpayne@68
|
36 return c + self.compressobj.flush()
|
jpayne@68
|
37 else:
|
jpayne@68
|
38 return self.compressobj.compress(input)
|
jpayne@68
|
39
|
jpayne@68
|
40 def reset(self):
|
jpayne@68
|
41 self.compressobj = zlib.compressobj()
|
jpayne@68
|
42
|
jpayne@68
|
43 class IncrementalDecoder(codecs.IncrementalDecoder):
|
jpayne@68
|
44 def __init__(self, errors='strict'):
|
jpayne@68
|
45 assert errors == 'strict'
|
jpayne@68
|
46 self.errors = errors
|
jpayne@68
|
47 self.decompressobj = zlib.decompressobj()
|
jpayne@68
|
48
|
jpayne@68
|
49 def decode(self, input, final=False):
|
jpayne@68
|
50 if final:
|
jpayne@68
|
51 c = self.decompressobj.decompress(input)
|
jpayne@68
|
52 return c + self.decompressobj.flush()
|
jpayne@68
|
53 else:
|
jpayne@68
|
54 return self.decompressobj.decompress(input)
|
jpayne@68
|
55
|
jpayne@68
|
56 def reset(self):
|
jpayne@68
|
57 self.decompressobj = zlib.decompressobj()
|
jpayne@68
|
58
|
jpayne@68
|
59 class StreamWriter(Codec, codecs.StreamWriter):
|
jpayne@68
|
60 charbuffertype = bytes
|
jpayne@68
|
61
|
jpayne@68
|
62 class StreamReader(Codec, codecs.StreamReader):
|
jpayne@68
|
63 charbuffertype = bytes
|
jpayne@68
|
64
|
jpayne@68
|
65 ### encodings module API
|
jpayne@68
|
66
|
jpayne@68
|
67 def getregentry():
|
jpayne@68
|
68 return codecs.CodecInfo(
|
jpayne@68
|
69 name='zlib',
|
jpayne@68
|
70 encode=zlib_encode,
|
jpayne@68
|
71 decode=zlib_decode,
|
jpayne@68
|
72 incrementalencoder=IncrementalEncoder,
|
jpayne@68
|
73 incrementaldecoder=IncrementalDecoder,
|
jpayne@68
|
74 streamreader=StreamReader,
|
jpayne@68
|
75 streamwriter=StreamWriter,
|
jpayne@68
|
76 _is_text_encoding=False,
|
jpayne@68
|
77 )
|