Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 87 lines (59 sloc) 2.103 kb
fb1e7e0 Improved reference documentation
Ask Solem authored
1 """
2 kombu.compression
3 =================
4
5 Compression utilities.
6
ba0935d @ask Updates copyright years to include 2012
ask authored
7 :copyright: (c) 2009 - 2012 by Ask Solem.
fb1e7e0 Improved reference documentation
Ask Solem authored
8 :license: BSD, see LICENSE for more details.
9
10 """
66ef950 @ask 2.0-devel: No longer supports Python 2.4
ask authored
11 from __future__ import absolute_import
12
de391b6 @steeve Handle binary bodies in compression.
steeve authored
13 from kombu.utils.encoding import ensure_bytes, bytes_to_str
14
2bf695d Unit tests now passing on Python 3
Ask Solem authored
15 import zlib
fb1e7e0 Improved reference documentation
Ask Solem authored
16
c5cb3a9 Added support for automatic compression/decompression of messages.
Ask Solem authored
17 _aliases = {}
18 _encoders = {}
19 _decoders = {}
20
5e7a324 @ask Use single quotes
ask authored
21 __all__ = ['register', 'encoders', 'get_encoder',
22 'get_decoder', 'compress', 'decompress']
88b8be3 @ask Adds __all__ to modules
ask authored
23
c5cb3a9 Added support for automatic compression/decompression of messages.
Ask Solem authored
24
25 def register(encoder, decoder, content_type, aliases=[]):
fb1e7e0 Improved reference documentation
Ask Solem authored
26 """Register new compression method.
27
28 :param encoder: Function used to compress text.
29 :param decoder: Function used to decompress previously compressed text.
30 :param content_type: The mime type this compression method identifies as.
31 :param aliases: A list of names to associate with this compression method.
32
33 """
c5cb3a9 Added support for automatic compression/decompression of messages.
Ask Solem authored
34 _encoders[content_type] = encoder
35 _decoders[content_type] = decoder
36 _aliases.update((alias, content_type) for alias in aliases)
37
38
fb1e7e0 Improved reference documentation
Ask Solem authored
39 def encoders():
40 """Returns a list of available compression methods."""
21279e7 @ask dict.keys is useless
ask authored
41 return list(_encoders)
fb1e7e0 Improved reference documentation
Ask Solem authored
42
43
c5cb3a9 Added support for automatic compression/decompression of messages.
Ask Solem authored
44 def get_encoder(t):
fb1e7e0 Improved reference documentation
Ask Solem authored
45 """Get encoder by alias name."""
c5cb3a9 Added support for automatic compression/decompression of messages.
Ask Solem authored
46 t = _aliases.get(t, t)
47 return _encoders[t], t
48
49
50 def get_decoder(t):
fb1e7e0 Improved reference documentation
Ask Solem authored
51 """Get decoder by alias name."""
c5cb3a9 Added support for automatic compression/decompression of messages.
Ask Solem authored
52 return _decoders[_aliases.get(t, t)]
53
54
55 def compress(body, content_type):
fb1e7e0 Improved reference documentation
Ask Solem authored
56 """Compress text.
57
58 :param body: The text to compress.
59 :param content_type: mime-type of compression method to use.
60
61 """
c5cb3a9 Added support for automatic compression/decompression of messages.
Ask Solem authored
62 encoder, content_type = get_encoder(content_type)
de391b6 @steeve Handle binary bodies in compression.
steeve authored
63 return encoder(ensure_bytes(body)), content_type
c5cb3a9 Added support for automatic compression/decompression of messages.
Ask Solem authored
64
65
66 def decompress(body, content_type):
fb1e7e0 Improved reference documentation
Ask Solem authored
67 """Decompress compressed text.
68
69 :param body: Previously compressed text to uncompress.
70 :param content_type: mime-type of compression method used.
71
72 """
de391b6 @steeve Handle binary bodies in compression.
steeve authored
73 return bytes_to_str(get_decoder(content_type)(body))
c5cb3a9 Added support for automatic compression/decompression of messages.
Ask Solem authored
74
75
2bf695d Unit tests now passing on Python 3
Ask Solem authored
76 register(zlib.compress,
77 zlib.decompress,
5e7a324 @ask Use single quotes
ask authored
78 'application/x-gzip', aliases=['gzip', 'zlib'])
f525112 @ask The bz2 module does not exist on Jython
ask authored
79 try:
80 import bz2
81 except ImportError:
82 pass # Jython?
83 else:
84 register(bz2.compress,
85 bz2.decompress,
5e7a324 @ask Use single quotes
ask authored
86 'application/x-bz2', aliases=['bzip2', 'bzip'])
Something went wrong with that request. Please try again.