This repository was archived by the owner on Jan 5, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 45
/
Copy paththrift.py
111 lines (90 loc) · 3.72 KB
/
thrift.py
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
# Copyright (c) 2016 Uber Technologies, Inc.
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
from __future__ import unicode_literals
import six
from tchannel.schemes import THRIFT
from .. import io
from .. import rw
class ThriftSerializer(object):
name = THRIFT
_headers_rw = rw.headers(
rw.number(2),
rw.len_prefixed_string(rw.number(2)),
rw.len_prefixed_string(rw.number(2)),
)
def __init__(self, deserialize_type):
self.deserialize_type = deserialize_type
def serialize_header(self, headers):
headers = headers or {}
result = self._headers_rw.write(headers, io.BytesIO()).getvalue()
return result
def deserialize_header(self, headers):
headers = headers or {}
if headers:
if six.PY3 and isinstance(headers, str):
headers = headers.encode('utf8')
headers = io.BytesIO(headers)
headers = self._headers_rw.read(headers)
result = dict(headers)
return result
def serialize_body(self, call_args):
# TODO - use fastbinary directly
#
# fastbinary.encode_binary(
# call_args, (call_args.__class__, call_args.thrift_spec)
# )
# fastbinary.decode_binary(
# result, TMemoryBuffer(body),(result_type, result_type.thrift_spec)
# )
#
from thrift.protocol import TBinaryProtocol
from thrift.transport import TTransport
trans = TTransport.TMemoryBuffer()
proto = TBinaryProtocol.TBinaryProtocolAccelerated(trans)
call_args.write(proto)
result = trans.getvalue()
return result
def deserialize_body(self, body):
from thrift.protocol import TBinaryProtocol
from thrift.transport import TTransport
trans = TTransport.TMemoryBuffer(body)
proto = TBinaryProtocol.TBinaryProtocolAccelerated(trans)
result = self.deserialize_type()
result.read(proto)
return result
class ThriftRWSerializer(ThriftSerializer):
def __init__(self, module, deserialize_type):
"""
:param module:
thriftrw generated module
"""
# TODO export dumps/loads on classes in thriftrw in addition to
# module-level dumps/loads.
super(ThriftRWSerializer, self).__init__(deserialize_type)
self.module = module
def serialize_body(self, obj):
return self.module.dumps(obj)
def deserialize_body(self, body):
if six.PY3 and isinstance(body, str):
body = body.encode('utf8', errors='surrogateescape')
return self.module.loads(self.deserialize_type, body)