Added support for Google's Brotli library (#156)

* Ignore coverage for brotli decompression

* Updated assert comment for brotli and brotlipy
This commit is contained in:
halbow 2019-07-27 18:32:01 +02:00 committed by Seth Michael Larson
parent f974ebd93e
commit 2cc8beabfc

View File

@ -85,25 +85,31 @@ class BrotliDecoder(Decoder):
Handle 'brotli' decoding.
Requires `pip install brotlipy`. See: https://brotlipy.readthedocs.io/
or `pip install brotli`. See https://github.com/google/brotli
Supports both 'brotlipy' and 'Brotli' packages since they share an import
name. The top branches are for 'brotlipy' and bottom branches for 'Brotli'
"""
def __init__(self) -> None:
assert (
brotli is not None
), "The 'brotlipy' library must be installed to use 'BrotliDecoder'"
), "The 'brotlipy' or 'brotli' library must be installed to use 'BrotliDecoder'"
self.decompressor = brotli.Decompressor()
def decode(self, data: bytes) -> bytes:
try:
return self.decompressor.decompress(data)
except brotli.Error as exc:
if hasattr(self.decompressor, "decompress"):
return self.decompressor.decompress(data)
return self.decompressor.process(data) # pragma: nocover
except brotli.error as exc:
raise DecodingError from exc
def flush(self) -> bytes:
try:
self.decompressor.finish()
if hasattr(self.decompressor, "finish"):
self.decompressor.finish()
return b""
except brotli.Error as exc: # pragma: nocover
except brotli.error as exc: # pragma: nocover
raise DecodingError from exc