blob: b856928789236c791e66f48e1ce0d09908f98e0c [file] [log] [blame]
Mark Slee89e2bb82007-03-01 00:20:36 +00001#!/usr/bin/env python
2#
3# Copyright (c) 2006- Facebook
4# Distributed under the Thrift Software License
5#
6# See accompanying file LICENSE or visit the Thrift site at:
7# http://developers.facebook.com/thrift/
8
Mark Sleee74306a2007-02-21 05:38:12 +00009class TType:
10 STOP = 0
11 VOID = 1
12 BOOL = 2
13 BYTE = 3
14 I08 = 3
15 DOUBLE = 4
16 I16 = 6
17 I32 = 8
18 I64 = 10
19 STRING = 11
20 UTF7 = 11
21 STRUCT = 12
22 MAP = 13
23 SET = 14
24 LIST = 15
25 UTF8 = 16
26 UTF16 = 17
27
28class TMessageType:
29 CALL = 1
30 REPLY = 2
31 EXCEPTION = 3
32
Mark Sleec9676562006-09-05 17:34:52 +000033class TProcessor:
34
35 """Base class for procsessor, which works on two streams."""
36
Mark Slee4ac459f2006-10-25 21:39:01 +000037 def process(iprot, oprot):
Mark Sleec9676562006-09-05 17:34:52 +000038 pass
Mark Slee92195ae2007-02-21 05:16:30 +000039
40class TException(Exception):
41
42 """Base class for all thrift exceptions."""
43
44 def __init__(self, message=None):
45 Exception.__init__(self, message)
Mark Slee76791962007-03-14 02:47:35 +000046 self.message = message
Mark Slee92195ae2007-02-21 05:16:30 +000047
48class TApplicationException(TException):
49
50 """Application level thrift exceptions."""
51
52 UNKNOWN = 0
53 UNKNOWN_METHOD = 1
54 INVALID_MESSAGE_TYPE = 2
55 WRONG_METHOD_NAME = 3
56 BAD_SEQUENCE_ID = 4
57 MISSING_RESULT = 5
58
59 def __init__(self, type=UNKNOWN, message=None):
60 TException.__init__(self, message)
61 self.type = type
62
dweatherford33d8f342008-01-07 22:23:07 +000063 def __str__(self):
64 if self.message:
65 return self.message
66 elif self.type == UNKNOWN_METHOD:
67 return 'Unknown method'
68 elif self.type == INVALID_MESSAGE_TYPE:
69 return 'Invalid message type'
70 elif self.type == WRONG_METHOD_NAME:
71 return 'Wrong method name'
72 elif self.type == BAD_SEQUENCE_ID:
73 return 'Bad sequence ID'
74 elif self.type == MISSING_RESULT:
75 return 'Missing result'
76 else:
77 return 'Default (unknown) TApplicationException'
78
Mark Slee92195ae2007-02-21 05:16:30 +000079 def read(self, iprot):
80 iprot.readStructBegin()
81 while True:
82 (fname, ftype, fid) = iprot.readFieldBegin()
83 if ftype == TType.STOP:
84 break
85 if fid == 1:
86 if ftype == TType.STRING:
87 self.message = iprot.readString();
88 else:
89 iprot.skip(ftype)
90 elif fid == 2:
91 if ftype == TType.I32:
92 self.type = iprot.readI32();
93 else:
94 iprot.skip(ftype)
95 else:
96 iprot.skip(ftype)
97 iprot.readFieldEnd()
98 iprot.readStructEnd()
99
100 def write(self, oprot):
101 oprot.writeStructBegin('TApplicationException')
102 if self.message != None:
103 oprot.writeFieldBegin('message', TType.STRING, 1)
104 oprot.writeString(self.message)
105 oprot.writeFieldEnd()
106 if self.type != None:
107 oprot.writeFieldBegin('type', TType.I32, 2)
108 oprot.writeI32(self.type)
109 oprot.writeFieldEnd()
110 oprot.writeFieldStop()
111 oprot.writeStructEnd()