blob: 1d271fcff5794c280feac5065b139c94ccbc01de [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
Mark Sleee74306a2007-02-21 05:38:12 +000022class TType:
23 STOP = 0
24 VOID = 1
25 BOOL = 2
26 BYTE = 3
27 I08 = 3
28 DOUBLE = 4
29 I16 = 6
30 I32 = 8
31 I64 = 10
32 STRING = 11
33 UTF7 = 11
34 STRUCT = 12
35 MAP = 13
36 SET = 14
37 LIST = 15
38 UTF8 = 16
39 UTF16 = 17
40
Roger Meierf4eec7a2011-09-11 18:16:21 +000041 _VALUES_TO_NAMES = ( 'STOP',
42 'VOID',
43 'BOOL',
44 'BYTE',
45 'DOUBLE',
46 None,
47 'I16',
48 None,
49 'I32',
50 None,
51 'I64',
52 'STRING',
53 'STRUCT',
54 'MAP',
55 'SET',
56 'LIST',
57 'UTF8',
58 'UTF16' )
59
Mark Sleee74306a2007-02-21 05:38:12 +000060class TMessageType:
61 CALL = 1
62 REPLY = 2
63 EXCEPTION = 3
David Reissdeda1412009-04-02 19:22:31 +000064 ONEWAY = 4
Mark Sleee74306a2007-02-21 05:38:12 +000065
Mark Sleec9676562006-09-05 17:34:52 +000066class TProcessor:
67
68 """Base class for procsessor, which works on two streams."""
69
Mark Slee4ac459f2006-10-25 21:39:01 +000070 def process(iprot, oprot):
Mark Sleec9676562006-09-05 17:34:52 +000071 pass
Mark Slee92195ae2007-02-21 05:16:30 +000072
73class TException(Exception):
74
75 """Base class for all thrift exceptions."""
76
David Reissc548b3d2010-03-09 05:19:18 +000077 # BaseException.message is deprecated in Python v[2.6,3.0)
78 if (2,6,0) <= sys.version_info < (3,0):
79 def _get_message(self):
80 return self._message
81 def _set_message(self, message):
82 self._message = message
83 message = property(_get_message, _set_message)
84
Mark Slee92195ae2007-02-21 05:16:30 +000085 def __init__(self, message=None):
86 Exception.__init__(self, message)
Mark Slee76791962007-03-14 02:47:35 +000087 self.message = message
Mark Slee92195ae2007-02-21 05:16:30 +000088
89class TApplicationException(TException):
90
91 """Application level thrift exceptions."""
92
93 UNKNOWN = 0
94 UNKNOWN_METHOD = 1
95 INVALID_MESSAGE_TYPE = 2
96 WRONG_METHOD_NAME = 3
97 BAD_SEQUENCE_ID = 4
98 MISSING_RESULT = 5
Roger Meier345ecc72011-08-03 09:49:27 +000099 INTERNAL_ERROR = 6
100 PROTOCOL_ERROR = 7
Mark Slee92195ae2007-02-21 05:16:30 +0000101
102 def __init__(self, type=UNKNOWN, message=None):
103 TException.__init__(self, message)
104 self.type = type
105
dweatherford33d8f342008-01-07 22:23:07 +0000106 def __str__(self):
107 if self.message:
108 return self.message
Bryan Duxbury686d92c2010-09-02 00:36:18 +0000109 elif self.type == self.UNKNOWN_METHOD:
dweatherford33d8f342008-01-07 22:23:07 +0000110 return 'Unknown method'
Bryan Duxbury686d92c2010-09-02 00:36:18 +0000111 elif self.type == self.INVALID_MESSAGE_TYPE:
dweatherford33d8f342008-01-07 22:23:07 +0000112 return 'Invalid message type'
Bryan Duxbury686d92c2010-09-02 00:36:18 +0000113 elif self.type == self.WRONG_METHOD_NAME:
dweatherford33d8f342008-01-07 22:23:07 +0000114 return 'Wrong method name'
Bryan Duxbury686d92c2010-09-02 00:36:18 +0000115 elif self.type == self.BAD_SEQUENCE_ID:
dweatherford33d8f342008-01-07 22:23:07 +0000116 return 'Bad sequence ID'
Bryan Duxbury686d92c2010-09-02 00:36:18 +0000117 elif self.type == self.MISSING_RESULT:
dweatherford33d8f342008-01-07 22:23:07 +0000118 return 'Missing result'
119 else:
120 return 'Default (unknown) TApplicationException'
121
Mark Slee92195ae2007-02-21 05:16:30 +0000122 def read(self, iprot):
123 iprot.readStructBegin()
124 while True:
125 (fname, ftype, fid) = iprot.readFieldBegin()
126 if ftype == TType.STOP:
127 break
128 if fid == 1:
129 if ftype == TType.STRING:
130 self.message = iprot.readString();
131 else:
132 iprot.skip(ftype)
133 elif fid == 2:
134 if ftype == TType.I32:
135 self.type = iprot.readI32();
136 else:
137 iprot.skip(ftype)
138 else:
139 iprot.skip(ftype)
140 iprot.readFieldEnd()
141 iprot.readStructEnd()
142
143 def write(self, oprot):
144 oprot.writeStructBegin('TApplicationException')
145 if self.message != None:
146 oprot.writeFieldBegin('message', TType.STRING, 1)
147 oprot.writeString(self.message)
148 oprot.writeFieldEnd()
149 if self.type != None:
150 oprot.writeFieldBegin('type', TType.I32, 2)
151 oprot.writeI32(self.type)
152 oprot.writeFieldEnd()
153 oprot.writeFieldStop()
154 oprot.writeStructEnd()