bz2_codec.py 3.1 KB

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