-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathutils.py
More file actions
250 lines (159 loc) · 4.99 KB
/
utils.py
File metadata and controls
250 lines (159 loc) · 4.99 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
# -*- coding: utf-8 -*-
"""
utils
~~~~~
utilities to support comparing python implementation
of serialization frameworks
:copyright: (c) 2014 by sc AmvTek srl
:email: devel@amvtek.com
"""
def is_protobuf_available(with_extension=False):
"return True if selected protobuf framework is reachable"
try:
if with_extension:
#TODO : OK for protobuf 2.5.0
#TODO : not clean as private modules are not public API...
import google.protobuf.internal._net_proto2___python
else:
import google.protobuf
return True
except ImportError:
return False
def build_protobuf_serializer():
"return callable serializing protobuf message object"
def serialize(protoobj):
return protoobj.SerializeToString()
return serialize
def build_protobuf_deserializer(Msg, with_extension=False):
"return callable deserializing message encoding MsgClass"
MsgClass = Msg.struct
def deserialize(data):
rv = MsgClass()
rv.ParseFromString(data)
return rv
return deserialize
def is_thrift_available(with_extension=False):
"return True if selected thrift framework is reachable"
try:
if with_extension:
from thrift.protocol import fastbinary
if not fastbinary:
raise ImportError("fastbinary not available")
else:
import thrift.protocol
return True
except:
return False
def build_thrift_serializer(with_extension=False):
"return callable serializing thrift struct object"
from thrift.protocol import TBinaryProtocol
from thrift.transport import TTransport
# set Transport
Transport = TTransport.TMemoryBuffer
# select Protocol
if with_extension:
Protocol = TBinaryProtocol.TBinaryProtocolAccelerated
else:
Protocol = TBinaryProtocol.TBinaryProtocol
def serialize(thriftobj):
tout = Transport()
thriftobj.write(Protocol(tout))
return tout.getvalue()
return serialize
def build_thrift_deserializer(Msg, with_extension=False):
"return callable deserializing message encoding StructClass"
from thrift.protocol import TBinaryProtocol
from thrift.transport import TTransport
# set Transport
Transport = TTransport.TMemoryBuffer
# select Protocol
if with_extension:
Protocol = TBinaryProtocol.TBinaryProtocolAccelerated
else:
Protocol = TBinaryProtocol.TBinaryProtocol
StructClass = Msg.struct
def deserialize(data):
msg = StructClass()
msg.read(Protocol(Transport(data)))
msg.validate()
return msg
return deserialize
def is_pycapnp_available(ignored=False):
"return True if pycapnp can be imported"
try:
import capnp
return True
except:
return False
def build_pycapnp_serializer():
"return callable serializing pycapnp message object"
def serialize(capnpobj):
return capnpobj.to_bytes()
return serialize
def build_pycapnp_deserializer(Msg, ignored=False):
"return callable deserializing message encoding Msg.struct"
MsgClass = Msg.struct
def deserialize(data):
return MsgClass.from_bytes(data)
return deserialize
def is_json_available(ignored=False):
"return True if json can be imported"
try:
import json
return True
except:
return False
def is_sjson_available(ignored=False):
"return True if simplejson can be imported"
try:
import simplejson
return True
except:
return False
def is_ujson_available(ignored=False):
"return True if ujson can be imported"
try:
import ujson
return True
except:
return False
def build_json_serializer():
"return json.dumps callable"
from json import dumps
return dumps
def build_sjson_serializer():
"return simplejson.dumps callable"
from simplejson import dumps
return dumps
def build_ujson_serializer():
"return ujson.dumps callable"
from ujson import dumps
return dumps
def build_json_deserializer(Msg, ignored=False):
"return json.loads callable"
from json import loads
return loads
def build_sjson_deserializer(Msg, ignored=False):
"return json.loads callable"
from simplejson import loads
return loads
def build_ujson_deserializer(Msg, ignored=False):
"return json.loads callable"
from ujson import loads
return loads
def is_msgpack_available(ignored=False):
"return True if msgpack can be imported"
try:
import msgpack
return True
except:
return False
def build_msgpack_serializer():
"return msgpack.dumps callable"
from msgpack import dumps
return dumps
def build_msgpack_deserializer(Msg, ignored=False):
"return msgpack.loads callable"
from msgpack import loads
from functools import partial
return partial(loads,use_list=0)