Auto Generated INFO.yaml file
[domino.git] / lib / thrift / protocol / TBinaryProtocol.py
1 #
2 # Licensed to the Apache Software Foundation (ASF) under one
3 # or more contributor license agreements. See the NOTICE file
4 # distributed with this work for additional information
5 # regarding copyright ownership. The ASF licenses this file
6 # to you under the Apache License, Version 2.0 (the
7 # "License"); you may not use this file except in compliance
8 # with the License. You may obtain a copy of the License at
9 #
10 #   http://www.apache.org/licenses/LICENSE-2.0
11 #
12 # Unless required by applicable law or agreed to in writing,
13 # software distributed under the License is distributed on an
14 # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15 # KIND, either express or implied. See the License for the
16 # specific language governing permissions and limitations
17 # under the License.
18 #
19
20 from TProtocol import *
21 from struct import pack, unpack
22
23
24 class TBinaryProtocol(TProtocolBase):
25   """Binary implementation of the Thrift protocol driver."""
26
27   # NastyHaxx. Python 2.4+ on 32-bit machines forces hex constants to be
28   # positive, converting this into a long. If we hardcode the int value
29   # instead it'll stay in 32 bit-land.
30
31   # VERSION_MASK = 0xffff0000
32   VERSION_MASK = -65536
33
34   # VERSION_1 = 0x80010000
35   VERSION_1 = -2147418112
36
37   TYPE_MASK = 0x000000ff
38
39   def __init__(self, trans, strictRead=False, strictWrite=True):
40     TProtocolBase.__init__(self, trans)
41     self.strictRead = strictRead
42     self.strictWrite = strictWrite
43
44   def writeMessageBegin(self, name, type, seqid):
45     if self.strictWrite:
46       self.writeI32(TBinaryProtocol.VERSION_1 | type)
47       self.writeString(name)
48       self.writeI32(seqid)
49     else:
50       self.writeString(name)
51       self.writeByte(type)
52       self.writeI32(seqid)
53
54   def writeMessageEnd(self):
55     pass
56
57   def writeStructBegin(self, name):
58     pass
59
60   def writeStructEnd(self):
61     pass
62
63   def writeFieldBegin(self, name, type, id):
64     self.writeByte(type)
65     self.writeI16(id)
66
67   def writeFieldEnd(self):
68     pass
69
70   def writeFieldStop(self):
71     self.writeByte(TType.STOP)
72
73   def writeMapBegin(self, ktype, vtype, size):
74     self.writeByte(ktype)
75     self.writeByte(vtype)
76     self.writeI32(size)
77
78   def writeMapEnd(self):
79     pass
80
81   def writeListBegin(self, etype, size):
82     self.writeByte(etype)
83     self.writeI32(size)
84
85   def writeListEnd(self):
86     pass
87
88   def writeSetBegin(self, etype, size):
89     self.writeByte(etype)
90     self.writeI32(size)
91
92   def writeSetEnd(self):
93     pass
94
95   def writeBool(self, bool):
96     if bool:
97       self.writeByte(1)
98     else:
99       self.writeByte(0)
100
101   def writeByte(self, byte):
102     buff = pack("!b", byte)
103     self.trans.write(buff)
104
105   def writeI16(self, i16):
106     buff = pack("!h", i16)
107     self.trans.write(buff)
108
109   def writeI32(self, i32):
110     buff = pack("!i", i32)
111     self.trans.write(buff)
112
113   def writeI64(self, i64):
114     buff = pack("!q", i64)
115     self.trans.write(buff)
116
117   def writeDouble(self, dub):
118     buff = pack("!d", dub)
119     self.trans.write(buff)
120
121   def writeString(self, str):
122     self.writeI32(len(str))
123     self.trans.write(str)
124
125   def readMessageBegin(self):
126     sz = self.readI32()
127     if sz < 0:
128       version = sz & TBinaryProtocol.VERSION_MASK
129       if version != TBinaryProtocol.VERSION_1:
130         raise TProtocolException(
131           type=TProtocolException.BAD_VERSION,
132           message='Bad version in readMessageBegin: %d' % (sz))
133       type = sz & TBinaryProtocol.TYPE_MASK
134       name = self.readString()
135       seqid = self.readI32()
136     else:
137       if self.strictRead:
138         raise TProtocolException(type=TProtocolException.BAD_VERSION,
139                                  message='No protocol version header')
140       name = self.trans.readAll(sz)
141       type = self.readByte()
142       seqid = self.readI32()
143     return (name, type, seqid)
144
145   def readMessageEnd(self):
146     pass
147
148   def readStructBegin(self):
149     pass
150
151   def readStructEnd(self):
152     pass
153
154   def readFieldBegin(self):
155     type = self.readByte()
156     if type == TType.STOP:
157       return (None, type, 0)
158     id = self.readI16()
159     return (None, type, id)
160
161   def readFieldEnd(self):
162     pass
163
164   def readMapBegin(self):
165     ktype = self.readByte()
166     vtype = self.readByte()
167     size = self.readI32()
168     return (ktype, vtype, size)
169
170   def readMapEnd(self):
171     pass
172
173   def readListBegin(self):
174     etype = self.readByte()
175     size = self.readI32()
176     return (etype, size)
177
178   def readListEnd(self):
179     pass
180
181   def readSetBegin(self):
182     etype = self.readByte()
183     size = self.readI32()
184     return (etype, size)
185
186   def readSetEnd(self):
187     pass
188
189   def readBool(self):
190     byte = self.readByte()
191     if byte == 0:
192       return False
193     return True
194
195   def readByte(self):
196     buff = self.trans.readAll(1)
197     val, = unpack('!b', buff)
198     return val
199
200   def readI16(self):
201     buff = self.trans.readAll(2)
202     val, = unpack('!h', buff)
203     return val
204
205   def readI32(self):
206     buff = self.trans.readAll(4)
207     val, = unpack('!i', buff)
208     return val
209
210   def readI64(self):
211     buff = self.trans.readAll(8)
212     val, = unpack('!q', buff)
213     return val
214
215   def readDouble(self):
216     buff = self.trans.readAll(8)
217     val, = unpack('!d', buff)
218     return val
219
220   def readString(self):
221     len = self.readI32()
222     str = self.trans.readAll(len)
223     return str
224
225
226 class TBinaryProtocolFactory:
227   def __init__(self, strictRead=False, strictWrite=True):
228     self.strictRead = strictRead
229     self.strictWrite = strictWrite
230
231   def getProtocol(self, trans):
232     prot = TBinaryProtocol(trans, self.strictRead, self.strictWrite)
233     return prot
234
235
236 class TBinaryProtocolAccelerated(TBinaryProtocol):
237   """C-Accelerated version of TBinaryProtocol.
238
239   This class does not override any of TBinaryProtocol's methods,
240   but the generated code recognizes it directly and will call into
241   our C module to do the encoding, bypassing this object entirely.
242   We inherit from TBinaryProtocol so that the normal TBinaryProtocol
243   encoding can happen if the fastbinary module doesn't work for some
244   reason.  (TODO(dreiss): Make this happen sanely in more cases.)
245
246   In order to take advantage of the C module, just use
247   TBinaryProtocolAccelerated instead of TBinaryProtocol.
248
249   NOTE:  This code was contributed by an external developer.
250          The internal Thrift team has reviewed and tested it,
251          but we cannot guarantee that it is production-ready.
252          Please feel free to report bugs and/or success stories
253          to the public mailing list.
254   """
255   pass
256
257
258 class TBinaryProtocolAcceleratedFactory:
259   def getProtocol(self, trans):
260     return TBinaryProtocolAccelerated(trans)