annotate CSP2/CSP2_env/env-d9b9114564458d9d-741b3de822f2aaca6c6caa4325c4afce/lib/python3.8/encodings/bz2_codec.py @ 68:5028fdace37b

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