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.transport import TTransport |
| 21 | |
Bryan Duxbury | 6972041 | 2012-01-03 17:32:30 +0000 | [diff] [blame] | 22 | |
Roger Meier | f4eec7a | 2011-09-11 18:16:21 +0000 | [diff] [blame] | 23 | class TBase(object): |
Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 24 | __slots__ = () |
Roger Meier | f4eec7a | 2011-09-11 18:16:21 +0000 | [diff] [blame] | 25 | |
Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 26 | 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 Meier | f4eec7a | 2011-09-11 18:16:21 +0000 | [diff] [blame] | 29 | |
Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 30 | 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 Duxbury | 6972041 | 2012-01-03 17:32:30 +0000 | [diff] [blame] | 39 | |
Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 40 | def __ne__(self, other): |
| 41 | return not (self == other) |
Bryan Duxbury | 6972041 | 2012-01-03 17:32:30 +0000 | [diff] [blame] | 42 | |
Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 43 | def read(self, iprot): |
Nobuaki Sukegawa | 6525f6a | 2016-02-11 13:58:39 +0900 | [diff] [blame] | 44 | if (iprot._fast_decode is not None and |
Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 45 | isinstance(iprot.trans, TTransport.CReadableTransport) and |
Nobuaki Sukegawa | 6525f6a | 2016-02-11 13:58:39 +0900 | [diff] [blame] | 46 | self.thrift_spec is not None): |
Eric Conner | c34653f | 2017-06-21 03:34:12 +0200 | [diff] [blame] | 47 | iprot._fast_decode(self, iprot, [self.__class__, self.thrift_spec]) |
Nobuaki Sukegawa | 6525f6a | 2016-02-11 13:58:39 +0900 | [diff] [blame] | 48 | else: |
| 49 | iprot.readStruct(self, self.thrift_spec) |
Roger Meier | f4eec7a | 2011-09-11 18:16:21 +0000 | [diff] [blame] | 50 | |
Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 51 | def write(self, oprot): |
Nobuaki Sukegawa | 6525f6a | 2016-02-11 13:58:39 +0900 | [diff] [blame] | 52 | if (oprot._fast_encode is not None and self.thrift_spec is not None): |
Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 53 | oprot.trans.write( |
Eric Conner | c34653f | 2017-06-21 03:34:12 +0200 | [diff] [blame] | 54 | oprot._fast_encode(self, [self.__class__, self.thrift_spec])) |
Nobuaki Sukegawa | 6525f6a | 2016-02-11 13:58:39 +0900 | [diff] [blame] | 55 | else: |
| 56 | oprot.writeStruct(self, self.thrift_spec) |
Roger Meier | f4eec7a | 2011-09-11 18:16:21 +0000 | [diff] [blame] | 57 | |
Bryan Duxbury | 6972041 | 2012-01-03 17:32:30 +0000 | [diff] [blame] | 58 | |
Nobuaki Sukegawa | 760511f | 2015-11-06 21:24:16 +0900 | [diff] [blame] | 59 | class TExceptionBase(TBase, Exception): |
Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 60 | pass |
Nobuaki Sukegawa | e841b3d | 2015-11-17 11:01:17 +0900 | [diff] [blame] | 61 | |
| 62 | |
| 63 | class TFrozenBase(TBase): |
Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 64 | def __setitem__(self, *args): |
| 65 | raise TypeError("Can't modify frozen struct") |
Nobuaki Sukegawa | e841b3d | 2015-11-17 11:01:17 +0900 | [diff] [blame] | 66 | |
Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 67 | def __delitem__(self, *args): |
| 68 | raise TypeError("Can't modify frozen struct") |
Nobuaki Sukegawa | e841b3d | 2015-11-17 11:01:17 +0900 | [diff] [blame] | 69 | |
Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 70 | def __hash__(self, *args): |
| 71 | return hash(self.__class__) ^ hash(self.__slots__) |
Nobuaki Sukegawa | e841b3d | 2015-11-17 11:01:17 +0900 | [diff] [blame] | 72 | |
Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 73 | @classmethod |
| 74 | def read(cls, iprot): |
Nobuaki Sukegawa | 6525f6a | 2016-02-11 13:58:39 +0900 | [diff] [blame] | 75 | if (iprot._fast_decode is not None and |
Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 76 | isinstance(iprot.trans, TTransport.CReadableTransport) and |
Nobuaki Sukegawa | 6525f6a | 2016-02-11 13:58:39 +0900 | [diff] [blame] | 77 | cls.thrift_spec is not None): |
Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 78 | self = cls() |
Nobuaki Sukegawa | 6525f6a | 2016-02-11 13:58:39 +0900 | [diff] [blame] | 79 | return iprot._fast_decode(None, iprot, |
Eric Conner | c34653f | 2017-06-21 03:34:12 +0200 | [diff] [blame] | 80 | [self.__class__, self.thrift_spec]) |
Nobuaki Sukegawa | 6525f6a | 2016-02-11 13:58:39 +0900 | [diff] [blame] | 81 | else: |
| 82 | return iprot.readStruct(cls, cls.thrift_spec, True) |