]>
Commit | Line | Data |
---|---|---|
4710c53d | 1 | """ Python 'zlib_codec' Codec - zlib compression encoding\r |
2 | \r | |
3 | Unlike most of the other codecs which target Unicode, this codec\r | |
4 | will return Python string objects for both encode and decode.\r | |
5 | \r | |
6 | Written by Marc-Andre Lemburg (mal@lemburg.com).\r | |
7 | \r | |
8 | """\r | |
9 | import codecs\r | |
10 | import zlib # this codec needs the optional zlib module !\r | |
11 | \r | |
12 | ### Codec APIs\r | |
13 | \r | |
14 | def zlib_encode(input,errors='strict'):\r | |
15 | \r | |
16 | """ Encodes the object input and returns a tuple (output\r | |
17 | object, length consumed).\r | |
18 | \r | |
19 | errors defines the error handling to apply. It defaults to\r | |
20 | 'strict' handling which is the only currently supported\r | |
21 | error handling for this codec.\r | |
22 | \r | |
23 | """\r | |
24 | assert errors == 'strict'\r | |
25 | output = zlib.compress(input)\r | |
26 | return (output, len(input))\r | |
27 | \r | |
28 | def zlib_decode(input,errors='strict'):\r | |
29 | \r | |
30 | """ Decodes the object input and returns a tuple (output\r | |
31 | object, length consumed).\r | |
32 | \r | |
33 | input must be an object which provides the bf_getreadbuf\r | |
34 | buffer slot. Python strings, buffer objects and memory\r | |
35 | mapped files are examples of objects providing this slot.\r | |
36 | \r | |
37 | errors defines the error handling to apply. It defaults to\r | |
38 | 'strict' handling which is the only currently supported\r | |
39 | error handling for this codec.\r | |
40 | \r | |
41 | """\r | |
42 | assert errors == 'strict'\r | |
43 | output = zlib.decompress(input)\r | |
44 | return (output, len(input))\r | |
45 | \r | |
46 | class Codec(codecs.Codec):\r | |
47 | \r | |
48 | def encode(self, input, errors='strict'):\r | |
49 | return zlib_encode(input, errors)\r | |
50 | def decode(self, input, errors='strict'):\r | |
51 | return zlib_decode(input, errors)\r | |
52 | \r | |
53 | class IncrementalEncoder(codecs.IncrementalEncoder):\r | |
54 | def __init__(self, errors='strict'):\r | |
55 | assert errors == 'strict'\r | |
56 | self.errors = errors\r | |
57 | self.compressobj = zlib.compressobj()\r | |
58 | \r | |
59 | def encode(self, input, final=False):\r | |
60 | if final:\r | |
61 | c = self.compressobj.compress(input)\r | |
62 | return c + self.compressobj.flush()\r | |
63 | else:\r | |
64 | return self.compressobj.compress(input)\r | |
65 | \r | |
66 | def reset(self):\r | |
67 | self.compressobj = zlib.compressobj()\r | |
68 | \r | |
69 | class IncrementalDecoder(codecs.IncrementalDecoder):\r | |
70 | def __init__(self, errors='strict'):\r | |
71 | assert errors == 'strict'\r | |
72 | self.errors = errors\r | |
73 | self.decompressobj = zlib.decompressobj()\r | |
74 | \r | |
75 | def decode(self, input, final=False):\r | |
76 | if final:\r | |
77 | c = self.decompressobj.decompress(input)\r | |
78 | return c + self.decompressobj.flush()\r | |
79 | else:\r | |
80 | return self.decompressobj.decompress(input)\r | |
81 | \r | |
82 | def reset(self):\r | |
83 | self.decompressobj = zlib.decompressobj()\r | |
84 | \r | |
85 | class StreamWriter(Codec,codecs.StreamWriter):\r | |
86 | pass\r | |
87 | \r | |
88 | class StreamReader(Codec,codecs.StreamReader):\r | |
89 | pass\r | |
90 | \r | |
91 | ### encodings module API\r | |
92 | \r | |
93 | def getregentry():\r | |
94 | return codecs.CodecInfo(\r | |
95 | name='zlib',\r | |
96 | encode=zlib_encode,\r | |
97 | decode=zlib_decode,\r | |
98 | incrementalencoder=IncrementalEncoder,\r | |
99 | incrementaldecoder=IncrementalDecoder,\r | |
100 | streamreader=StreamReader,\r | |
101 | streamwriter=StreamWriter,\r | |
102 | )\r |