| [3225] | 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 |
|
|---|
| 6 | Adapted by Raymond Hettinger from zlib_codec.py which was written
|
|---|
| 7 | by Marc-Andre Lemburg ([email protected]).
|
|---|
| 8 |
|
|---|
| 9 | """
|
|---|
| 10 | import codecs
|
|---|
| 11 | import bz2 # this codec needs the optional bz2 module !
|
|---|
| 12 |
|
|---|
| 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 | """
|
|---|
| 25 | assert errors == 'strict'
|
|---|
| 26 | output = bz2.compress(input)
|
|---|
| 27 | return (output, len(input))
|
|---|
| 28 |
|
|---|
| 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 | """
|
|---|
| 43 | assert errors == 'strict'
|
|---|
| 44 | output = bz2.decompress(input)
|
|---|
| 45 | return (output, len(input))
|
|---|
| 46 |
|
|---|
| 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 |
|
|---|
| 54 | class IncrementalEncoder(codecs.IncrementalEncoder):
|
|---|
| 55 | def encode(self, input, final=False):
|
|---|
| 56 | assert self.errors == 'strict'
|
|---|
| 57 | return bz2.compress(input)
|
|---|
| 58 |
|
|---|
| 59 | class IncrementalDecoder(codecs.IncrementalDecoder):
|
|---|
| 60 | def decode(self, input, final=False):
|
|---|
| 61 | assert self.errors == 'strict'
|
|---|
| 62 | return bz2.decompress(input)
|
|---|
| 63 |
|
|---|
| 64 | class StreamWriter(Codec,codecs.StreamWriter):
|
|---|
| 65 | pass
|
|---|
| 66 |
|
|---|
| 67 | class StreamReader(Codec,codecs.StreamReader):
|
|---|
| 68 | pass
|
|---|
| 69 |
|
|---|
| 70 | ### encodings module API
|
|---|
| 71 |
|
|---|
| 72 | def getregentry():
|
|---|
| 73 | return codecs.CodecInfo(
|
|---|
| 74 | name="bz2",
|
|---|
| 75 | encode=bz2_encode,
|
|---|
| 76 | decode=bz2_decode,
|
|---|
| 77 | incrementalencoder=IncrementalEncoder,
|
|---|
| 78 | incrementaldecoder=IncrementalDecoder,
|
|---|
| 79 | streamwriter=StreamWriter,
|
|---|
| 80 | streamreader=StreamReader,
|
|---|
| 81 | )
|
|---|