blob: 5f2564de59822c5a7ba336564247c961c30e5259 [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
80
81 def __init__(self, type=UNKNOWN, message=None):
82 TException.__init__(self, message)
83 self.type = type
84
dweatherford33d8f342008-01-07 22:23:07 +000085 def __str__(self):
86 if self.message:
87 return self.message
88 elif self.type == UNKNOWN_METHOD:
89 return 'Unknown method'
90 elif self.type == INVALID_MESSAGE_TYPE:
91 return 'Invalid message type'
92 elif self.type == WRONG_METHOD_NAME:
93 return 'Wrong method name'
94 elif self.type == BAD_SEQUENCE_ID:
95 return 'Bad sequence ID'
96 elif self.type == MISSING_RESULT:
97 return 'Missing result'
98 else:
99 return 'Default (unknown) TApplicationException'
100
Mark Slee92195ae2007-02-21 05:16:30 +0000101 def read(self, iprot):
102 iprot.readStructBegin()
103 while True:
104 (fname, ftype, fid) = iprot.readFieldBegin()
105 if ftype == TType.STOP:
106 break
107 if fid == 1:
108 if ftype == TType.STRING:
109 self.message = iprot.readString();
110 else:
111 iprot.skip(ftype)
112 elif fid == 2:
113 if ftype == TType.I32:
114 self.type = iprot.readI32();
115 else:
116 iprot.skip(ftype)
117 else:
118 iprot.skip(ftype)
119 iprot.readFieldEnd()
120 iprot.readStructEnd()
121
122 def write(self, oprot):
123 oprot.writeStructBegin('TApplicationException')
124 if self.message != None:
125 oprot.writeFieldBegin('message', TType.STRING, 1)
126 oprot.writeString(self.message)
127 oprot.writeFieldEnd()
128 if self.type != None:
129 oprot.writeFieldBegin('type', TType.I32, 2)
130 oprot.writeI32(self.type)
131 oprot.writeFieldEnd()
132 oprot.writeFieldStop()
133 oprot.writeStructEnd()