]>
Commit | Line | Data |
---|---|---|
3257aa99 DM |
1 | """ Python 'bz2_codec' Codec - bz2 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 | Adapted by Raymond Hettinger from zlib_codec.py which was written\r | |
7 | by Marc-Andre Lemburg (mal@lemburg.com).\r | |
8 | \r | |
9 | """\r | |
10 | import codecs\r | |
11 | import bz2 # this codec needs the optional bz2 module !\r | |
12 | \r | |
13 | ### Codec APIs\r | |
14 | \r | |
15 | def bz2_encode(input,errors='strict'):\r | |
16 | \r | |
17 | """ Encodes the object input and returns a tuple (output\r | |
18 | object, length consumed).\r | |
19 | \r | |
20 | errors defines the error handling to apply. It defaults to\r | |
21 | 'strict' handling which is the only currently supported\r | |
22 | error handling for this codec.\r | |
23 | \r | |
24 | """\r | |
25 | assert errors == 'strict'\r | |
26 | output = bz2.compress(input)\r | |
27 | return (output, len(input))\r | |
28 | \r | |
29 | def bz2_decode(input,errors='strict'):\r | |
30 | \r | |
31 | """ Decodes the object input and returns a tuple (output\r | |
32 | object, length consumed).\r | |
33 | \r | |
34 | input must be an object which provides the bf_getreadbuf\r | |
35 | buffer slot. Python strings, buffer objects and memory\r | |
36 | mapped files are examples of objects providing this slot.\r | |
37 | \r | |
38 | errors defines the error handling to apply. It defaults to\r | |
39 | 'strict' handling which is the only currently supported\r | |
40 | error handling for this codec.\r | |
41 | \r | |
42 | """\r | |
43 | assert errors == 'strict'\r | |
44 | output = bz2.decompress(input)\r | |
45 | return (output, len(input))\r | |
46 | \r | |
47 | class Codec(codecs.Codec):\r | |
48 | \r | |
49 | def encode(self, input, errors='strict'):\r | |
50 | return bz2_encode(input, errors)\r | |
51 | def decode(self, input, errors='strict'):\r | |
52 | return bz2_decode(input, errors)\r | |
53 | \r | |
54 | class IncrementalEncoder(codecs.IncrementalEncoder):\r | |
55 | def __init__(self, errors='strict'):\r | |
56 | assert errors == 'strict'\r | |
57 | self.errors = errors\r | |
58 | self.compressobj = bz2.BZ2Compressor()\r | |
59 | \r | |
60 | def encode(self, input, final=False):\r | |
61 | if final:\r | |
62 | c = self.compressobj.compress(input)\r | |
63 | return c + self.compressobj.flush()\r | |
64 | else:\r | |
65 | return self.compressobj.compress(input)\r | |
66 | \r | |
67 | def reset(self):\r | |
68 | self.compressobj = bz2.BZ2Compressor()\r | |
69 | \r | |
70 | class IncrementalDecoder(codecs.IncrementalDecoder):\r | |
71 | def __init__(self, errors='strict'):\r | |
72 | assert errors == 'strict'\r | |
73 | self.errors = errors\r | |
74 | self.decompressobj = bz2.BZ2Decompressor()\r | |
75 | \r | |
76 | def decode(self, input, final=False):\r | |
77 | try:\r | |
78 | return self.decompressobj.decompress(input)\r | |
79 | except EOFError:\r | |
80 | return ''\r | |
81 | \r | |
82 | def reset(self):\r | |
83 | self.decompressobj = bz2.BZ2Decompressor()\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="bz2",\r | |
96 | encode=bz2_encode,\r | |
97 | decode=bz2_decode,\r | |
98 | incrementalencoder=IncrementalEncoder,\r | |
99 | incrementaldecoder=IncrementalDecoder,\r | |
100 | streamwriter=StreamWriter,\r | |
101 | streamreader=StreamReader,\r | |
102 | )\r |