--- Downloads\codec.py 2014-09-17 15:17:35.175824100 +0100 +++ Desktop\codec.py 2014-09-17 15:16:21.000000000 +0100 @@ -8,9 +8,9 @@ # to you under the Apache License, Version 2.0 (the # "License"); you may not use this file except in compliance # with the License. You may obtain a copy of the License at -# +# # http://www.apache.org/licenses/LICENSE-2.0 -# +# # Unless required by applicable law or agreed to in writing, # software distributed under the License is distributed on an # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY @@ -176,6 +176,17 @@ self.encode_long(len(s)) self.write(s) + def decode_array(self): + size = self.decode_long() + start = self.nread + type = self.read(1) + result = [] + getValue = self._get_decoder_by_amqp_type(type) + + while self.nread - start < size: + result.append(getValue()) + return result + def decode_table(self): size = self.decode_long() start = self.nread @@ -183,19 +194,30 @@ while self.nread - start < size: key = self.decode_shortstr() type = self.read(1) - if type == "S": - value = self.decode_longstr() - elif type == "I": - value = self.decode_long() - elif type == "F": - value = self.decode_table() - elif type == "t": - value = (self.decode_octet() != 0) - else: - raise ValueError(repr(type)) - result[key] = value + try: + getValue = self._get_decoder_by_amqp_type(type) + except ValueError as err: + raise ValueError("{}::{}".format(key, err)) + result[key] = getValue() return result + def _get_decoder_by_amqp_type(self, type): + if type == "S": + rv = self.decode_longstr + elif type == "I": + rv = self.decode_long + elif type == "F": + rv = self.decode_table + elif type == "t": + rv = lambda: (self.decode_octet() != 0) + elif type == "T": + rv = self.decode_timestamp + elif type == "A": + rv = self.decode_array + else: + raise ValueError(repr(type)) + return rv + def test(type, value): if isinstance(value, (list, tuple)): values = value