Mark Slee | 89e2bb8 | 2007-03-01 00:20:36 +0000 | [diff] [blame] | 1 | #!/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 Slee | e74306a | 2007-02-21 05:38:12 +0000 | [diff] [blame] | 9 | class 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 | |
| 28 | class TMessageType: |
| 29 | CALL = 1 |
| 30 | REPLY = 2 |
| 31 | EXCEPTION = 3 |
| 32 | |
Mark Slee | c967656 | 2006-09-05 17:34:52 +0000 | [diff] [blame] | 33 | class TProcessor: |
| 34 | |
| 35 | """Base class for procsessor, which works on two streams.""" |
| 36 | |
Mark Slee | 4ac459f | 2006-10-25 21:39:01 +0000 | [diff] [blame] | 37 | def process(iprot, oprot): |
Mark Slee | c967656 | 2006-09-05 17:34:52 +0000 | [diff] [blame] | 38 | pass |
Mark Slee | 92195ae | 2007-02-21 05:16:30 +0000 | [diff] [blame] | 39 | |
| 40 | class TException(Exception): |
| 41 | |
| 42 | """Base class for all thrift exceptions.""" |
| 43 | |
| 44 | def __init__(self, message=None): |
| 45 | Exception.__init__(self, message) |
Mark Slee | 7679196 | 2007-03-14 02:47:35 +0000 | [diff] [blame] | 46 | self.message = message |
Mark Slee | 92195ae | 2007-02-21 05:16:30 +0000 | [diff] [blame] | 47 | |
| 48 | class 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 | |
dweatherford | 33d8f34 | 2008-01-07 22:23:07 +0000 | [diff] [blame] | 63 | 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 Slee | 92195ae | 2007-02-21 05:16:30 +0000 | [diff] [blame] | 79 | 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() |