1
- from io import BytesIO
2
1
import gzip
2
+ from io import BytesIO
3
3
import struct
4
4
5
- import six
6
5
from six .moves import xrange
7
6
8
7
_XERIAL_V1_HEADER = (- 126 , b'S' , b'N' , b'A' , b'P' , b'P' , b'Y' , 0 , 1 , 1 )
9
8
_XERIAL_V1_FORMAT = 'bccccccBii'
10
9
11
10
try :
12
11
import snappy
13
- _has_snappy = True
12
+ _HAS_SNAPPY = True
14
13
except ImportError :
15
- _has_snappy = False
14
+ _HAS_SNAPPY = False
16
15
17
16
18
17
def has_gzip ():
19
18
return True
20
19
21
20
22
21
def has_snappy ():
23
- return _has_snappy
22
+ return _HAS_SNAPPY
24
23
25
24
26
25
def gzip_encode (payload ):
27
- buffer = BytesIO ()
28
- handle = gzip .GzipFile (fileobj = buffer , mode = "w" )
29
- handle .write (payload )
30
- handle .close ()
31
- buffer .seek (0 )
32
- result = buffer .read ()
33
- buffer .close ()
26
+ with BytesIO () as buf :
27
+
28
+ # Gzip context manager introduced in python 2.6
29
+ # so old-fashioned way until we decide to not support 2.6
30
+ gzipper = gzip .GzipFile (fileobj = buf , mode = "w" )
31
+ try :
32
+ gzipper .write (payload )
33
+ finally :
34
+ gzipper .close ()
35
+
36
+ result = buf .getvalue ()
37
+
34
38
return result
35
39
36
40
37
41
def gzip_decode (payload ):
38
- buffer = BytesIO (payload )
39
- handle = gzip .GzipFile (fileobj = buffer , mode = 'r' )
40
- result = handle .read ()
41
- handle .close ()
42
- buffer .close ()
42
+ with BytesIO (payload ) as buf :
43
+
44
+ # Gzip context manager introduced in python 2.6
45
+ # so old-fashioned way until we decide to not support 2.6
46
+ gzipper = gzip .GzipFile (fileobj = buf , mode = 'r' )
47
+ try :
48
+ result = gzipper .read ()
49
+ finally :
50
+ gzipper .close ()
51
+
43
52
return result
44
53
45
54
46
55
def snappy_encode (payload , xerial_compatible = False , xerial_blocksize = 32 * 1024 ):
47
56
"""Encodes the given data with snappy if xerial_compatible is set then the
48
57
stream is encoded in a fashion compatible with the xerial snappy library
49
58
50
- The block size (xerial_blocksize) controls how frequent the blocking occurs
51
- 32k is the default in the xerial library.
59
+ The block size (xerial_blocksize) controls how frequent the blocking
60
+ occurs 32k is the default in the xerial library.
52
61
53
62
The format winds up being
54
63
+-------------+------------+--------------+------------+--------------+
@@ -63,7 +72,7 @@ def snappy_encode(payload, xerial_compatible=False, xerial_blocksize=32 * 1024):
63
72
length will always be <= blocksize.
64
73
"""
65
74
66
- if not _has_snappy :
75
+ if not has_snappy () :
67
76
raise NotImplementedError ("Snappy codec is not available" )
68
77
69
78
if xerial_compatible :
@@ -74,7 +83,7 @@ def _chunker():
74
83
out = BytesIO ()
75
84
76
85
header = b'' .join ([struct .pack ('!' + fmt , dat ) for fmt , dat
77
- in zip (_XERIAL_V1_FORMAT , _XERIAL_V1_HEADER )])
86
+ in zip (_XERIAL_V1_FORMAT , _XERIAL_V1_HEADER )])
78
87
79
88
out .write (header )
80
89
for chunk in _chunker ():
@@ -113,13 +122,13 @@ def _detect_xerial_stream(payload):
113
122
"""
114
123
115
124
if len (payload ) > 16 :
116
- header = header = struct .unpack ('!' + _XERIAL_V1_FORMAT , bytes (payload )[:16 ])
125
+ header = struct .unpack ('!' + _XERIAL_V1_FORMAT , bytes (payload )[:16 ])
117
126
return header == _XERIAL_V1_HEADER
118
127
return False
119
128
120
129
121
130
def snappy_decode (payload ):
122
- if not _has_snappy :
131
+ if not has_snappy () :
123
132
raise NotImplementedError ("Snappy codec is not available" )
124
133
125
134
if _detect_xerial_stream (payload ):
0 commit comments