annotate CSP2/CSP2_env/env-d9b9114564458d9d-741b3de822f2aaca6c6caa4325c4afce/lib/python3.8/encodings/bz2_codec.py @ 69:33d812a61356

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