blob: 4f71e119e442eb8747847582fd34fd42372c0cc5 [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
Roger Meierf4eec7a2011-09-11 18:16:21 +000020from thrift.protocol import TBinaryProtocol
21from thrift.transport import TTransport
22
23try:
24 from thrift.protocol import fastbinary
25except:
26 fastbinary = None
27
Bryan Duxbury69720412012-01-03 17:32:30 +000028
Roger Meierf4eec7a2011-09-11 18:16:21 +000029class TBase(object):
Nobuaki Sukegawae841b3d2015-11-17 11:01:17 +090030 __slots__ = ()
Roger Meierf4eec7a2011-09-11 18:16:21 +000031
32 def __repr__(self):
Nobuaki Sukegawa760511f2015-11-06 21:24:16 +090033 L = ['%s=%r' % (key, getattr(self, key)) for key in self.__slots__]
Roger Meierf4eec7a2011-09-11 18:16:21 +000034 return '%s(%s)' % (self.__class__.__name__, ', '.join(L))
35
36 def __eq__(self, other):
37 if not isinstance(other, self.__class__):
38 return False
39 for attr in self.__slots__:
40 my_val = getattr(self, attr)
41 other_val = getattr(other, attr)
42 if my_val != other_val:
43 return False
44 return True
Bryan Duxbury69720412012-01-03 17:32:30 +000045
Roger Meierf4eec7a2011-09-11 18:16:21 +000046 def __ne__(self, other):
47 return not (self == other)
Bryan Duxbury69720412012-01-03 17:32:30 +000048
Roger Meierf4eec7a2011-09-11 18:16:21 +000049 def read(self, iprot):
Bryan Duxbury69720412012-01-03 17:32:30 +000050 if (iprot.__class__ == TBinaryProtocol.TBinaryProtocolAccelerated and
Nobuaki Sukegawa760511f2015-11-06 21:24:16 +090051 isinstance(iprot.trans, TTransport.CReadableTransport) and
52 self.thrift_spec is not None and
53 fastbinary is not None):
Bryan Duxbury69720412012-01-03 17:32:30 +000054 fastbinary.decode_binary(self,
55 iprot.trans,
56 (self.__class__, self.thrift_spec))
Roger Meierf4eec7a2011-09-11 18:16:21 +000057 return
58 iprot.readStruct(self, self.thrift_spec)
59
60 def write(self, oprot):
Bryan Duxbury69720412012-01-03 17:32:30 +000061 if (oprot.__class__ == TBinaryProtocol.TBinaryProtocolAccelerated and
Nobuaki Sukegawa760511f2015-11-06 21:24:16 +090062 self.thrift_spec is not None and
63 fastbinary is not None):
Bryan Duxbury69720412012-01-03 17:32:30 +000064 oprot.trans.write(
65 fastbinary.encode_binary(self, (self.__class__, self.thrift_spec)))
Roger Meierf4eec7a2011-09-11 18:16:21 +000066 return
67 oprot.writeStruct(self, self.thrift_spec)
68
Bryan Duxbury69720412012-01-03 17:32:30 +000069
Nobuaki Sukegawa760511f2015-11-06 21:24:16 +090070class TExceptionBase(TBase, Exception):
Nobuaki Sukegawae841b3d2015-11-17 11:01:17 +090071 pass
72
73
74class TFrozenBase(TBase):
75 def __setitem__(self, *args):
76 raise TypeError("Can't modify frozen struct")
77
78 def __delitem__(self, *args):
79 raise TypeError("Can't modify frozen struct")
80
81 def __hash__(self, *args):
82 return hash(self.__class__) ^ hash(self.__slots__)
83
84 @classmethod
85 def read(cls, iprot):
86 if (iprot.__class__ == TBinaryProtocol.TBinaryProtocolAccelerated and
87 isinstance(iprot.trans, TTransport.CReadableTransport) and
88 cls.thrift_spec is not None and
89 fastbinary is not None):
90 self = cls()
91 return fastbinary.decode_binary(None,
92 iprot.trans,
93 (self.__class__, self.thrift_spec))
94 return iprot.readStruct(cls, cls.thrift_spec, True)