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