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