blob: 6cbd5f39a4772eb2bc2a1c92b640c52f2ce2e9b2 [file] [log] [blame]
Roger Meierf4eec7a2011-09-11 18:16:21 +00001#
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
20from thrift.Thrift import *
21from thrift.protocol import TBinaryProtocol
22from thrift.transport import TTransport
23
24try:
25 from thrift.protocol import fastbinary
26except:
27 fastbinary = None
28
Bryan Duxbury69720412012-01-03 17:32:30 +000029
Roger Meierf4eec7a2011-09-11 18:16:21 +000030class TBase(object):
31 __slots__ = []
32
33 def __repr__(self):
34 L = ['%s=%r' % (key, getattr(self, key))
Bryan Duxbury69720412012-01-03 17:32:30 +000035 for key in self.__slots__]
Roger Meierf4eec7a2011-09-11 18:16:21 +000036 return '%s(%s)' % (self.__class__.__name__, ', '.join(L))
37
38 def __eq__(self, other):
39 if not isinstance(other, self.__class__):
40 return False
41 for attr in self.__slots__:
42 my_val = getattr(self, attr)
43 other_val = getattr(other, attr)
44 if my_val != other_val:
45 return False
46 return True
Bryan Duxbury69720412012-01-03 17:32:30 +000047
Roger Meierf4eec7a2011-09-11 18:16:21 +000048 def __ne__(self, other):
49 return not (self == other)
Bryan Duxbury69720412012-01-03 17:32:30 +000050
Roger Meierf4eec7a2011-09-11 18:16:21 +000051 def read(self, iprot):
Bryan Duxbury69720412012-01-03 17:32:30 +000052 if (iprot.__class__ == TBinaryProtocol.TBinaryProtocolAccelerated and
53 isinstance(iprot.trans, TTransport.CReadableTransport) and
54 self.thrift_spec is not None and
55 fastbinary is not None):
56 fastbinary.decode_binary(self,
57 iprot.trans,
58 (self.__class__, self.thrift_spec))
Roger Meierf4eec7a2011-09-11 18:16:21 +000059 return
60 iprot.readStruct(self, self.thrift_spec)
61
62 def write(self, oprot):
Bryan Duxbury69720412012-01-03 17:32:30 +000063 if (oprot.__class__ == TBinaryProtocol.TBinaryProtocolAccelerated and
64 self.thrift_spec is not None and
65 fastbinary is not None):
66 oprot.trans.write(
67 fastbinary.encode_binary(self, (self.__class__, self.thrift_spec)))
Roger Meierf4eec7a2011-09-11 18:16:21 +000068 return
69 oprot.writeStruct(self, self.thrift_spec)
70
Bryan Duxbury69720412012-01-03 17:32:30 +000071
Roger Meierf4eec7a2011-09-11 18:16:21 +000072class TExceptionBase(Exception):
73 # old style class so python2.4 can raise exceptions derived from this
74 # This can't inherit from TBase because of that limitation.
75 __slots__ = []
Bryan Duxbury69720412012-01-03 17:32:30 +000076
Roger Meierf4eec7a2011-09-11 18:16:21 +000077 __repr__ = TBase.__repr__.im_func
78 __eq__ = TBase.__eq__.im_func
79 __ne__ = TBase.__ne__.im_func
80 read = TBase.read.im_func
81 write = TBase.write.im_func