blob: cbb9184407ad4cafecad409b5bbf4b03a14616a2 [file] [log] [blame]
Mark Slee89e2bb82007-03-01 00:20:36 +00001#
David Reissea2cba82009-03-30 21:35:00 +00002# 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#
Mark Slee89e2bb82007-03-01 00:20:36 +000019
David Reissc548b3d2010-03-09 05:19:18 +000020import sys
21
Bryan Duxbury69720412012-01-03 17:32:30 +000022
Mark Sleee74306a2007-02-21 05:38:12 +000023class TType:
24 STOP = 0
25 VOID = 1
26 BOOL = 2
27 BYTE = 3
28 I08 = 3
29 DOUBLE = 4
30 I16 = 6
31 I32 = 8
32 I64 = 10
33 STRING = 11
34 UTF7 = 11
35 STRUCT = 12
36 MAP = 13
37 SET = 14
38 LIST = 15
39 UTF8 = 16
40 UTF16 = 17
41
Bryan Duxbury69720412012-01-03 17:32:30 +000042 _VALUES_TO_NAMES = ('STOP',
Roger Meierf4eec7a2011-09-11 18:16:21 +000043 'VOID',
44 'BOOL',
45 'BYTE',
46 'DOUBLE',
47 None,
48 'I16',
49 None,
50 'I32',
51 None,
Bryan Duxbury69720412012-01-03 17:32:30 +000052 'I64',
53 'STRING',
54 'STRUCT',
55 'MAP',
56 'SET',
57 'LIST',
58 'UTF8',
59 'UTF16')
60
Roger Meierf4eec7a2011-09-11 18:16:21 +000061
Mark Sleee74306a2007-02-21 05:38:12 +000062class TMessageType:
Bryan Duxbury69720412012-01-03 17:32:30 +000063 CALL = 1
Mark Sleee74306a2007-02-21 05:38:12 +000064 REPLY = 2
65 EXCEPTION = 3
David Reissdeda1412009-04-02 19:22:31 +000066 ONEWAY = 4
Mark Sleee74306a2007-02-21 05:38:12 +000067
Mark Sleec9676562006-09-05 17:34:52 +000068
Bryan Duxbury69720412012-01-03 17:32:30 +000069class TProcessor:
Mark Sleec9676562006-09-05 17:34:52 +000070 """Base class for procsessor, which works on two streams."""
71
Mark Slee4ac459f2006-10-25 21:39:01 +000072 def process(iprot, oprot):
Mark Sleec9676562006-09-05 17:34:52 +000073 pass
Mark Slee92195ae2007-02-21 05:16:30 +000074
Mark Slee92195ae2007-02-21 05:16:30 +000075
Bryan Duxbury69720412012-01-03 17:32:30 +000076class TException(Exception):
Mark Slee92195ae2007-02-21 05:16:30 +000077 """Base class for all thrift exceptions."""
78
David Reissc548b3d2010-03-09 05:19:18 +000079 # BaseException.message is deprecated in Python v[2.6,3.0)
Bryan Duxbury69720412012-01-03 17:32:30 +000080 if (2, 6, 0) <= sys.version_info < (3, 0):
David Reissc548b3d2010-03-09 05:19:18 +000081 def _get_message(self):
Bryan Duxbury69720412012-01-03 17:32:30 +000082 return self._message
83
David Reissc548b3d2010-03-09 05:19:18 +000084 def _set_message(self, message):
Bryan Duxbury69720412012-01-03 17:32:30 +000085 self._message = message
David Reissc548b3d2010-03-09 05:19:18 +000086 message = property(_get_message, _set_message)
87
Mark Slee92195ae2007-02-21 05:16:30 +000088 def __init__(self, message=None):
89 Exception.__init__(self, message)
Mark Slee76791962007-03-14 02:47:35 +000090 self.message = message
Mark Slee92195ae2007-02-21 05:16:30 +000091
Mark Slee92195ae2007-02-21 05:16:30 +000092
Bryan Duxbury69720412012-01-03 17:32:30 +000093class TApplicationException(TException):
Mark Slee92195ae2007-02-21 05:16:30 +000094 """Application level thrift exceptions."""
95
96 UNKNOWN = 0
97 UNKNOWN_METHOD = 1
98 INVALID_MESSAGE_TYPE = 2
99 WRONG_METHOD_NAME = 3
100 BAD_SEQUENCE_ID = 4
101 MISSING_RESULT = 5
Roger Meier345ecc72011-08-03 09:49:27 +0000102 INTERNAL_ERROR = 6
103 PROTOCOL_ERROR = 7
Roger Meier01931492012-12-22 21:31:03 +0100104 INVALID_TRANSFORM = 8
105 INVALID_PROTOCOL = 9
106 UNSUPPORTED_CLIENT_TYPE = 10
Mark Slee92195ae2007-02-21 05:16:30 +0000107
108 def __init__(self, type=UNKNOWN, message=None):
109 TException.__init__(self, message)
110 self.type = type
111
dweatherford33d8f342008-01-07 22:23:07 +0000112 def __str__(self):
113 if self.message:
114 return self.message
Bryan Duxbury686d92c2010-09-02 00:36:18 +0000115 elif self.type == self.UNKNOWN_METHOD:
dweatherford33d8f342008-01-07 22:23:07 +0000116 return 'Unknown method'
Bryan Duxbury686d92c2010-09-02 00:36:18 +0000117 elif self.type == self.INVALID_MESSAGE_TYPE:
dweatherford33d8f342008-01-07 22:23:07 +0000118 return 'Invalid message type'
Bryan Duxbury686d92c2010-09-02 00:36:18 +0000119 elif self.type == self.WRONG_METHOD_NAME:
dweatherford33d8f342008-01-07 22:23:07 +0000120 return 'Wrong method name'
Bryan Duxbury686d92c2010-09-02 00:36:18 +0000121 elif self.type == self.BAD_SEQUENCE_ID:
dweatherford33d8f342008-01-07 22:23:07 +0000122 return 'Bad sequence ID'
Bryan Duxbury686d92c2010-09-02 00:36:18 +0000123 elif self.type == self.MISSING_RESULT:
dweatherford33d8f342008-01-07 22:23:07 +0000124 return 'Missing result'
Roger Meier01931492012-12-22 21:31:03 +0100125 elif self.type == self.INTERNAL_ERROR:
126 return 'Internal error'
127 elif self.type == self.PROTOCOL_ERROR:
128 return 'Protocol error'
129 elif self.type == self.INVALID_TRANSFORM:
130 return 'Invalid transform'
131 elif self.type == self.INVALID_PROTOCOL:
132 return 'Invalid protocol'
133 elif self.type == self.UNSUPPORTED_CLIENT_TYPE:
134 return 'Unsupported client type'
dweatherford33d8f342008-01-07 22:23:07 +0000135 else:
136 return 'Default (unknown) TApplicationException'
137
Mark Slee92195ae2007-02-21 05:16:30 +0000138 def read(self, iprot):
139 iprot.readStructBegin()
140 while True:
141 (fname, ftype, fid) = iprot.readFieldBegin()
142 if ftype == TType.STOP:
143 break
144 if fid == 1:
145 if ftype == TType.STRING:
Bryan Duxbury69720412012-01-03 17:32:30 +0000146 self.message = iprot.readString()
Mark Slee92195ae2007-02-21 05:16:30 +0000147 else:
148 iprot.skip(ftype)
149 elif fid == 2:
150 if ftype == TType.I32:
Bryan Duxbury69720412012-01-03 17:32:30 +0000151 self.type = iprot.readI32()
Mark Slee92195ae2007-02-21 05:16:30 +0000152 else:
153 iprot.skip(ftype)
154 else:
155 iprot.skip(ftype)
156 iprot.readFieldEnd()
157 iprot.readStructEnd()
158
159 def write(self, oprot):
160 oprot.writeStructBegin('TApplicationException')
Bryan Duxbury69720412012-01-03 17:32:30 +0000161 if self.message is not None:
Mark Slee92195ae2007-02-21 05:16:30 +0000162 oprot.writeFieldBegin('message', TType.STRING, 1)
163 oprot.writeString(self.message)
164 oprot.writeFieldEnd()
Bryan Duxbury69720412012-01-03 17:32:30 +0000165 if self.type is not None:
Mark Slee92195ae2007-02-21 05:16:30 +0000166 oprot.writeFieldBegin('type', TType.I32, 2)
167 oprot.writeI32(self.type)
168 oprot.writeFieldEnd()
169 oprot.writeFieldStop()
170 oprot.writeStructEnd()
Nobuaki Sukegawae841b3d2015-11-17 11:01:17 +0900171
172
173class TFrozenDict(dict):
174 """A dictionary that is "frozen" like a frozenset"""
175
176 def __init__(self, *args, **kwargs):
177 super(TFrozenDict, self).__init__(*args, **kwargs)
178 # Sort the items so they will be in a consistent order.
179 # XOR in the hash of the class so we don't collide with
180 # the hash of a list of tuples.
181 self.__hashval = hash(TFrozenDict) ^ hash(tuple(sorted(self.items())))
182
183 def __setitem__(self, *args):
184 raise TypeError("Can't modify frozen TFreezableDict")
185
186 def __delitem__(self, *args):
187 raise TypeError("Can't modify frozen TFreezableDict")
188
189 def __hash__(self):
190 return self.__hashval