blob: 9ae1b1182723f1693dceea6e60fff1f5720e8f60 [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.transport import TTransport
21
Bryan Duxbury69720412012-01-03 17:32:30 +000022
Roger Meierf4eec7a2011-09-11 18:16:21 +000023class TBase(object):
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +090024 __slots__ = ()
Roger Meierf4eec7a2011-09-11 18:16:21 +000025
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +090026 def __repr__(self):
27 L = ['%s=%r' % (key, getattr(self, key)) for key in self.__slots__]
28 return '%s(%s)' % (self.__class__.__name__, ', '.join(L))
Roger Meierf4eec7a2011-09-11 18:16:21 +000029
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +090030 def __eq__(self, other):
31 if not isinstance(other, self.__class__):
32 return False
33 for attr in self.__slots__:
34 my_val = getattr(self, attr)
35 other_val = getattr(other, attr)
36 if my_val != other_val:
37 return False
38 return True
Bryan Duxbury69720412012-01-03 17:32:30 +000039
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +090040 def __ne__(self, other):
41 return not (self == other)
Bryan Duxbury69720412012-01-03 17:32:30 +000042
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +090043 def read(self, iprot):
Nobuaki Sukegawa6525f6a2016-02-11 13:58:39 +090044 if (iprot._fast_decode is not None and
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +090045 isinstance(iprot.trans, TTransport.CReadableTransport) and
Nobuaki Sukegawa6525f6a2016-02-11 13:58:39 +090046 self.thrift_spec is not None):
Eric Connerc34653f2017-06-21 03:34:12 +020047 iprot._fast_decode(self, iprot, [self.__class__, self.thrift_spec])
Nobuaki Sukegawa6525f6a2016-02-11 13:58:39 +090048 else:
49 iprot.readStruct(self, self.thrift_spec)
Roger Meierf4eec7a2011-09-11 18:16:21 +000050
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +090051 def write(self, oprot):
Nobuaki Sukegawa6525f6a2016-02-11 13:58:39 +090052 if (oprot._fast_encode is not None and self.thrift_spec is not None):
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +090053 oprot.trans.write(
Eric Connerc34653f2017-06-21 03:34:12 +020054 oprot._fast_encode(self, [self.__class__, self.thrift_spec]))
Nobuaki Sukegawa6525f6a2016-02-11 13:58:39 +090055 else:
56 oprot.writeStruct(self, self.thrift_spec)
Roger Meierf4eec7a2011-09-11 18:16:21 +000057
Bryan Duxbury69720412012-01-03 17:32:30 +000058
Nobuaki Sukegawa760511f2015-11-06 21:24:16 +090059class TExceptionBase(TBase, Exception):
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +090060 pass
Nobuaki Sukegawae841b3d2015-11-17 11:01:17 +090061
62
63class TFrozenBase(TBase):
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +090064 def __setitem__(self, *args):
65 raise TypeError("Can't modify frozen struct")
Nobuaki Sukegawae841b3d2015-11-17 11:01:17 +090066
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +090067 def __delitem__(self, *args):
68 raise TypeError("Can't modify frozen struct")
Nobuaki Sukegawae841b3d2015-11-17 11:01:17 +090069
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +090070 def __hash__(self, *args):
71 return hash(self.__class__) ^ hash(self.__slots__)
Nobuaki Sukegawae841b3d2015-11-17 11:01:17 +090072
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +090073 @classmethod
74 def read(cls, iprot):
Nobuaki Sukegawa6525f6a2016-02-11 13:58:39 +090075 if (iprot._fast_decode is not None and
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +090076 isinstance(iprot.trans, TTransport.CReadableTransport) and
Nobuaki Sukegawa6525f6a2016-02-11 13:58:39 +090077 cls.thrift_spec is not None):
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +090078 self = cls()
Nobuaki Sukegawa6525f6a2016-02-11 13:58:39 +090079 return iprot._fast_decode(None, iprot,
Eric Connerc34653f2017-06-21 03:34:12 +020080 [self.__class__, self.thrift_spec])
Nobuaki Sukegawa6525f6a2016-02-11 13:58:39 +090081 else:
82 return iprot.readStruct(cls, cls.thrift_spec, True)