zlib_codec.py 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. """ Python 'zlib_codec' Codec - zlib 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. Written by Marc-Andre Lemburg (mal@lemburg.com).
  5. """
  6. import codecs
  7. import zlib # this codec needs the optional zlib module !
  8. ### Codec APIs
  9. def zlib_encode(input,errors='strict'):
  10. """ Encodes the object input and returns a tuple (output
  11. object, length consumed).
  12. errors defines the error handling to apply. It defaults to
  13. 'strict' handling which is the only currently supported
  14. error handling for this codec.
  15. """
  16. assert errors == 'strict'
  17. output = zlib.compress(input)
  18. return (output, len(input))
  19. def zlib_decode(input,errors='strict'):
  20. """ Decodes the object input and returns a tuple (output
  21. object, length consumed).
  22. input must be an object which provides the bf_getreadbuf
  23. buffer slot. Python strings, buffer objects and memory
  24. mapped files are examples of objects providing this slot.
  25. errors defines the error handling to apply. It defaults to
  26. 'strict' handling which is the only currently supported
  27. error handling for this codec.
  28. """
  29. assert errors == 'strict'
  30. output = zlib.decompress(input)
  31. return (output, len(input))
  32. class Codec(codecs.Codec):
  33. def encode(self, input, errors='strict'):
  34. return zlib_encode(input, errors)
  35. def decode(self, input, errors='strict'):
  36. return zlib_decode(input, errors)
  37. class IncrementalEncoder(codecs.IncrementalEncoder):
  38. def __init__(self, errors='strict'):
  39. assert errors == 'strict'
  40. self.errors = errors
  41. self.compressobj = zlib.compressobj()
  42. def encode(self, input, final=False):
  43. if final:
  44. c = self.compressobj.compress(input)
  45. return c + self.compressobj.flush()
  46. else:
  47. return self.compressobj.compress(input)
  48. def reset(self):
  49. self.compressobj = zlib.compressobj()
  50. class IncrementalDecoder(codecs.IncrementalDecoder):
  51. def __init__(self, errors='strict'):
  52. assert errors == 'strict'
  53. self.errors = errors
  54. self.decompressobj = zlib.decompressobj()
  55. def decode(self, input, final=False):
  56. if final:
  57. c = self.decompressobj.decompress(input)
  58. return c + self.decompressobj.flush()
  59. else:
  60. return self.decompressobj.decompress(input)
  61. def reset(self):
  62. self.decompressobj = zlib.decompressobj()
  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='zlib',
  71. encode=zlib_encode,
  72. decode=zlib_decode,
  73. incrementalencoder=IncrementalEncoder,
  74. incrementaldecoder=IncrementalDecoder,
  75. streamreader=StreamReader,
  76. streamwriter=StreamWriter,
  77. _is_text_encoding=False,
  78. )