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 | |
David Reiss | c548b3d | 2010-03-09 05:19:18 +0000 | [diff] [blame] | 20 | import sys |
| 21 | |
Mark Slee | e74306a | 2007-02-21 05:38:12 +0000 | [diff] [blame] | 22 | class 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 | |
Roger Meier | f4eec7a | 2011-09-11 18:16:21 +0000 | [diff] [blame] | 41 | _VALUES_TO_NAMES = ( 'STOP', |
| 42 | 'VOID', |
| 43 | 'BOOL', |
| 44 | 'BYTE', |
| 45 | 'DOUBLE', |
| 46 | None, |
| 47 | 'I16', |
| 48 | None, |
| 49 | 'I32', |
| 50 | None, |
| 51 | 'I64', |
| 52 | 'STRING', |
| 53 | 'STRUCT', |
| 54 | 'MAP', |
| 55 | 'SET', |
| 56 | 'LIST', |
| 57 | 'UTF8', |
| 58 | 'UTF16' ) |
| 59 | |
Mark Slee | e74306a | 2007-02-21 05:38:12 +0000 | [diff] [blame] | 60 | class TMessageType: |
| 61 | CALL = 1 |
| 62 | REPLY = 2 |
| 63 | EXCEPTION = 3 |
David Reiss | deda141 | 2009-04-02 19:22:31 +0000 | [diff] [blame] | 64 | ONEWAY = 4 |
Mark Slee | e74306a | 2007-02-21 05:38:12 +0000 | [diff] [blame] | 65 | |
Mark Slee | c967656 | 2006-09-05 17:34:52 +0000 | [diff] [blame] | 66 | class TProcessor: |
| 67 | |
| 68 | """Base class for procsessor, which works on two streams.""" |
| 69 | |
Mark Slee | 4ac459f | 2006-10-25 21:39:01 +0000 | [diff] [blame] | 70 | def process(iprot, oprot): |
Mark Slee | c967656 | 2006-09-05 17:34:52 +0000 | [diff] [blame] | 71 | pass |
Mark Slee | 92195ae | 2007-02-21 05:16:30 +0000 | [diff] [blame] | 72 | |
| 73 | class TException(Exception): |
| 74 | |
| 75 | """Base class for all thrift exceptions.""" |
| 76 | |
David Reiss | c548b3d | 2010-03-09 05:19:18 +0000 | [diff] [blame] | 77 | # BaseException.message is deprecated in Python v[2.6,3.0) |
| 78 | if (2,6,0) <= sys.version_info < (3,0): |
| 79 | def _get_message(self): |
| 80 | return self._message |
| 81 | def _set_message(self, message): |
| 82 | self._message = message |
| 83 | message = property(_get_message, _set_message) |
| 84 | |
Mark Slee | 92195ae | 2007-02-21 05:16:30 +0000 | [diff] [blame] | 85 | def __init__(self, message=None): |
| 86 | Exception.__init__(self, message) |
Mark Slee | 7679196 | 2007-03-14 02:47:35 +0000 | [diff] [blame] | 87 | self.message = message |
Mark Slee | 92195ae | 2007-02-21 05:16:30 +0000 | [diff] [blame] | 88 | |
| 89 | class TApplicationException(TException): |
| 90 | |
| 91 | """Application level thrift exceptions.""" |
| 92 | |
| 93 | UNKNOWN = 0 |
| 94 | UNKNOWN_METHOD = 1 |
| 95 | INVALID_MESSAGE_TYPE = 2 |
| 96 | WRONG_METHOD_NAME = 3 |
| 97 | BAD_SEQUENCE_ID = 4 |
| 98 | MISSING_RESULT = 5 |
Roger Meier | 345ecc7 | 2011-08-03 09:49:27 +0000 | [diff] [blame] | 99 | INTERNAL_ERROR = 6 |
| 100 | PROTOCOL_ERROR = 7 |
Mark Slee | 92195ae | 2007-02-21 05:16:30 +0000 | [diff] [blame] | 101 | |
| 102 | def __init__(self, type=UNKNOWN, message=None): |
| 103 | TException.__init__(self, message) |
| 104 | self.type = type |
| 105 | |
dweatherford | 33d8f34 | 2008-01-07 22:23:07 +0000 | [diff] [blame] | 106 | def __str__(self): |
| 107 | if self.message: |
| 108 | return self.message |
Bryan Duxbury | 686d92c | 2010-09-02 00:36:18 +0000 | [diff] [blame] | 109 | elif self.type == self.UNKNOWN_METHOD: |
dweatherford | 33d8f34 | 2008-01-07 22:23:07 +0000 | [diff] [blame] | 110 | return 'Unknown method' |
Bryan Duxbury | 686d92c | 2010-09-02 00:36:18 +0000 | [diff] [blame] | 111 | elif self.type == self.INVALID_MESSAGE_TYPE: |
dweatherford | 33d8f34 | 2008-01-07 22:23:07 +0000 | [diff] [blame] | 112 | return 'Invalid message type' |
Bryan Duxbury | 686d92c | 2010-09-02 00:36:18 +0000 | [diff] [blame] | 113 | elif self.type == self.WRONG_METHOD_NAME: |
dweatherford | 33d8f34 | 2008-01-07 22:23:07 +0000 | [diff] [blame] | 114 | return 'Wrong method name' |
Bryan Duxbury | 686d92c | 2010-09-02 00:36:18 +0000 | [diff] [blame] | 115 | elif self.type == self.BAD_SEQUENCE_ID: |
dweatherford | 33d8f34 | 2008-01-07 22:23:07 +0000 | [diff] [blame] | 116 | return 'Bad sequence ID' |
Bryan Duxbury | 686d92c | 2010-09-02 00:36:18 +0000 | [diff] [blame] | 117 | elif self.type == self.MISSING_RESULT: |
dweatherford | 33d8f34 | 2008-01-07 22:23:07 +0000 | [diff] [blame] | 118 | return 'Missing result' |
| 119 | else: |
| 120 | return 'Default (unknown) TApplicationException' |
| 121 | |
Mark Slee | 92195ae | 2007-02-21 05:16:30 +0000 | [diff] [blame] | 122 | def read(self, iprot): |
| 123 | iprot.readStructBegin() |
| 124 | while True: |
| 125 | (fname, ftype, fid) = iprot.readFieldBegin() |
| 126 | if ftype == TType.STOP: |
| 127 | break |
| 128 | if fid == 1: |
| 129 | if ftype == TType.STRING: |
| 130 | self.message = iprot.readString(); |
| 131 | else: |
| 132 | iprot.skip(ftype) |
| 133 | elif fid == 2: |
| 134 | if ftype == TType.I32: |
| 135 | self.type = iprot.readI32(); |
| 136 | else: |
| 137 | iprot.skip(ftype) |
| 138 | else: |
| 139 | iprot.skip(ftype) |
| 140 | iprot.readFieldEnd() |
| 141 | iprot.readStructEnd() |
| 142 | |
| 143 | def write(self, oprot): |
| 144 | oprot.writeStructBegin('TApplicationException') |
| 145 | if self.message != None: |
| 146 | oprot.writeFieldBegin('message', TType.STRING, 1) |
| 147 | oprot.writeString(self.message) |
| 148 | oprot.writeFieldEnd() |
| 149 | if self.type != None: |
| 150 | oprot.writeFieldBegin('type', TType.I32, 2) |
| 151 | oprot.writeI32(self.type) |
| 152 | oprot.writeFieldEnd() |
| 153 | oprot.writeFieldStop() |
| 154 | oprot.writeStructEnd() |