From 93a05f7261b05005113c34a078328706fb930d86 Mon Sep 17 00:00:00 2001 From: Omer Katz Date: Wed, 9 Jan 2019 19:05:02 +0200 Subject: [PATCH] Added optional support for brotli compression (#983) * Added optional support for brotli compression. * Ignore import error for coverage purposes. --- kombu/compression.py | 9 +++++++++ requirements/extras/brotli.txt | 2 ++ requirements/test-ci.txt | 1 + t/unit/test_compression.py | 16 ++++++++++++++++ 4 files changed, 28 insertions(+) create mode 100644 requirements/extras/brotli.txt diff --git a/kombu/compression.py b/kombu/compression.py index 26feb70c..2ae07172 100644 --- a/kombu/compression.py +++ b/kombu/compression.py @@ -76,3 +76,12 @@ register(zlib.compress, register(bz2.compress, bz2.decompress, 'application/x-bz2', aliases=['bzip2', 'bzip']) + +try: + import brotli +except ImportError: # pragma: no cover + pass +else: + register(brotli.compress, + brotli.decompress, + 'application/x-brotli', aliases=['brotli']) diff --git a/requirements/extras/brotli.txt b/requirements/extras/brotli.txt new file mode 100644 index 00000000..a449c7de --- /dev/null +++ b/requirements/extras/brotli.txt @@ -0,0 +1,2 @@ +brotlipy>=0.7.0;python_implementation=="PyPy" +brotli>=1.0.0;python_implementation=="CPython" diff --git a/requirements/test-ci.txt b/requirements/test-ci.txt index 65b43895..143af172 100644 --- a/requirements/test-ci.txt +++ b/requirements/test-ci.txt @@ -9,4 +9,5 @@ msgpack-python>0.2.0 -r extras/consul.txt -r extras/librabbitmq.txt -r extras/zookeeper.txt +-r extras/brotli.txt sqlalchemy diff --git a/t/unit/test_compression.py b/t/unit/test_compression.py index 4f18f05d..2b416824 100644 --- a/t/unit/test_compression.py +++ b/t/unit/test_compression.py @@ -1,5 +1,7 @@ from __future__ import absolute_import, unicode_literals +import pytest + from kombu import compression @@ -11,6 +13,11 @@ class test_compression: def test_encoders__bz2(self): assert 'application/x-bz2' in compression.encoders() + def test_encoders__brotli(self): + pytest.importorskip('brotli') + + assert 'application/x-brotli' in compression.encoders() + def test_compress__decompress__zlib(self): text = b'The Quick Brown Fox Jumps Over The Lazy Dog' c, ctype = compression.compress(text, 'zlib') @@ -24,3 +31,12 @@ class test_compression: assert text != c d = compression.decompress(c, ctype) assert d == text + + def test_compress__decompress__brotli(self): + pytest.importorskip('brotli') + + text = b'The Brown Quick Fox Over The Lazy Dog Jumps' + c, ctype = compression.compress(text, 'brotli') + assert text != c + d = compression.decompress(c, ctype) + assert d == text