blob: d106f4e03bb8d8078876ffe6dfb893ede26b8238 [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,
Nobuaki Sukegawa7b545b52016-01-11 13:46:04 +090056 (self.__class__, self.thrift_spec),
57 iprot.string_length_limit,
58 iprot.container_length_limit)
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
Nobuaki Sukegawa760511f2015-11-06 21:24:16 +090064 self.thrift_spec is not None and
65 fastbinary is not None):
Bryan Duxbury69720412012-01-03 17:32:30 +000066 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
Nobuaki Sukegawa760511f2015-11-06 21:24:16 +090072class TExceptionBase(TBase, Exception):
Nobuaki Sukegawae841b3d2015-11-17 11:01:17 +090073 pass
74
75
76class TFrozenBase(TBase):
77 def __setitem__(self, *args):
78 raise TypeError("Can't modify frozen struct")
79
80 def __delitem__(self, *args):
81 raise TypeError("Can't modify frozen struct")
82
83 def __hash__(self, *args):
84 return hash(self.__class__) ^ hash(self.__slots__)
85
86 @classmethod
87 def read(cls, iprot):
88 if (iprot.__class__ == TBinaryProtocol.TBinaryProtocolAccelerated and
89 isinstance(iprot.trans, TTransport.CReadableTransport) and
90 cls.thrift_spec is not None and
91 fastbinary is not None):
92 self = cls()
93 return fastbinary.decode_binary(None,
94 iprot.trans,
Nobuaki Sukegawa7b545b52016-01-11 13:46:04 +090095 (self.__class__, self.thrift_spec),
96 iprot.string_length_limit,
97 iprot.container_length_limit)
Nobuaki Sukegawae841b3d2015-11-17 11:01:17 +090098 return iprot.readStruct(cls, cls.thrift_spec, True)