7
7
import shutil
8
8
import json
9
9
from collections import deque
10
+ from hubblestack .utils .misc import numbered_file_split_key
11
+ from hubblestack .utils .encoding import encode_something_to_bytes , decode_something_to_string
10
12
11
13
__all__ = [
12
14
'QueueTypeError' , 'QueueCapacityError' , 'MemQueue' , 'DiskQueue' ,
@@ -37,6 +39,7 @@ def check_type(self, item):
37
39
if not isinstance (item , self .ok_types ):
38
40
raise QueueTypeError ('type({0}) is not ({1})' .format (type (item ), self .ok_types ))
39
41
42
+
40
43
class NoQueue (object ):
41
44
cn = 0
42
45
def put (self , * a , ** kw ):
@@ -69,6 +72,7 @@ def __bool__(self):
69
72
__nonzero__ = __bool__ # stupid python2
70
73
71
74
def compress (self , dat ):
75
+ dat = encode_something_to_bytes (dat )
72
76
if not self .compression :
73
77
return dat
74
78
def _bz2 (x ):
@@ -84,7 +88,8 @@ def unlink_(self, fname):
84
88
os .unlink (name )
85
89
86
90
def decompress (self , dat ):
87
- if str (dat ).startswith ('BZ' ):
91
+ dat = encode_something_to_bytes (dat )
92
+ if dat .startswith (b'BZ' ):
88
93
try :
89
94
return bz2 .BZ2Decompressor ().decompress (dat )
90
95
except IOError :
@@ -131,8 +136,6 @@ def put(self, item, **meta):
131
136
f = os .path .join (d , remainder )
132
137
with open (f , 'wb' ) as fh :
133
138
log .debug ('writing item to disk cache' )
134
- if isinstance (bstr , str ):
135
- bstr = str .encode (bstr )
136
139
fh .write (bstr )
137
140
if meta :
138
141
with open (f + '.meta' , 'w' ) as fh :
@@ -160,23 +163,23 @@ def peek(self):
160
163
"""
161
164
for fname in self .files :
162
165
with open (fname , 'rb' ) as fh :
163
- return self .decompress (fh .read ()), self .read_meta (fname )
166
+ return decode_something_to_string ( self .decompress (fh .read () )), self .read_meta (fname )
164
167
165
168
def get (self ):
166
169
""" get the next item from the queue
167
170
returns: data_octets, meta_data_dict
168
171
"""
169
172
for fname in self .files :
170
173
with open (fname , 'rb' ) as fh :
171
- ret = self .decompress (fh .read ())
172
- ret = ret , self .read_meta (fname )
174
+ dat = self .decompress (fh .read ())
175
+ mdat = self .read_meta (fname )
173
176
sz = os .stat (fname ).st_size
174
177
self .unlink_ (fname )
175
178
self .cn -= 1
176
179
self .sz -= sz
177
180
if self .double_check_cnsz :
178
181
self ._count (double_check_only = True , tag = 'get' )
179
- return ret
182
+ return decode_something_to_string ( dat ), mdat
180
183
181
184
def getz (self , sz = SPLUNK_MAX_MSG ):
182
185
""" fetch items from the queue and concatenate them together using the
@@ -219,7 +222,7 @@ def getz(self, sz=SPLUNK_MAX_MSG):
219
222
#
220
223
# occasionally this will return something pessimistic
221
224
meta_data [k ] = max (meta_data [k ])
222
- return ret , meta_data
225
+ return decode_something_to_string ( ret ) , meta_data
223
226
224
227
def pop (self ):
225
228
""" remove the next item from the queue (do not return it); useful with .peek() """
@@ -235,14 +238,8 @@ def pop(self):
235
238
@property
236
239
def files (self ):
237
240
""" generate all filenames in the diskqueue (returns iterable) """
238
- def _k (x ):
239
- try :
240
- return [int (i ) for i in x .split ('.' )]
241
- except :
242
- pass
243
- return x
244
241
for path , dirs , files in sorted (os .walk (self .directory )):
245
- for fname in [os .path .join (path , f ) for f in sorted (files , key = _k )]:
242
+ for fname in [os .path .join (path , f ) for f in sorted (files , key = numbered_file_split_key )]:
246
243
if fname .endswith ('.meta' ):
247
244
continue
248
245
yield fname
0 commit comments