def decompress(msg, method):
# type: (bytes, int) -> bytes
"""Shortcut method for decompression
Args:
msg: The message you wish to decompress, the type required is
defined by the requested method
method: The decompression method you wish to use. Supported
(assuming installed):
- :py:data:`~py2p.flags.gzip`
- :py:data:`~py2p.flags.zlib`
- :py:data:`~py2p.flags.bz2`
- :py:data:`~py2p.flags.lzma`
- :py:data:`~py2p.flags.snappy`
Returns:
Defined by the decompression method, but typically the bytes of the
compressed message
Warning:
The types fed are dependent on which decompression method you use.
Best to assume most values are :py:class:`bytes` or
:py:class:`bytearray`
Raises:
ValueError: if there is an unknown compression method, or a
method-specific error
"""
if method in (flags.gzip, flags.zlib):
return zlib.decompress(msg, zlib.MAX_WBITS | 32)
elif method == flags.bz2:
return bz2.decompress(msg)
elif method == flags.lzma:
return lzma.decompress(msg)
elif method == flags.snappy:
return snappy.decompress(msg)
else: # pragma: no cover
raise ValueError('Unknown decompression method')
# This should be in order of preference, with None being implied as last
评论列表
文章目录