Marc-André Lemburg | 2d92041 | 2001-05-15 12:00:02 +0000 | [diff] [blame] | 1 | """ Python 'zlib_codec' Codec - zlib 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 | |
| 6 | Written by Marc-Andre Lemburg (mal@lemburg.com). |
| 7 | |
| 8 | """ |
| 9 | import codecs |
| 10 | import zlib # this codec needs the optional zlib module ! |
| 11 | |
| 12 | ### Codec APIs |
| 13 | |
| 14 | def zlib_encode(input,errors='strict'): |
| 15 | |
| 16 | """ Encodes the object input and returns a tuple (output |
| 17 | object, length consumed). |
| 18 | |
| 19 | errors defines the error handling to apply. It defaults to |
| 20 | 'strict' handling which is the only currently supported |
| 21 | error handling for this codec. |
| 22 | |
| 23 | """ |
| 24 | assert errors == 'strict' |
| 25 | output = zlib.compress(input) |
| 26 | return (output, len(input)) |
| 27 | |
| 28 | def zlib_decode(input,errors='strict'): |
| 29 | |
| 30 | """ Decodes the object input and returns a tuple (output |
| 31 | object, length consumed). |
| 32 | |
| 33 | input must be an object which provides the bf_getreadbuf |
| 34 | buffer slot. Python strings, buffer objects and memory |
| 35 | mapped files are examples of objects providing this slot. |
| 36 | |
| 37 | errors defines the error handling to apply. It defaults to |
| 38 | 'strict' handling which is the only currently supported |
| 39 | error handling for this codec. |
| 40 | |
| 41 | """ |
| 42 | assert errors == 'strict' |
| 43 | output = zlib.decompress(input) |
| 44 | return (output, len(input)) |
| 45 | |
| 46 | class Codec(codecs.Codec): |
| 47 | |
Marc-André Lemburg | 26e3b68 | 2001-09-20 10:33:38 +0000 | [diff] [blame] | 48 | def encode(self, input, errors='strict'): |
| 49 | return zlib_encode(input, errors) |
| 50 | def decode(self, input, errors='strict'): |
| 51 | return zlib_decode(input, errors) |
Marc-André Lemburg | 2d92041 | 2001-05-15 12:00:02 +0000 | [diff] [blame] | 52 | |
Walter Dörwald | abb02e5 | 2006-03-15 11:35:15 +0000 | [diff] [blame] | 53 | class IncrementalEncoder(codecs.IncrementalEncoder): |
Georg Brandl | 2c9838e | 2006-10-29 14:39:09 +0000 | [diff] [blame] | 54 | def __init__(self, errors='strict'): |
| 55 | assert errors == 'strict' |
| 56 | self.errors = errors |
| 57 | self.compressobj = zlib.compressobj() |
| 58 | |
Walter Dörwald | abb02e5 | 2006-03-15 11:35:15 +0000 | [diff] [blame] | 59 | def encode(self, input, final=False): |
Georg Brandl | 2c9838e | 2006-10-29 14:39:09 +0000 | [diff] [blame] | 60 | if final: |
| 61 | c = self.compressobj.compress(input) |
| 62 | return c + self.compressobj.flush() |
| 63 | else: |
| 64 | return self.compressobj.compress(input) |
| 65 | |
| 66 | def reset(self): |
| 67 | self.compressobj = zlib.compressobj() |
Walter Dörwald | abb02e5 | 2006-03-15 11:35:15 +0000 | [diff] [blame] | 68 | |
| 69 | class IncrementalDecoder(codecs.IncrementalDecoder): |
Georg Brandl | 2c9838e | 2006-10-29 14:39:09 +0000 | [diff] [blame] | 70 | def __init__(self, errors='strict'): |
| 71 | assert errors == 'strict' |
| 72 | self.errors = errors |
| 73 | self.decompressobj = zlib.decompressobj() |
| 74 | |
Walter Dörwald | abb02e5 | 2006-03-15 11:35:15 +0000 | [diff] [blame] | 75 | def decode(self, input, final=False): |
Georg Brandl | 2c9838e | 2006-10-29 14:39:09 +0000 | [diff] [blame] | 76 | if final: |
| 77 | c = self.decompressobj.decompress(input) |
| 78 | return c + self.decompressobj.flush() |
| 79 | else: |
| 80 | return self.decompressobj.decompress(input) |
| 81 | |
| 82 | def reset(self): |
| 83 | self.decompressobj = zlib.decompressobj() |
Walter Dörwald | abb02e5 | 2006-03-15 11:35:15 +0000 | [diff] [blame] | 84 | |
Marc-André Lemburg | 2d92041 | 2001-05-15 12:00:02 +0000 | [diff] [blame] | 85 | class StreamWriter(Codec,codecs.StreamWriter): |
| 86 | pass |
Tim Peters | 469cdad | 2002-08-08 20:19:19 +0000 | [diff] [blame] | 87 | |
Marc-André Lemburg | 2d92041 | 2001-05-15 12:00:02 +0000 | [diff] [blame] | 88 | class StreamReader(Codec,codecs.StreamReader): |
| 89 | pass |
| 90 | |
| 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='zlib', |
| 96 | encode=zlib_encode, |
| 97 | decode=zlib_decode, |
| 98 | incrementalencoder=IncrementalEncoder, |
| 99 | incrementaldecoder=IncrementalDecoder, |
| 100 | streamreader=StreamReader, |
| 101 | streamwriter=StreamWriter, |
| 102 | ) |