def test_binary_value(): value = c_uamqp.binary_value(bytearray([50])) assert len(value) == 1 assert value.value == bytearray([50]) assert value.type == c_uamqp.AMQPType.BinaryValue assert str(value) == "<32>" value = c_uamqp.binary_value(b'Test') assert len(value) == 4 assert value.value == b'Test' assert value.type == c_uamqp.AMQPType.BinaryValue assert str(value) == "<54 65 73 74>" value = c_uamqp.binary_value(bytearray(b'Test')) assert len(value) == 4 assert value.value == b'Test' assert value.type == c_uamqp.AMQPType.BinaryValue assert str(value) == "<54 65 73 74>" payload_hex = [ '00', '53', '72', 'c1', '28', '02', 'a3', '1c', '78', '2d', '6f', '70', '74', '2d', '73', '63', '68', '65', '64', '75', '6c', '65', '64', '2d', '65', '6e', '71', '75', '65', '75', '65', '2d', '74', '69', '6d', '65', '83', '00', '00', '01', '66', 'cc', '90', 'e5', 'a0', '00', '53', '73', 'c0', '27', '01', 'a1', '24', '65', '33', '61', '39', '38', '63', '32', '35', '2d', '34', '35', '37', '34', '2d', '34', '64', '62', '66', '2d', '61', '35', '62', '66', '2d', '32', '65', '35', '63', '64', '37', '66', '31', '39', '38', '38', '32', '00', '53', '75', 'a0', '0a', '68', '61', '6c', '6c', '6f', '77', '65', '65', '6e', '32'] payload = bytearray.fromhex(''.join(payload_hex)) value = c_uamqp.binary_value(payload) assert str(value) == "<{}>".format(' '.join(payload_hex)).upper() assert len(value) == 104 assert value.value == b"\x00Sr\xc1(\x02\xa3\x1cx-opt-scheduled-enqueue-time\x83\x00\x00\x01f\xcc\x90\xe5\xa0\x00Ss\xc0\'\x01\xa1$e3a98c25-4574-4dbf-a5bf-2e5cd7f19882\x00Su\xa0\nhalloween2"
def data_factory(value, encoding='UTF-8'): """Wrap a Python type in the equivalent C AMQP type. If the Python type has already been wrapped in a ~uamqp.types.AMQPType object - then this will be used to select the appropriate C type. - bool => c_uamqp.BoolValue - int => c_uamqp.IntValue, LongValue, DoubleValue - str => c_uamqp.StringValue - bytes => c_uamqp.BinaryValue - list/set/tuple => c_uamqp.ListValue - dict => c_uamqp.DictValue (AMQP map) - float => c_uamqp.DoubleValue - uuid.UUID => c_uamqp.UUIDValue :param value: The value to wrap. :type value: ~uamqp.types.AMQPType :rtype: uamqp.c_uamqp.AMQPValue """ result = None if value is None: result = c_uamqp.null_value() elif hasattr(value, 'c_data'): result = value.c_data elif isinstance(value, c_uamqp.AMQPValue): result = value elif isinstance(value, bool): result = c_uamqp.bool_value(value) elif isinstance(value, six.text_type): result = c_uamqp.string_value(value.encode(encoding)) elif isinstance(value, six.binary_type): result = c_uamqp.string_value(value) elif isinstance(value, uuid.UUID): result = c_uamqp.uuid_value(value) elif isinstance(value, bytearray): result = c_uamqp.binary_value(value) elif isinstance(value, six.integer_types): result = _convert_py_number(value) elif isinstance(value, float): result = c_uamqp.double_value(value) elif isinstance(value, dict): wrapped_dict = c_uamqp.dict_value() for key, item in value.items(): wrapped_dict[data_factory(key, encoding=encoding)] = data_factory( item, encoding=encoding) result = wrapped_dict elif isinstance(value, (list, set, tuple)): wrapped_list = c_uamqp.list_value() wrapped_list.size = len(value) for index, item in enumerate(value): wrapped_list[index] = data_factory(item, encoding=encoding) result = wrapped_list elif isinstance(value, datetime): timestamp = int((time.mktime(value.utctimetuple()) * 1000) + (value.microsecond / 1000)) result = c_uamqp.timestamp_value(timestamp) return result
def data_factory(value, encoding='UTF-8'): """Wrap a Python type in the equivalent C AMQP type. If the Python type has already been wrapped in a ~uamqp.types.AMQPType object - then this will be used to select the appropriate C type. - bool => c_uamqp.BoolValue - int => c_uamqp.IntValue - str => c_uamqp.StringValue - bytes => c_uamqp.BinaryValue - str (char) => c_uamqp.CharValue - list/set/tuple => c_uamqp.ListValue - dict => c_uamqp.DictValue (AMQP map) - float => c_uamqp.DoubleValue - uuid.UUID => c_uamqp.UUIDValue :param value: The value to wrap. :returns: c_uamqp.AMQPValue """ result = None if value is None: result = c_uamqp.null_value() elif isinstance(value, types.AMQPType): result = value.c_data elif isinstance(value, c_uamqp.AMQPValue): result = value elif isinstance(value, bool): result = c_uamqp.bool_value(value) elif isinstance(value, str) and len(value) == 1: result = c_uamqp.char_value(value.encode(encoding)) elif isinstance(value, str) and len(value) > 1: result = c_uamqp.string_value(value.encode(encoding)) elif isinstance(value, bytes): result = c_uamqp.string_value(value) elif isinstance(value, uuid.UUID): result = c_uamqp.uuid_value(value) elif isinstance(value, bytearray): result = c_uamqp.binary_value(value) elif isinstance(value, float): result = c_uamqp.double_value(value) elif isinstance(value, int): result = c_uamqp.int_value(value) elif isinstance(value, dict): wrapped_dict = c_uamqp.dict_value() for key, item in value.items(): wrapped_dict[data_factory(key, encoding=encoding)] = data_factory(item, encoding=encoding) result = wrapped_dict elif isinstance(value, (list, set, tuple)): wrapped_list = c_uamqp.list_value() wrapped_list.size = len(value) for index, item in enumerate(value): wrapped_list[index] = data_factory(item, encoding=encoding) result = wrapped_list return result