Merge "Example as code, documentation template for sphinx build"
[domino.git] / lib / thrift / protocol / TBase.py
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
20 from thrift.Thrift import *
21 from thrift.protocol import TBinaryProtocol
22 from thrift.transport import TTransport
23
24 try:
25   from thrift.protocol import fastbinary
26 except:
27   fastbinary = None
28
29
30 class TBase(object):
31   __slots__ = []
32
33   def __repr__(self):
34     L = ['%s=%r' % (key, getattr(self, key))
35               for key in self.__slots__]
36     return '%s(%s)' % (self.__class__.__name__, ', '.join(L))
37
38   def __eq__(self, other):
39     if not isinstance(other, self.__class__):
40       return False
41     for attr in self.__slots__:
42       my_val = getattr(self, attr)
43       other_val = getattr(other, attr)
44       if my_val != other_val:
45         return False
46     return True
47
48   def __ne__(self, other):
49     return not (self == other)
50
51   def read(self, iprot):
52     if (iprot.__class__ == TBinaryProtocol.TBinaryProtocolAccelerated and
53         isinstance(iprot.trans, TTransport.CReadableTransport) and
54         self.thrift_spec is not None and
55         fastbinary is not None):
56       fastbinary.decode_binary(self,
57                                iprot.trans,
58                                (self.__class__, self.thrift_spec))
59       return
60     iprot.readStruct(self, self.thrift_spec)
61
62   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(self, (self.__class__, self.thrift_spec)))
68       return
69     oprot.writeStruct(self, self.thrift_spec)
70
71
72 class TExceptionBase(Exception):
73   # old style class so python2.4 can raise exceptions derived from this
74   #  This can't inherit from TBase because of that limitation.
75   __slots__ = []
76
77   __repr__ = TBase.__repr__.im_func
78   __eq__ = TBase.__eq__.im_func
79   __ne__ = TBase.__ne__.im_func
80   read = TBase.read.im_func
81   write = TBase.write.im_func