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