blob: 87caf0d16bdd456aa2678399b159c7480dbfb321 [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:
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +090024 from thrift.protocol import fastbinary
Roger Meierf4eec7a2011-09-11 18:16:21 +000025except:
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +090026 fastbinary = None
Roger Meierf4eec7a2011-09-11 18:16:21 +000027
Bryan Duxbury69720412012-01-03 17:32:30 +000028
Roger Meierf4eec7a2011-09-11 18:16:21 +000029class TBase(object):
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +090030 __slots__ = ()
Roger Meierf4eec7a2011-09-11 18:16:21 +000031
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +090032 def __repr__(self):
33 L = ['%s=%r' % (key, getattr(self, key)) for key in self.__slots__]
34 return '%s(%s)' % (self.__class__.__name__, ', '.join(L))
Roger Meierf4eec7a2011-09-11 18:16:21 +000035
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +090036 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
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +090046 def __ne__(self, other):
47 return not (self == other)
Bryan Duxbury69720412012-01-03 17:32:30 +000048
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +090049 def read(self, iprot):
50 if (iprot.__class__ == TBinaryProtocol.TBinaryProtocolAccelerated and
51 isinstance(iprot.trans, TTransport.CReadableTransport) and
52 self.thrift_spec is not None and
53 fastbinary is not None):
54 fastbinary.decode_binary(self,
55 iprot.trans,
56 (self.__class__, self.thrift_spec),
57 iprot.string_length_limit,
58 iprot.container_length_limit)
59 return
60 iprot.readStruct(self, self.thrift_spec)
Roger Meierf4eec7a2011-09-11 18:16:21 +000061
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +090062 def write(self, oprot):
63 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(
68 self, (self.__class__, self.thrift_spec)))
69 return
70 oprot.writeStruct(self, self.thrift_spec)
Roger Meierf4eec7a2011-09-11 18:16:21 +000071
Bryan Duxbury69720412012-01-03 17:32:30 +000072
Nobuaki Sukegawa760511f2015-11-06 21:24:16 +090073class TExceptionBase(TBase, Exception):
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +090074 pass
Nobuaki Sukegawae841b3d2015-11-17 11:01:17 +090075
76
77class TFrozenBase(TBase):
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +090078 def __setitem__(self, *args):
79 raise TypeError("Can't modify frozen struct")
Nobuaki Sukegawae841b3d2015-11-17 11:01:17 +090080
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +090081 def __delitem__(self, *args):
82 raise TypeError("Can't modify frozen struct")
Nobuaki Sukegawae841b3d2015-11-17 11:01:17 +090083
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +090084 def __hash__(self, *args):
85 return hash(self.__class__) ^ hash(self.__slots__)
Nobuaki Sukegawae841b3d2015-11-17 11:01:17 +090086
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +090087 @classmethod
88 def read(cls, iprot):
89 if (iprot.__class__ == TBinaryProtocol.TBinaryProtocolAccelerated and
90 isinstance(iprot.trans, TTransport.CReadableTransport) and
91 cls.thrift_spec is not None and
92 fastbinary is not None):
93 self = cls()
94 return fastbinary.decode_binary(None,
95 iprot.trans,
96 (self.__class__, self.thrift_spec),
97 iprot.string_length_limit,
98 iprot.container_length_limit)
99 return iprot.readStruct(cls, cls.thrift_spec, True)