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