Mark Slee | 89e2bb8 | 2007-03-01 00:20:36 +0000 | [diff] [blame] | 1 | # |
David Reiss | ea2cba8 | 2009-03-30 21:35:00 +0000 | [diff] [blame^] | 2 | # 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 Slee | 89e2bb8 | 2007-03-01 00:20:36 +0000 | [diff] [blame] | 19 | |
Mark Slee | e74306a | 2007-02-21 05:38:12 +0000 | [diff] [blame] | 20 | class TType: |
| 21 | STOP = 0 |
| 22 | VOID = 1 |
| 23 | BOOL = 2 |
| 24 | BYTE = 3 |
| 25 | I08 = 3 |
| 26 | DOUBLE = 4 |
| 27 | I16 = 6 |
| 28 | I32 = 8 |
| 29 | I64 = 10 |
| 30 | STRING = 11 |
| 31 | UTF7 = 11 |
| 32 | STRUCT = 12 |
| 33 | MAP = 13 |
| 34 | SET = 14 |
| 35 | LIST = 15 |
| 36 | UTF8 = 16 |
| 37 | UTF16 = 17 |
| 38 | |
| 39 | class TMessageType: |
| 40 | CALL = 1 |
| 41 | REPLY = 2 |
| 42 | EXCEPTION = 3 |
| 43 | |
Mark Slee | c967656 | 2006-09-05 17:34:52 +0000 | [diff] [blame] | 44 | class TProcessor: |
| 45 | |
| 46 | """Base class for procsessor, which works on two streams.""" |
| 47 | |
Mark Slee | 4ac459f | 2006-10-25 21:39:01 +0000 | [diff] [blame] | 48 | def process(iprot, oprot): |
Mark Slee | c967656 | 2006-09-05 17:34:52 +0000 | [diff] [blame] | 49 | pass |
Mark Slee | 92195ae | 2007-02-21 05:16:30 +0000 | [diff] [blame] | 50 | |
| 51 | class TException(Exception): |
| 52 | |
| 53 | """Base class for all thrift exceptions.""" |
| 54 | |
| 55 | def __init__(self, message=None): |
| 56 | Exception.__init__(self, message) |
Mark Slee | 7679196 | 2007-03-14 02:47:35 +0000 | [diff] [blame] | 57 | self.message = message |
Mark Slee | 92195ae | 2007-02-21 05:16:30 +0000 | [diff] [blame] | 58 | |
| 59 | class TApplicationException(TException): |
| 60 | |
| 61 | """Application level thrift exceptions.""" |
| 62 | |
| 63 | UNKNOWN = 0 |
| 64 | UNKNOWN_METHOD = 1 |
| 65 | INVALID_MESSAGE_TYPE = 2 |
| 66 | WRONG_METHOD_NAME = 3 |
| 67 | BAD_SEQUENCE_ID = 4 |
| 68 | MISSING_RESULT = 5 |
| 69 | |
| 70 | def __init__(self, type=UNKNOWN, message=None): |
| 71 | TException.__init__(self, message) |
| 72 | self.type = type |
| 73 | |
dweatherford | 33d8f34 | 2008-01-07 22:23:07 +0000 | [diff] [blame] | 74 | def __str__(self): |
| 75 | if self.message: |
| 76 | return self.message |
| 77 | elif self.type == UNKNOWN_METHOD: |
| 78 | return 'Unknown method' |
| 79 | elif self.type == INVALID_MESSAGE_TYPE: |
| 80 | return 'Invalid message type' |
| 81 | elif self.type == WRONG_METHOD_NAME: |
| 82 | return 'Wrong method name' |
| 83 | elif self.type == BAD_SEQUENCE_ID: |
| 84 | return 'Bad sequence ID' |
| 85 | elif self.type == MISSING_RESULT: |
| 86 | return 'Missing result' |
| 87 | else: |
| 88 | return 'Default (unknown) TApplicationException' |
| 89 | |
Mark Slee | 92195ae | 2007-02-21 05:16:30 +0000 | [diff] [blame] | 90 | def read(self, iprot): |
| 91 | iprot.readStructBegin() |
| 92 | while True: |
| 93 | (fname, ftype, fid) = iprot.readFieldBegin() |
| 94 | if ftype == TType.STOP: |
| 95 | break |
| 96 | if fid == 1: |
| 97 | if ftype == TType.STRING: |
| 98 | self.message = iprot.readString(); |
| 99 | else: |
| 100 | iprot.skip(ftype) |
| 101 | elif fid == 2: |
| 102 | if ftype == TType.I32: |
| 103 | self.type = iprot.readI32(); |
| 104 | else: |
| 105 | iprot.skip(ftype) |
| 106 | else: |
| 107 | iprot.skip(ftype) |
| 108 | iprot.readFieldEnd() |
| 109 | iprot.readStructEnd() |
| 110 | |
| 111 | def write(self, oprot): |
| 112 | oprot.writeStructBegin('TApplicationException') |
| 113 | if self.message != None: |
| 114 | oprot.writeFieldBegin('message', TType.STRING, 1) |
| 115 | oprot.writeString(self.message) |
| 116 | oprot.writeFieldEnd() |
| 117 | if self.type != None: |
| 118 | oprot.writeFieldBegin('type', TType.I32, 2) |
| 119 | oprot.writeI32(self.type) |
| 120 | oprot.writeFieldEnd() |
| 121 | oprot.writeFieldStop() |
| 122 | oprot.writeStructEnd() |