Mark Slee | 89e2bb8 | 2007-03-01 00:20:36 +0000 | [diff] [blame] | 1 | # Copyright (c) 2006- Facebook |
| 2 | # Distributed under the Thrift Software License |
| 3 | # |
| 4 | # See accompanying file LICENSE or visit the Thrift site at: |
| 5 | # http://developers.facebook.com/thrift/ |
| 6 | |
Mark Slee | e74306a | 2007-02-21 05:38:12 +0000 | [diff] [blame] | 7 | from thrift.Thrift import * |
Mark Slee | cde2b61 | 2006-09-03 21:13:07 +0000 | [diff] [blame] | 8 | |
Mark Slee | 92195ae | 2007-02-21 05:16:30 +0000 | [diff] [blame] | 9 | class TProtocolException(TException): |
| 10 | |
| 11 | """Custom Protocol Exception class""" |
| 12 | |
| 13 | UNKNOWN = 0 |
| 14 | INVALID_DATA = 1 |
| 15 | NEGATIVE_SIZE = 2 |
| 16 | SIZE_LIMIT = 3 |
Mark Slee | 808454e | 2007-06-20 21:51:57 +0000 | [diff] [blame] | 17 | BAD_VERSION = 4 |
Mark Slee | 92195ae | 2007-02-21 05:16:30 +0000 | [diff] [blame] | 18 | |
| 19 | def __init__(self, type=UNKNOWN, message=None): |
| 20 | TException.__init__(self, message) |
| 21 | self.type = type |
| 22 | |
Mark Slee | cde2b61 | 2006-09-03 21:13:07 +0000 | [diff] [blame] | 23 | class TProtocolBase: |
| 24 | |
| 25 | """Base class for Thrift protocol driver.""" |
| 26 | |
Aditya Agarwal | 5c46819 | 2007-02-06 01:14:33 +0000 | [diff] [blame] | 27 | def __init__(self, trans): |
| 28 | self.trans = trans |
Mark Slee | 4ac459f | 2006-10-25 21:39:01 +0000 | [diff] [blame] | 29 | |
| 30 | def writeMessageBegin(self, name, type, seqid): |
Mark Slee | cde2b61 | 2006-09-03 21:13:07 +0000 | [diff] [blame] | 31 | pass |
| 32 | |
Mark Slee | 4ac459f | 2006-10-25 21:39:01 +0000 | [diff] [blame] | 33 | def writeMessageEnd(self): |
Mark Slee | cde2b61 | 2006-09-03 21:13:07 +0000 | [diff] [blame] | 34 | pass |
| 35 | |
Mark Slee | 4ac459f | 2006-10-25 21:39:01 +0000 | [diff] [blame] | 36 | def writeStructBegin(self, name): |
Mark Slee | cde2b61 | 2006-09-03 21:13:07 +0000 | [diff] [blame] | 37 | pass |
| 38 | |
Mark Slee | 4ac459f | 2006-10-25 21:39:01 +0000 | [diff] [blame] | 39 | def writeStructEnd(self): |
Mark Slee | cde2b61 | 2006-09-03 21:13:07 +0000 | [diff] [blame] | 40 | pass |
| 41 | |
Mark Slee | 4ac459f | 2006-10-25 21:39:01 +0000 | [diff] [blame] | 42 | def writeFieldBegin(self, name, type, id): |
Mark Slee | cde2b61 | 2006-09-03 21:13:07 +0000 | [diff] [blame] | 43 | pass |
| 44 | |
Mark Slee | 4ac459f | 2006-10-25 21:39:01 +0000 | [diff] [blame] | 45 | def writeFieldEnd(self): |
Mark Slee | cde2b61 | 2006-09-03 21:13:07 +0000 | [diff] [blame] | 46 | pass |
| 47 | |
Mark Slee | 4ac459f | 2006-10-25 21:39:01 +0000 | [diff] [blame] | 48 | def writeFieldStop(self): |
Mark Slee | cde2b61 | 2006-09-03 21:13:07 +0000 | [diff] [blame] | 49 | pass |
| 50 | |
Mark Slee | 4ac459f | 2006-10-25 21:39:01 +0000 | [diff] [blame] | 51 | def writeMapBegin(self, ktype, vtype, size): |
Mark Slee | cde2b61 | 2006-09-03 21:13:07 +0000 | [diff] [blame] | 52 | pass |
| 53 | |
Mark Slee | 4ac459f | 2006-10-25 21:39:01 +0000 | [diff] [blame] | 54 | def writeMapEnd(self): |
Mark Slee | cde2b61 | 2006-09-03 21:13:07 +0000 | [diff] [blame] | 55 | pass |
| 56 | |
Mark Slee | 4ac459f | 2006-10-25 21:39:01 +0000 | [diff] [blame] | 57 | def writeListBegin(self, etype, size): |
Mark Slee | cde2b61 | 2006-09-03 21:13:07 +0000 | [diff] [blame] | 58 | pass |
| 59 | |
Mark Slee | 4ac459f | 2006-10-25 21:39:01 +0000 | [diff] [blame] | 60 | def writeListEnd(self): |
Mark Slee | cde2b61 | 2006-09-03 21:13:07 +0000 | [diff] [blame] | 61 | pass |
| 62 | |
Mark Slee | 4ac459f | 2006-10-25 21:39:01 +0000 | [diff] [blame] | 63 | def writeSetBegin(self, etype, size): |
Mark Slee | cde2b61 | 2006-09-03 21:13:07 +0000 | [diff] [blame] | 64 | pass |
| 65 | |
Mark Slee | 4ac459f | 2006-10-25 21:39:01 +0000 | [diff] [blame] | 66 | def writeSetEnd(self): |
Mark Slee | cde2b61 | 2006-09-03 21:13:07 +0000 | [diff] [blame] | 67 | pass |
| 68 | |
Mark Slee | 4ac459f | 2006-10-25 21:39:01 +0000 | [diff] [blame] | 69 | def writeBool(self, bool): |
Mark Slee | cde2b61 | 2006-09-03 21:13:07 +0000 | [diff] [blame] | 70 | pass |
| 71 | |
Mark Slee | 4ac459f | 2006-10-25 21:39:01 +0000 | [diff] [blame] | 72 | def writeByte(self, byte): |
Mark Slee | cde2b61 | 2006-09-03 21:13:07 +0000 | [diff] [blame] | 73 | pass |
| 74 | |
Mark Slee | 4ac459f | 2006-10-25 21:39:01 +0000 | [diff] [blame] | 75 | def writeI16(self, i16): |
Mark Slee | cde2b61 | 2006-09-03 21:13:07 +0000 | [diff] [blame] | 76 | pass |
| 77 | |
Mark Slee | 4ac459f | 2006-10-25 21:39:01 +0000 | [diff] [blame] | 78 | def writeI32(self, i32): |
Mark Slee | cde2b61 | 2006-09-03 21:13:07 +0000 | [diff] [blame] | 79 | pass |
| 80 | |
Mark Slee | 4ac459f | 2006-10-25 21:39:01 +0000 | [diff] [blame] | 81 | def writeI64(self, i64): |
Mark Slee | cde2b61 | 2006-09-03 21:13:07 +0000 | [diff] [blame] | 82 | pass |
| 83 | |
Mark Slee | 4ac459f | 2006-10-25 21:39:01 +0000 | [diff] [blame] | 84 | def writeDouble(self, dub): |
Mark Slee | c98d050 | 2006-09-06 02:42:25 +0000 | [diff] [blame] | 85 | pass |
| 86 | |
Mark Slee | 4ac459f | 2006-10-25 21:39:01 +0000 | [diff] [blame] | 87 | def writeString(self, str): |
Mark Slee | cde2b61 | 2006-09-03 21:13:07 +0000 | [diff] [blame] | 88 | pass |
| 89 | |
Mark Slee | 4ac459f | 2006-10-25 21:39:01 +0000 | [diff] [blame] | 90 | def readMessageBegin(self): |
Mark Slee | cde2b61 | 2006-09-03 21:13:07 +0000 | [diff] [blame] | 91 | pass |
| 92 | |
Mark Slee | 4ac459f | 2006-10-25 21:39:01 +0000 | [diff] [blame] | 93 | def readMessageEnd(self): |
Mark Slee | cde2b61 | 2006-09-03 21:13:07 +0000 | [diff] [blame] | 94 | pass |
| 95 | |
Mark Slee | 4ac459f | 2006-10-25 21:39:01 +0000 | [diff] [blame] | 96 | def readStructBegin(self): |
Mark Slee | cde2b61 | 2006-09-03 21:13:07 +0000 | [diff] [blame] | 97 | pass |
| 98 | |
Mark Slee | 4ac459f | 2006-10-25 21:39:01 +0000 | [diff] [blame] | 99 | def readStructEnd(self): |
Mark Slee | cde2b61 | 2006-09-03 21:13:07 +0000 | [diff] [blame] | 100 | pass |
| 101 | |
Mark Slee | 4ac459f | 2006-10-25 21:39:01 +0000 | [diff] [blame] | 102 | def readFieldBegin(self): |
Mark Slee | cde2b61 | 2006-09-03 21:13:07 +0000 | [diff] [blame] | 103 | pass |
| 104 | |
Mark Slee | 4ac459f | 2006-10-25 21:39:01 +0000 | [diff] [blame] | 105 | def readFieldEnd(self): |
Mark Slee | cde2b61 | 2006-09-03 21:13:07 +0000 | [diff] [blame] | 106 | pass |
| 107 | |
Mark Slee | 4ac459f | 2006-10-25 21:39:01 +0000 | [diff] [blame] | 108 | def readMapBegin(self): |
Mark Slee | cde2b61 | 2006-09-03 21:13:07 +0000 | [diff] [blame] | 109 | pass |
| 110 | |
Mark Slee | 4ac459f | 2006-10-25 21:39:01 +0000 | [diff] [blame] | 111 | def readMapEnd(self): |
Mark Slee | cde2b61 | 2006-09-03 21:13:07 +0000 | [diff] [blame] | 112 | pass |
| 113 | |
Mark Slee | 4ac459f | 2006-10-25 21:39:01 +0000 | [diff] [blame] | 114 | def readListBegin(self): |
Mark Slee | cde2b61 | 2006-09-03 21:13:07 +0000 | [diff] [blame] | 115 | pass |
| 116 | |
Mark Slee | 4ac459f | 2006-10-25 21:39:01 +0000 | [diff] [blame] | 117 | def readListEnd(self): |
Mark Slee | cde2b61 | 2006-09-03 21:13:07 +0000 | [diff] [blame] | 118 | pass |
| 119 | |
Mark Slee | 4ac459f | 2006-10-25 21:39:01 +0000 | [diff] [blame] | 120 | def readSetBegin(self): |
Mark Slee | cde2b61 | 2006-09-03 21:13:07 +0000 | [diff] [blame] | 121 | pass |
| 122 | |
Mark Slee | 4ac459f | 2006-10-25 21:39:01 +0000 | [diff] [blame] | 123 | def readSetEnd(self): |
Mark Slee | cde2b61 | 2006-09-03 21:13:07 +0000 | [diff] [blame] | 124 | pass |
| 125 | |
Mark Slee | 4ac459f | 2006-10-25 21:39:01 +0000 | [diff] [blame] | 126 | def readBool(self): |
Mark Slee | cde2b61 | 2006-09-03 21:13:07 +0000 | [diff] [blame] | 127 | pass |
| 128 | |
Mark Slee | 4ac459f | 2006-10-25 21:39:01 +0000 | [diff] [blame] | 129 | def readByte(self): |
Mark Slee | cde2b61 | 2006-09-03 21:13:07 +0000 | [diff] [blame] | 130 | pass |
| 131 | |
Mark Slee | 4ac459f | 2006-10-25 21:39:01 +0000 | [diff] [blame] | 132 | def readI16(self): |
Mark Slee | cde2b61 | 2006-09-03 21:13:07 +0000 | [diff] [blame] | 133 | pass |
| 134 | |
Mark Slee | 4ac459f | 2006-10-25 21:39:01 +0000 | [diff] [blame] | 135 | def readI32(self): |
Mark Slee | cde2b61 | 2006-09-03 21:13:07 +0000 | [diff] [blame] | 136 | pass |
| 137 | |
Mark Slee | 4ac459f | 2006-10-25 21:39:01 +0000 | [diff] [blame] | 138 | def readI64(self): |
Mark Slee | cde2b61 | 2006-09-03 21:13:07 +0000 | [diff] [blame] | 139 | pass |
| 140 | |
Mark Slee | 4ac459f | 2006-10-25 21:39:01 +0000 | [diff] [blame] | 141 | def readDouble(self): |
Mark Slee | c98d050 | 2006-09-06 02:42:25 +0000 | [diff] [blame] | 142 | pass |
| 143 | |
Mark Slee | 4ac459f | 2006-10-25 21:39:01 +0000 | [diff] [blame] | 144 | def readString(self): |
Mark Slee | cde2b61 | 2006-09-03 21:13:07 +0000 | [diff] [blame] | 145 | pass |
| 146 | |
Mark Slee | 4ac459f | 2006-10-25 21:39:01 +0000 | [diff] [blame] | 147 | def skip(self, type): |
Mark Slee | cde2b61 | 2006-09-03 21:13:07 +0000 | [diff] [blame] | 148 | if type == TType.STOP: |
| 149 | return |
| 150 | elif type == TType.BOOL: |
Mark Slee | 4ac459f | 2006-10-25 21:39:01 +0000 | [diff] [blame] | 151 | self.readBool() |
Mark Slee | cde2b61 | 2006-09-03 21:13:07 +0000 | [diff] [blame] | 152 | elif type == TType.BYTE: |
Mark Slee | 4ac459f | 2006-10-25 21:39:01 +0000 | [diff] [blame] | 153 | self.readByte() |
Mark Slee | cde2b61 | 2006-09-03 21:13:07 +0000 | [diff] [blame] | 154 | elif type == TType.I16: |
Mark Slee | 4ac459f | 2006-10-25 21:39:01 +0000 | [diff] [blame] | 155 | self.readI16() |
Mark Slee | cde2b61 | 2006-09-03 21:13:07 +0000 | [diff] [blame] | 156 | elif type == TType.I32: |
Mark Slee | 4ac459f | 2006-10-25 21:39:01 +0000 | [diff] [blame] | 157 | self.readI32() |
Mark Slee | cde2b61 | 2006-09-03 21:13:07 +0000 | [diff] [blame] | 158 | elif type == TType.I64: |
Mark Slee | 4ac459f | 2006-10-25 21:39:01 +0000 | [diff] [blame] | 159 | self.readI64() |
Mark Slee | c98d050 | 2006-09-06 02:42:25 +0000 | [diff] [blame] | 160 | elif type == TType.DOUBLE: |
Mark Slee | 4ac459f | 2006-10-25 21:39:01 +0000 | [diff] [blame] | 161 | self.readDouble() |
Mark Slee | cde2b61 | 2006-09-03 21:13:07 +0000 | [diff] [blame] | 162 | elif type == TType.STRING: |
Mark Slee | 4ac459f | 2006-10-25 21:39:01 +0000 | [diff] [blame] | 163 | self.readString() |
Mark Slee | cde2b61 | 2006-09-03 21:13:07 +0000 | [diff] [blame] | 164 | elif type == TType.STRUCT: |
Mark Slee | 4ac459f | 2006-10-25 21:39:01 +0000 | [diff] [blame] | 165 | name = self.readStructBegin() |
Mark Slee | cde2b61 | 2006-09-03 21:13:07 +0000 | [diff] [blame] | 166 | while True: |
Mark Slee | 4ac459f | 2006-10-25 21:39:01 +0000 | [diff] [blame] | 167 | (name, type, id) = self.readFieldBegin() |
Mark Slee | cde2b61 | 2006-09-03 21:13:07 +0000 | [diff] [blame] | 168 | if type == TType.STOP: |
| 169 | break |
Mark Slee | 4ac459f | 2006-10-25 21:39:01 +0000 | [diff] [blame] | 170 | self.skip(type) |
| 171 | self.readFieldEnd() |
| 172 | self.readStructEnd() |
Mark Slee | cde2b61 | 2006-09-03 21:13:07 +0000 | [diff] [blame] | 173 | elif type == TType.MAP: |
Mark Slee | 4ac459f | 2006-10-25 21:39:01 +0000 | [diff] [blame] | 174 | (ktype, vtype, size) = self.readMapBegin() |
Mark Slee | cde2b61 | 2006-09-03 21:13:07 +0000 | [diff] [blame] | 175 | for i in range(size): |
Mark Slee | 4ac459f | 2006-10-25 21:39:01 +0000 | [diff] [blame] | 176 | self.skip(ktype) |
| 177 | self.skip(vtype) |
| 178 | self.readMapEnd() |
Mark Slee | cde2b61 | 2006-09-03 21:13:07 +0000 | [diff] [blame] | 179 | elif type == TType.SET: |
Mark Slee | 4ac459f | 2006-10-25 21:39:01 +0000 | [diff] [blame] | 180 | (etype, size) = self.readSetBegin() |
Mark Slee | cde2b61 | 2006-09-03 21:13:07 +0000 | [diff] [blame] | 181 | for i in range(size): |
Mark Slee | 4ac459f | 2006-10-25 21:39:01 +0000 | [diff] [blame] | 182 | self.skip(etype) |
| 183 | self.readSetEnd() |
Mark Slee | cde2b61 | 2006-09-03 21:13:07 +0000 | [diff] [blame] | 184 | elif type == TType.LIST: |
Mark Slee | 4ac459f | 2006-10-25 21:39:01 +0000 | [diff] [blame] | 185 | (etype, size) = self.readListBegin() |
Mark Slee | cde2b61 | 2006-09-03 21:13:07 +0000 | [diff] [blame] | 186 | for i in range(size): |
Mark Slee | 4ac459f | 2006-10-25 21:39:01 +0000 | [diff] [blame] | 187 | self.skip(etype) |
| 188 | self.readListEnd() |
Mark Slee | cde2b61 | 2006-09-03 21:13:07 +0000 | [diff] [blame] | 189 | |
Mark Slee | 4ac459f | 2006-10-25 21:39:01 +0000 | [diff] [blame] | 190 | class TProtocolFactory: |
Aditya Agarwal | 5c46819 | 2007-02-06 01:14:33 +0000 | [diff] [blame] | 191 | def getProtocol(self, trans): |
Mark Slee | 4ac459f | 2006-10-25 21:39:01 +0000 | [diff] [blame] | 192 | pass |