blob: af6f58daf48f4f406ea90804ca69d2804268d4ce [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
41class TMessageType:
42 CALL = 1
43 REPLY = 2
44 EXCEPTION = 3
David Reissdeda1412009-04-02 19:22:31 +000045 ONEWAY = 4
Mark Sleee74306a2007-02-21 05:38:12 +000046
Mark Sleec9676562006-09-05 17:34:52 +000047class TProcessor:
48
49 """Base class for procsessor, which works on two streams."""
50
Mark Slee4ac459f2006-10-25 21:39:01 +000051 def process(iprot, oprot):
Mark Sleec9676562006-09-05 17:34:52 +000052 pass
Mark Slee92195ae2007-02-21 05:16:30 +000053
54class TException(Exception):
55
56 """Base class for all thrift exceptions."""
57
David Reissc548b3d2010-03-09 05:19:18 +000058 # BaseException.message is deprecated in Python v[2.6,3.0)
59 if (2,6,0) <= sys.version_info < (3,0):
60 def _get_message(self):
61 return self._message
62 def _set_message(self, message):
63 self._message = message
64 message = property(_get_message, _set_message)
65
Mark Slee92195ae2007-02-21 05:16:30 +000066 def __init__(self, message=None):
67 Exception.__init__(self, message)
Mark Slee76791962007-03-14 02:47:35 +000068 self.message = message
Mark Slee92195ae2007-02-21 05:16:30 +000069
70class TApplicationException(TException):
71
72 """Application level thrift exceptions."""
73
74 UNKNOWN = 0
75 UNKNOWN_METHOD = 1
76 INVALID_MESSAGE_TYPE = 2
77 WRONG_METHOD_NAME = 3
78 BAD_SEQUENCE_ID = 4
79 MISSING_RESULT = 5
Roger Meier345ecc72011-08-03 09:49:27 +000080 INTERNAL_ERROR = 6
81 PROTOCOL_ERROR = 7
Mark Slee92195ae2007-02-21 05:16:30 +000082
83 def __init__(self, type=UNKNOWN, message=None):
84 TException.__init__(self, message)
85 self.type = type
86
dweatherford33d8f342008-01-07 22:23:07 +000087 def __str__(self):
88 if self.message:
89 return self.message
Bryan Duxbury686d92c2010-09-02 00:36:18 +000090 elif self.type == self.UNKNOWN_METHOD:
dweatherford33d8f342008-01-07 22:23:07 +000091 return 'Unknown method'
Bryan Duxbury686d92c2010-09-02 00:36:18 +000092 elif self.type == self.INVALID_MESSAGE_TYPE:
dweatherford33d8f342008-01-07 22:23:07 +000093 return 'Invalid message type'
Bryan Duxbury686d92c2010-09-02 00:36:18 +000094 elif self.type == self.WRONG_METHOD_NAME:
dweatherford33d8f342008-01-07 22:23:07 +000095 return 'Wrong method name'
Bryan Duxbury686d92c2010-09-02 00:36:18 +000096 elif self.type == self.BAD_SEQUENCE_ID:
dweatherford33d8f342008-01-07 22:23:07 +000097 return 'Bad sequence ID'
Bryan Duxbury686d92c2010-09-02 00:36:18 +000098 elif self.type == self.MISSING_RESULT:
dweatherford33d8f342008-01-07 22:23:07 +000099 return 'Missing result'
100 else:
101 return 'Default (unknown) TApplicationException'
102
Mark Slee92195ae2007-02-21 05:16:30 +0000103 def read(self, iprot):
104 iprot.readStructBegin()
105 while True:
106 (fname, ftype, fid) = iprot.readFieldBegin()
107 if ftype == TType.STOP:
108 break
109 if fid == 1:
110 if ftype == TType.STRING:
111 self.message = iprot.readString();
112 else:
113 iprot.skip(ftype)
114 elif fid == 2:
115 if ftype == TType.I32:
116 self.type = iprot.readI32();
117 else:
118 iprot.skip(ftype)
119 else:
120 iprot.skip(ftype)
121 iprot.readFieldEnd()
122 iprot.readStructEnd()
123
124 def write(self, oprot):
125 oprot.writeStructBegin('TApplicationException')
126 if self.message != None:
127 oprot.writeFieldBegin('message', TType.STRING, 1)
128 oprot.writeString(self.message)
129 oprot.writeFieldEnd()
130 if self.type != None:
131 oprot.writeFieldBegin('type', TType.I32, 2)
132 oprot.writeI32(self.type)
133 oprot.writeFieldEnd()
134 oprot.writeFieldStop()
135 oprot.writeStructEnd()