Roger Meier | f4eec7a | 2011-09-11 18:16:21 +0000 | [diff] [blame] | 1 | # |
| 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 Meier | f4eec7a | 2011-09-11 18:16:21 +0000 | [diff] [blame] | 20 | from thrift.protocol import TBinaryProtocol |
| 21 | from thrift.transport import TTransport |
| 22 | |
| 23 | try: |
| 24 | from thrift.protocol import fastbinary |
| 25 | except: |
| 26 | fastbinary = None |
| 27 | |
Bryan Duxbury | 6972041 | 2012-01-03 17:32:30 +0000 | [diff] [blame] | 28 | |
Roger Meier | f4eec7a | 2011-09-11 18:16:21 +0000 | [diff] [blame] | 29 | class TBase(object): |
Nobuaki Sukegawa | e841b3d | 2015-11-17 11:01:17 +0900 | [diff] [blame^] | 30 | __slots__ = () |
Roger Meier | f4eec7a | 2011-09-11 18:16:21 +0000 | [diff] [blame] | 31 | |
| 32 | def __repr__(self): |
Nobuaki Sukegawa | 760511f | 2015-11-06 21:24:16 +0900 | [diff] [blame] | 33 | L = ['%s=%r' % (key, getattr(self, key)) for key in self.__slots__] |
Roger Meier | f4eec7a | 2011-09-11 18:16:21 +0000 | [diff] [blame] | 34 | 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 Duxbury | 6972041 | 2012-01-03 17:32:30 +0000 | [diff] [blame] | 45 | |
Roger Meier | f4eec7a | 2011-09-11 18:16:21 +0000 | [diff] [blame] | 46 | def __ne__(self, other): |
| 47 | return not (self == other) |
Bryan Duxbury | 6972041 | 2012-01-03 17:32:30 +0000 | [diff] [blame] | 48 | |
Roger Meier | f4eec7a | 2011-09-11 18:16:21 +0000 | [diff] [blame] | 49 | def read(self, iprot): |
Bryan Duxbury | 6972041 | 2012-01-03 17:32:30 +0000 | [diff] [blame] | 50 | if (iprot.__class__ == TBinaryProtocol.TBinaryProtocolAccelerated and |
Nobuaki Sukegawa | 760511f | 2015-11-06 21:24:16 +0900 | [diff] [blame] | 51 | isinstance(iprot.trans, TTransport.CReadableTransport) and |
| 52 | self.thrift_spec is not None and |
| 53 | fastbinary is not None): |
Bryan Duxbury | 6972041 | 2012-01-03 17:32:30 +0000 | [diff] [blame] | 54 | fastbinary.decode_binary(self, |
| 55 | iprot.trans, |
| 56 | (self.__class__, self.thrift_spec)) |
Roger Meier | f4eec7a | 2011-09-11 18:16:21 +0000 | [diff] [blame] | 57 | return |
| 58 | iprot.readStruct(self, self.thrift_spec) |
| 59 | |
| 60 | def write(self, oprot): |
Bryan Duxbury | 6972041 | 2012-01-03 17:32:30 +0000 | [diff] [blame] | 61 | if (oprot.__class__ == TBinaryProtocol.TBinaryProtocolAccelerated and |
Nobuaki Sukegawa | 760511f | 2015-11-06 21:24:16 +0900 | [diff] [blame] | 62 | self.thrift_spec is not None and |
| 63 | fastbinary is not None): |
Bryan Duxbury | 6972041 | 2012-01-03 17:32:30 +0000 | [diff] [blame] | 64 | oprot.trans.write( |
| 65 | fastbinary.encode_binary(self, (self.__class__, self.thrift_spec))) |
Roger Meier | f4eec7a | 2011-09-11 18:16:21 +0000 | [diff] [blame] | 66 | return |
| 67 | oprot.writeStruct(self, self.thrift_spec) |
| 68 | |
Bryan Duxbury | 6972041 | 2012-01-03 17:32:30 +0000 | [diff] [blame] | 69 | |
Nobuaki Sukegawa | 760511f | 2015-11-06 21:24:16 +0900 | [diff] [blame] | 70 | class TExceptionBase(TBase, Exception): |
Nobuaki Sukegawa | e841b3d | 2015-11-17 11:01:17 +0900 | [diff] [blame^] | 71 | pass |
| 72 | |
| 73 | |
| 74 | class 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) |