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 | from thrift.Thrift import * |
Mark Slee | cde2b61 | 2006-09-03 21:13:07 +0000 | [diff] [blame] | 21 | |
Mark Slee | 92195ae | 2007-02-21 05:16:30 +0000 | [diff] [blame] | 22 | |
Bryan Duxbury | 6972041 | 2012-01-03 17:32:30 +0000 | [diff] [blame^] | 23 | class TProtocolException(TException): |
Mark Slee | 92195ae | 2007-02-21 05:16:30 +0000 | [diff] [blame] | 24 | """Custom Protocol Exception class""" |
| 25 | |
| 26 | UNKNOWN = 0 |
| 27 | INVALID_DATA = 1 |
| 28 | NEGATIVE_SIZE = 2 |
| 29 | SIZE_LIMIT = 3 |
Mark Slee | 808454e | 2007-06-20 21:51:57 +0000 | [diff] [blame] | 30 | BAD_VERSION = 4 |
Mark Slee | 92195ae | 2007-02-21 05:16:30 +0000 | [diff] [blame] | 31 | |
| 32 | def __init__(self, type=UNKNOWN, message=None): |
| 33 | TException.__init__(self, message) |
| 34 | self.type = type |
| 35 | |
Mark Slee | cde2b61 | 2006-09-03 21:13:07 +0000 | [diff] [blame] | 36 | |
Bryan Duxbury | 6972041 | 2012-01-03 17:32:30 +0000 | [diff] [blame^] | 37 | class TProtocolBase: |
Mark Slee | cde2b61 | 2006-09-03 21:13:07 +0000 | [diff] [blame] | 38 | """Base class for Thrift protocol driver.""" |
| 39 | |
Aditya Agarwal | 5c46819 | 2007-02-06 01:14:33 +0000 | [diff] [blame] | 40 | def __init__(self, trans): |
| 41 | self.trans = trans |
Mark Slee | 4ac459f | 2006-10-25 21:39:01 +0000 | [diff] [blame] | 42 | |
| 43 | def writeMessageBegin(self, name, type, seqid): |
Mark Slee | cde2b61 | 2006-09-03 21:13:07 +0000 | [diff] [blame] | 44 | pass |
| 45 | |
Mark Slee | 4ac459f | 2006-10-25 21:39:01 +0000 | [diff] [blame] | 46 | def writeMessageEnd(self): |
Mark Slee | cde2b61 | 2006-09-03 21:13:07 +0000 | [diff] [blame] | 47 | pass |
| 48 | |
Mark Slee | 4ac459f | 2006-10-25 21:39:01 +0000 | [diff] [blame] | 49 | def writeStructBegin(self, name): |
Mark Slee | cde2b61 | 2006-09-03 21:13:07 +0000 | [diff] [blame] | 50 | pass |
| 51 | |
Mark Slee | 4ac459f | 2006-10-25 21:39:01 +0000 | [diff] [blame] | 52 | def writeStructEnd(self): |
Mark Slee | cde2b61 | 2006-09-03 21:13:07 +0000 | [diff] [blame] | 53 | pass |
| 54 | |
Mark Slee | 4ac459f | 2006-10-25 21:39:01 +0000 | [diff] [blame] | 55 | def writeFieldBegin(self, name, type, id): |
Mark Slee | cde2b61 | 2006-09-03 21:13:07 +0000 | [diff] [blame] | 56 | pass |
| 57 | |
Mark Slee | 4ac459f | 2006-10-25 21:39:01 +0000 | [diff] [blame] | 58 | def writeFieldEnd(self): |
Mark Slee | cde2b61 | 2006-09-03 21:13:07 +0000 | [diff] [blame] | 59 | pass |
| 60 | |
Mark Slee | 4ac459f | 2006-10-25 21:39:01 +0000 | [diff] [blame] | 61 | def writeFieldStop(self): |
Mark Slee | cde2b61 | 2006-09-03 21:13:07 +0000 | [diff] [blame] | 62 | pass |
| 63 | |
Mark Slee | 4ac459f | 2006-10-25 21:39:01 +0000 | [diff] [blame] | 64 | def writeMapBegin(self, ktype, vtype, size): |
Mark Slee | cde2b61 | 2006-09-03 21:13:07 +0000 | [diff] [blame] | 65 | pass |
| 66 | |
Mark Slee | 4ac459f | 2006-10-25 21:39:01 +0000 | [diff] [blame] | 67 | def writeMapEnd(self): |
Mark Slee | cde2b61 | 2006-09-03 21:13:07 +0000 | [diff] [blame] | 68 | pass |
| 69 | |
Mark Slee | 4ac459f | 2006-10-25 21:39:01 +0000 | [diff] [blame] | 70 | def writeListBegin(self, etype, size): |
Mark Slee | cde2b61 | 2006-09-03 21:13:07 +0000 | [diff] [blame] | 71 | pass |
| 72 | |
Mark Slee | 4ac459f | 2006-10-25 21:39:01 +0000 | [diff] [blame] | 73 | def writeListEnd(self): |
Mark Slee | cde2b61 | 2006-09-03 21:13:07 +0000 | [diff] [blame] | 74 | pass |
| 75 | |
Mark Slee | 4ac459f | 2006-10-25 21:39:01 +0000 | [diff] [blame] | 76 | def writeSetBegin(self, etype, size): |
Mark Slee | cde2b61 | 2006-09-03 21:13:07 +0000 | [diff] [blame] | 77 | pass |
| 78 | |
Mark Slee | 4ac459f | 2006-10-25 21:39:01 +0000 | [diff] [blame] | 79 | def writeSetEnd(self): |
Mark Slee | cde2b61 | 2006-09-03 21:13:07 +0000 | [diff] [blame] | 80 | pass |
| 81 | |
Mark Slee | 4ac459f | 2006-10-25 21:39:01 +0000 | [diff] [blame] | 82 | def writeBool(self, bool): |
Mark Slee | cde2b61 | 2006-09-03 21:13:07 +0000 | [diff] [blame] | 83 | pass |
| 84 | |
Mark Slee | 4ac459f | 2006-10-25 21:39:01 +0000 | [diff] [blame] | 85 | def writeByte(self, byte): |
Mark Slee | cde2b61 | 2006-09-03 21:13:07 +0000 | [diff] [blame] | 86 | pass |
| 87 | |
Mark Slee | 4ac459f | 2006-10-25 21:39:01 +0000 | [diff] [blame] | 88 | def writeI16(self, i16): |
Mark Slee | cde2b61 | 2006-09-03 21:13:07 +0000 | [diff] [blame] | 89 | pass |
| 90 | |
Mark Slee | 4ac459f | 2006-10-25 21:39:01 +0000 | [diff] [blame] | 91 | def writeI32(self, i32): |
Mark Slee | cde2b61 | 2006-09-03 21:13:07 +0000 | [diff] [blame] | 92 | pass |
| 93 | |
Mark Slee | 4ac459f | 2006-10-25 21:39:01 +0000 | [diff] [blame] | 94 | def writeI64(self, i64): |
Mark Slee | cde2b61 | 2006-09-03 21:13:07 +0000 | [diff] [blame] | 95 | pass |
| 96 | |
Mark Slee | 4ac459f | 2006-10-25 21:39:01 +0000 | [diff] [blame] | 97 | def writeDouble(self, dub): |
Mark Slee | c98d050 | 2006-09-06 02:42:25 +0000 | [diff] [blame] | 98 | pass |
| 99 | |
Mark Slee | 4ac459f | 2006-10-25 21:39:01 +0000 | [diff] [blame] | 100 | def writeString(self, str): |
Mark Slee | cde2b61 | 2006-09-03 21:13:07 +0000 | [diff] [blame] | 101 | pass |
| 102 | |
Mark Slee | 4ac459f | 2006-10-25 21:39:01 +0000 | [diff] [blame] | 103 | def readMessageBegin(self): |
Mark Slee | cde2b61 | 2006-09-03 21:13:07 +0000 | [diff] [blame] | 104 | pass |
| 105 | |
Mark Slee | 4ac459f | 2006-10-25 21:39:01 +0000 | [diff] [blame] | 106 | def readMessageEnd(self): |
Mark Slee | cde2b61 | 2006-09-03 21:13:07 +0000 | [diff] [blame] | 107 | pass |
| 108 | |
Mark Slee | 4ac459f | 2006-10-25 21:39:01 +0000 | [diff] [blame] | 109 | def readStructBegin(self): |
Mark Slee | cde2b61 | 2006-09-03 21:13:07 +0000 | [diff] [blame] | 110 | pass |
| 111 | |
Mark Slee | 4ac459f | 2006-10-25 21:39:01 +0000 | [diff] [blame] | 112 | def readStructEnd(self): |
Mark Slee | cde2b61 | 2006-09-03 21:13:07 +0000 | [diff] [blame] | 113 | pass |
| 114 | |
Mark Slee | 4ac459f | 2006-10-25 21:39:01 +0000 | [diff] [blame] | 115 | def readFieldBegin(self): |
Mark Slee | cde2b61 | 2006-09-03 21:13:07 +0000 | [diff] [blame] | 116 | pass |
| 117 | |
Mark Slee | 4ac459f | 2006-10-25 21:39:01 +0000 | [diff] [blame] | 118 | def readFieldEnd(self): |
Mark Slee | cde2b61 | 2006-09-03 21:13:07 +0000 | [diff] [blame] | 119 | pass |
| 120 | |
Mark Slee | 4ac459f | 2006-10-25 21:39:01 +0000 | [diff] [blame] | 121 | def readMapBegin(self): |
Mark Slee | cde2b61 | 2006-09-03 21:13:07 +0000 | [diff] [blame] | 122 | pass |
| 123 | |
Mark Slee | 4ac459f | 2006-10-25 21:39:01 +0000 | [diff] [blame] | 124 | def readMapEnd(self): |
Mark Slee | cde2b61 | 2006-09-03 21:13:07 +0000 | [diff] [blame] | 125 | pass |
| 126 | |
Mark Slee | 4ac459f | 2006-10-25 21:39:01 +0000 | [diff] [blame] | 127 | def readListBegin(self): |
Mark Slee | cde2b61 | 2006-09-03 21:13:07 +0000 | [diff] [blame] | 128 | pass |
| 129 | |
Mark Slee | 4ac459f | 2006-10-25 21:39:01 +0000 | [diff] [blame] | 130 | def readListEnd(self): |
Mark Slee | cde2b61 | 2006-09-03 21:13:07 +0000 | [diff] [blame] | 131 | pass |
| 132 | |
Mark Slee | 4ac459f | 2006-10-25 21:39:01 +0000 | [diff] [blame] | 133 | def readSetBegin(self): |
Mark Slee | cde2b61 | 2006-09-03 21:13:07 +0000 | [diff] [blame] | 134 | pass |
| 135 | |
Mark Slee | 4ac459f | 2006-10-25 21:39:01 +0000 | [diff] [blame] | 136 | def readSetEnd(self): |
Mark Slee | cde2b61 | 2006-09-03 21:13:07 +0000 | [diff] [blame] | 137 | pass |
| 138 | |
Mark Slee | 4ac459f | 2006-10-25 21:39:01 +0000 | [diff] [blame] | 139 | def readBool(self): |
Mark Slee | cde2b61 | 2006-09-03 21:13:07 +0000 | [diff] [blame] | 140 | pass |
| 141 | |
Mark Slee | 4ac459f | 2006-10-25 21:39:01 +0000 | [diff] [blame] | 142 | def readByte(self): |
Mark Slee | cde2b61 | 2006-09-03 21:13:07 +0000 | [diff] [blame] | 143 | pass |
| 144 | |
Mark Slee | 4ac459f | 2006-10-25 21:39:01 +0000 | [diff] [blame] | 145 | def readI16(self): |
Mark Slee | cde2b61 | 2006-09-03 21:13:07 +0000 | [diff] [blame] | 146 | pass |
| 147 | |
Mark Slee | 4ac459f | 2006-10-25 21:39:01 +0000 | [diff] [blame] | 148 | def readI32(self): |
Mark Slee | cde2b61 | 2006-09-03 21:13:07 +0000 | [diff] [blame] | 149 | pass |
| 150 | |
Mark Slee | 4ac459f | 2006-10-25 21:39:01 +0000 | [diff] [blame] | 151 | def readI64(self): |
Mark Slee | cde2b61 | 2006-09-03 21:13:07 +0000 | [diff] [blame] | 152 | pass |
| 153 | |
Mark Slee | 4ac459f | 2006-10-25 21:39:01 +0000 | [diff] [blame] | 154 | def readDouble(self): |
Mark Slee | c98d050 | 2006-09-06 02:42:25 +0000 | [diff] [blame] | 155 | pass |
| 156 | |
Mark Slee | 4ac459f | 2006-10-25 21:39:01 +0000 | [diff] [blame] | 157 | def readString(self): |
Mark Slee | cde2b61 | 2006-09-03 21:13:07 +0000 | [diff] [blame] | 158 | pass |
| 159 | |
Mark Slee | 4ac459f | 2006-10-25 21:39:01 +0000 | [diff] [blame] | 160 | def skip(self, type): |
Mark Slee | cde2b61 | 2006-09-03 21:13:07 +0000 | [diff] [blame] | 161 | if type == TType.STOP: |
| 162 | return |
| 163 | elif type == TType.BOOL: |
Mark Slee | 4ac459f | 2006-10-25 21:39:01 +0000 | [diff] [blame] | 164 | self.readBool() |
Mark Slee | cde2b61 | 2006-09-03 21:13:07 +0000 | [diff] [blame] | 165 | elif type == TType.BYTE: |
Mark Slee | 4ac459f | 2006-10-25 21:39:01 +0000 | [diff] [blame] | 166 | self.readByte() |
Mark Slee | cde2b61 | 2006-09-03 21:13:07 +0000 | [diff] [blame] | 167 | elif type == TType.I16: |
Mark Slee | 4ac459f | 2006-10-25 21:39:01 +0000 | [diff] [blame] | 168 | self.readI16() |
Mark Slee | cde2b61 | 2006-09-03 21:13:07 +0000 | [diff] [blame] | 169 | elif type == TType.I32: |
Mark Slee | 4ac459f | 2006-10-25 21:39:01 +0000 | [diff] [blame] | 170 | self.readI32() |
Mark Slee | cde2b61 | 2006-09-03 21:13:07 +0000 | [diff] [blame] | 171 | elif type == TType.I64: |
Mark Slee | 4ac459f | 2006-10-25 21:39:01 +0000 | [diff] [blame] | 172 | self.readI64() |
Mark Slee | c98d050 | 2006-09-06 02:42:25 +0000 | [diff] [blame] | 173 | elif type == TType.DOUBLE: |
Mark Slee | 4ac459f | 2006-10-25 21:39:01 +0000 | [diff] [blame] | 174 | self.readDouble() |
Mark Slee | cde2b61 | 2006-09-03 21:13:07 +0000 | [diff] [blame] | 175 | elif type == TType.STRING: |
Mark Slee | 4ac459f | 2006-10-25 21:39:01 +0000 | [diff] [blame] | 176 | self.readString() |
Mark Slee | cde2b61 | 2006-09-03 21:13:07 +0000 | [diff] [blame] | 177 | elif type == TType.STRUCT: |
Mark Slee | 4ac459f | 2006-10-25 21:39:01 +0000 | [diff] [blame] | 178 | name = self.readStructBegin() |
Mark Slee | cde2b61 | 2006-09-03 21:13:07 +0000 | [diff] [blame] | 179 | while True: |
Mark Slee | 4ac459f | 2006-10-25 21:39:01 +0000 | [diff] [blame] | 180 | (name, type, id) = self.readFieldBegin() |
Mark Slee | cde2b61 | 2006-09-03 21:13:07 +0000 | [diff] [blame] | 181 | if type == TType.STOP: |
| 182 | break |
Mark Slee | 4ac459f | 2006-10-25 21:39:01 +0000 | [diff] [blame] | 183 | self.skip(type) |
| 184 | self.readFieldEnd() |
| 185 | self.readStructEnd() |
Mark Slee | cde2b61 | 2006-09-03 21:13:07 +0000 | [diff] [blame] | 186 | elif type == TType.MAP: |
Mark Slee | 4ac459f | 2006-10-25 21:39:01 +0000 | [diff] [blame] | 187 | (ktype, vtype, size) = self.readMapBegin() |
Mark Slee | cde2b61 | 2006-09-03 21:13:07 +0000 | [diff] [blame] | 188 | for i in range(size): |
Mark Slee | 4ac459f | 2006-10-25 21:39:01 +0000 | [diff] [blame] | 189 | self.skip(ktype) |
| 190 | self.skip(vtype) |
| 191 | self.readMapEnd() |
Mark Slee | cde2b61 | 2006-09-03 21:13:07 +0000 | [diff] [blame] | 192 | elif type == TType.SET: |
Mark Slee | 4ac459f | 2006-10-25 21:39:01 +0000 | [diff] [blame] | 193 | (etype, size) = self.readSetBegin() |
Mark Slee | cde2b61 | 2006-09-03 21:13:07 +0000 | [diff] [blame] | 194 | for i in range(size): |
Mark Slee | 4ac459f | 2006-10-25 21:39:01 +0000 | [diff] [blame] | 195 | self.skip(etype) |
| 196 | self.readSetEnd() |
Mark Slee | cde2b61 | 2006-09-03 21:13:07 +0000 | [diff] [blame] | 197 | elif type == TType.LIST: |
Mark Slee | 4ac459f | 2006-10-25 21:39:01 +0000 | [diff] [blame] | 198 | (etype, size) = self.readListBegin() |
Mark Slee | cde2b61 | 2006-09-03 21:13:07 +0000 | [diff] [blame] | 199 | for i in range(size): |
Mark Slee | 4ac459f | 2006-10-25 21:39:01 +0000 | [diff] [blame] | 200 | self.skip(etype) |
| 201 | self.readListEnd() |
Mark Slee | cde2b61 | 2006-09-03 21:13:07 +0000 | [diff] [blame] | 202 | |
Bryan Duxbury | 6972041 | 2012-01-03 17:32:30 +0000 | [diff] [blame^] | 203 | # tuple of: ( 'reader method' name, is_container bool, 'writer_method' name ) |
Roger Meier | f4eec7a | 2011-09-11 18:16:21 +0000 | [diff] [blame] | 204 | _TTYPE_HANDLERS = ( |
Bryan Duxbury | 6972041 | 2012-01-03 17:32:30 +0000 | [diff] [blame^] | 205 | (None, None, False), # 0 TType.STOP |
| 206 | (None, None, False), # 1 TType.VOID # TODO: handle void? |
| 207 | ('readBool', 'writeBool', False), # 2 TType.BOOL |
| 208 | ('readByte', 'writeByte', False), # 3 TType.BYTE and I08 |
| 209 | ('readDouble', 'writeDouble', False), # 4 TType.DOUBLE |
| 210 | (None, None, False), # 5 undefined |
| 211 | ('readI16', 'writeI16', False), # 6 TType.I16 |
| 212 | (None, None, False), # 7 undefined |
| 213 | ('readI32', 'writeI32', False), # 8 TType.I32 |
| 214 | (None, None, False), # 9 undefined |
| 215 | ('readI64', 'writeI64', False), # 10 TType.I64 |
| 216 | ('readString', 'writeString', False), # 11 TType.STRING and UTF7 |
| 217 | ('readContainerStruct', 'writeContainerStruct', True), # 12 *.STRUCT |
| 218 | ('readContainerMap', 'writeContainerMap', True), # 13 TType.MAP |
| 219 | ('readContainerSet', 'writeContainerSet', True), # 14 TType.SET |
| 220 | ('readContainerList', 'writeContainerList', True), # 15 TType.LIST |
| 221 | (None, None, False), # 16 TType.UTF8 # TODO: handle utf8 types? |
| 222 | (None, None, False) # 17 TType.UTF16 # TODO: handle utf16 types? |
Roger Meier | f4eec7a | 2011-09-11 18:16:21 +0000 | [diff] [blame] | 223 | ) |
| 224 | |
| 225 | def readFieldByTType(self, ttype, spec): |
| 226 | try: |
| 227 | (r_handler, w_handler, is_container) = self._TTYPE_HANDLERS[ttype] |
| 228 | except IndexError: |
| 229 | raise TProtocolException(type=TProtocolException.INVALID_DATA, |
| 230 | message='Invalid field type %d' % (ttype)) |
| 231 | if r_handler is None: |
| 232 | raise TProtocolException(type=TProtocolException.INVALID_DATA, |
| 233 | message='Invalid field type %d' % (ttype)) |
| 234 | reader = getattr(self, r_handler) |
| 235 | if not is_container: |
| 236 | return reader() |
| 237 | return reader(spec) |
| 238 | |
| 239 | def readContainerList(self, spec): |
| 240 | results = [] |
| 241 | ttype, tspec = spec[0], spec[1] |
| 242 | r_handler = self._TTYPE_HANDLERS[ttype][0] |
| 243 | reader = getattr(self, r_handler) |
| 244 | (list_type, list_len) = self.readListBegin() |
| 245 | if tspec is None: |
| 246 | # list values are simple types |
| 247 | for idx in xrange(list_len): |
| 248 | results.append(reader()) |
| 249 | else: |
| 250 | # this is like an inlined readFieldByTType |
| 251 | container_reader = self._TTYPE_HANDLERS[list_type][0] |
| 252 | val_reader = getattr(self, container_reader) |
| 253 | for idx in xrange(list_len): |
| 254 | val = val_reader(tspec) |
| 255 | results.append(val) |
| 256 | self.readListEnd() |
| 257 | return results |
| 258 | |
| 259 | def readContainerSet(self, spec): |
| 260 | results = set() |
| 261 | ttype, tspec = spec[0], spec[1] |
| 262 | r_handler = self._TTYPE_HANDLERS[ttype][0] |
| 263 | reader = getattr(self, r_handler) |
| 264 | (set_type, set_len) = self.readSetBegin() |
| 265 | if tspec is None: |
| 266 | # set members are simple types |
| 267 | for idx in xrange(set_len): |
| 268 | results.add(reader()) |
| 269 | else: |
| 270 | container_reader = self._TTYPE_HANDLERS[set_type][0] |
| 271 | val_reader = getattr(self, container_reader) |
| 272 | for idx in xrange(set_len): |
Bryan Duxbury | 6972041 | 2012-01-03 17:32:30 +0000 | [diff] [blame^] | 273 | results.add(val_reader(tspec)) |
Roger Meier | f4eec7a | 2011-09-11 18:16:21 +0000 | [diff] [blame] | 274 | self.readSetEnd() |
| 275 | return results |
| 276 | |
| 277 | def readContainerStruct(self, spec): |
| 278 | (obj_class, obj_spec) = spec |
| 279 | obj = obj_class() |
| 280 | obj.read(self) |
| 281 | return obj |
Bryan Duxbury | 6972041 | 2012-01-03 17:32:30 +0000 | [diff] [blame^] | 282 | |
Roger Meier | f4eec7a | 2011-09-11 18:16:21 +0000 | [diff] [blame] | 283 | def readContainerMap(self, spec): |
| 284 | results = dict() |
| 285 | key_ttype, key_spec = spec[0], spec[1] |
| 286 | val_ttype, val_spec = spec[2], spec[3] |
| 287 | (map_ktype, map_vtype, map_len) = self.readMapBegin() |
Bryan Duxbury | 6972041 | 2012-01-03 17:32:30 +0000 | [diff] [blame^] | 288 | # TODO: compare types we just decoded with thrift_spec and |
| 289 | # abort/skip if types disagree |
Roger Meier | f4eec7a | 2011-09-11 18:16:21 +0000 | [diff] [blame] | 290 | key_reader = getattr(self, self._TTYPE_HANDLERS[key_ttype][0]) |
| 291 | val_reader = getattr(self, self._TTYPE_HANDLERS[val_ttype][0]) |
| 292 | # list values are simple types |
| 293 | for idx in xrange(map_len): |
| 294 | if key_spec is None: |
| 295 | k_val = key_reader() |
| 296 | else: |
| 297 | k_val = self.readFieldByTType(key_ttype, key_spec) |
| 298 | if val_spec is None: |
| 299 | v_val = val_reader() |
| 300 | else: |
| 301 | v_val = self.readFieldByTType(val_ttype, val_spec) |
Bryan Duxbury | 6972041 | 2012-01-03 17:32:30 +0000 | [diff] [blame^] | 302 | # this raises a TypeError with unhashable keys types |
| 303 | # i.e. this fails: d=dict(); d[[0,1]] = 2 |
Roger Meier | f4eec7a | 2011-09-11 18:16:21 +0000 | [diff] [blame] | 304 | results[k_val] = v_val |
| 305 | self.readMapEnd() |
| 306 | return results |
| 307 | |
| 308 | def readStruct(self, obj, thrift_spec): |
| 309 | self.readStructBegin() |
| 310 | while True: |
| 311 | (fname, ftype, fid) = self.readFieldBegin() |
| 312 | if ftype == TType.STOP: |
| 313 | break |
| 314 | try: |
| 315 | field = thrift_spec[fid] |
| 316 | except IndexError: |
| 317 | self.skip(ftype) |
| 318 | else: |
| 319 | if field is not None and ftype == field[1]: |
| 320 | fname = field[2] |
| 321 | fspec = field[3] |
| 322 | val = self.readFieldByTType(ftype, fspec) |
| 323 | setattr(obj, fname, val) |
| 324 | else: |
| 325 | self.skip(ftype) |
| 326 | self.readFieldEnd() |
| 327 | self.readStructEnd() |
| 328 | |
| 329 | def writeContainerStruct(self, val, spec): |
| 330 | val.write(self) |
| 331 | |
| 332 | def writeContainerList(self, val, spec): |
| 333 | self.writeListBegin(spec[0], len(val)) |
Bryan Duxbury | 6972041 | 2012-01-03 17:32:30 +0000 | [diff] [blame^] | 334 | r_handler, w_handler, is_container = self._TTYPE_HANDLERS[spec[0]] |
Roger Meier | f4eec7a | 2011-09-11 18:16:21 +0000 | [diff] [blame] | 335 | e_writer = getattr(self, w_handler) |
| 336 | if not is_container: |
| 337 | for elem in val: |
| 338 | e_writer(elem) |
| 339 | else: |
| 340 | for elem in val: |
| 341 | e_writer(elem, spec[1]) |
| 342 | self.writeListEnd() |
| 343 | |
| 344 | def writeContainerSet(self, val, spec): |
| 345 | self.writeSetBegin(spec[0], len(val)) |
| 346 | r_handler, w_handler, is_container = self._TTYPE_HANDLERS[spec[0]] |
| 347 | e_writer = getattr(self, w_handler) |
| 348 | if not is_container: |
| 349 | for elem in val: |
| 350 | e_writer(elem) |
| 351 | else: |
| 352 | for elem in val: |
| 353 | e_writer(elem, spec[1]) |
| 354 | self.writeSetEnd() |
| 355 | |
| 356 | def writeContainerMap(self, val, spec): |
| 357 | k_type = spec[0] |
| 358 | v_type = spec[2] |
| 359 | ignore, ktype_name, k_is_container = self._TTYPE_HANDLERS[k_type] |
| 360 | ignore, vtype_name, v_is_container = self._TTYPE_HANDLERS[v_type] |
| 361 | k_writer = getattr(self, ktype_name) |
| 362 | v_writer = getattr(self, vtype_name) |
| 363 | self.writeMapBegin(k_type, v_type, len(val)) |
| 364 | for m_key, m_val in val.iteritems(): |
| 365 | if not k_is_container: |
| 366 | k_writer(m_key) |
| 367 | else: |
| 368 | k_writer(m_key, spec[1]) |
| 369 | if not v_is_container: |
| 370 | v_writer(m_val) |
| 371 | else: |
| 372 | v_writer(m_val, spec[3]) |
| 373 | self.writeMapEnd() |
| 374 | |
| 375 | def writeStruct(self, obj, thrift_spec): |
| 376 | self.writeStructBegin(obj.__class__.__name__) |
| 377 | for field in thrift_spec: |
| 378 | if field is None: |
| 379 | continue |
| 380 | fname = field[2] |
| 381 | val = getattr(obj, fname) |
| 382 | if val is None: |
| 383 | # skip writing out unset fields |
| 384 | continue |
| 385 | fid = field[0] |
| 386 | ftype = field[1] |
| 387 | fspec = field[3] |
| 388 | # get the writer method for this value |
| 389 | self.writeFieldBegin(fname, ftype, fid) |
| 390 | self.writeFieldByTType(ftype, val, fspec) |
| 391 | self.writeFieldEnd() |
| 392 | self.writeFieldStop() |
| 393 | self.writeStructEnd() |
| 394 | |
| 395 | def writeFieldByTType(self, ttype, val, spec): |
| 396 | r_handler, w_handler, is_container = self._TTYPE_HANDLERS[ttype] |
| 397 | writer = getattr(self, w_handler) |
| 398 | if is_container: |
| 399 | writer(val, spec) |
| 400 | else: |
| 401 | writer(val) |
| 402 | |
Bryan Duxbury | 6972041 | 2012-01-03 17:32:30 +0000 | [diff] [blame^] | 403 | |
Mark Slee | 4ac459f | 2006-10-25 21:39:01 +0000 | [diff] [blame] | 404 | class TProtocolFactory: |
Aditya Agarwal | 5c46819 | 2007-02-06 01:14:33 +0000 | [diff] [blame] | 405 | def getProtocol(self, trans): |
Mark Slee | 4ac459f | 2006-10-25 21:39:01 +0000 | [diff] [blame] | 406 | pass |