Exemplo n.º 1
0
    def set_filter(self,
                   value,
                   name=constants.STRING_FILTER,
                   descriptor=constants.STRING_FILTER):
        """Set a filter on the endpoint. Only one filter
        can be applied to an endpoint.

        :param value: The filter to apply to the endpoint. Set to None for a NULL filter.
        :type value: bytes or str or None
        :param name: The name of the filter. This will be encoded as
         an AMQP Symbol. By default this is set to b'apache.org:selector-filter:string'.
        :type name: bytes
        :param descriptor: The descriptor used if the filter is to be encoded as a described value.
         This will be encoded as an AMQP Symbol. By default this is set to b'apache.org:selector-filter:string'.
         Set to None if the filter should not be encoded as a described value.
        :type descriptor: bytes or None
        """
        value = value.encode(self._encoding) if isinstance(
            value, six.text_type) else value
        filter_set = c_uamqp.dict_value()
        filter_key = c_uamqp.symbol_value(name)
        filter_value = utils.data_factory(value, encoding=self._encoding)
        if value is not None and descriptor is not None:
            descriptor = c_uamqp.symbol_value(descriptor)
            filter_value = c_uamqp.described_value(descriptor, filter_value)

        filter_set[filter_key] = filter_value
        self._address.filter_set = filter_set
def test_delivery_rejected():
    rej_val = c_uamqp.Messaging.delivery_rejected(b'Failed', b'Test failure')
    assert rej_val.type == c_uamqp.AMQPType.CompositeType
    assert rej_val.size == 1
    assert rej_val[0].type == c_uamqp.AMQPType.CompositeType
    assert rej_val[0].size == 2
    assert str(rej_val[0][0]) == 'Failed'
    assert str(rej_val[0][1]) == 'Test failure'

    error = c_uamqp.dict_value()
    error_key = c_uamqp.string_value(b"key123")
    error_value = c_uamqp.string_value(b"value456")
    error[error_key] = error_value
    error_info = c_uamqp.create_fields(error)

    rej_val = c_uamqp.Messaging.delivery_rejected(b'Failed', b'Test failure',
                                                  error_info)
    assert rej_val.type == c_uamqp.AMQPType.CompositeType
    assert rej_val.size == 1
    assert rej_val[0].type == c_uamqp.AMQPType.CompositeType
    assert rej_val[0].size == 3
    assert str(rej_val[0][0]) == 'Failed'
    assert str(rej_val[0][1]) == 'Test failure'
    error_info = rej_val[0][2]
    assert error_info.type == c_uamqp.AMQPType.DictValue
    assert error_info[error_key] == error_value
Exemplo n.º 3
0
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
Exemplo n.º 4
0
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
Exemplo n.º 5
0
    def set_filter(self, value, name=constants.STRING_FILTER):
        """Set a filter on the endpoint. Only one filter
        can be applied to an endpoint.

        :param value: The filter to apply to the endpoint.
        :type value: bytes or str
        :param name: The name of the filter. This will be encoded as
         an AMQP Symbol and will also be used as the filter descriptor.
         By default this is set to b'apache.org:selector-filter:string'.
        :type name: bytes
        """
        value = value.encode(self._encoding) if isinstance(value,
                                                           str) else value
        filter_set = c_uamqp.dict_value()
        filter_key = c_uamqp.symbol_value(name)
        descriptor = c_uamqp.symbol_value(name)
        filter_value = c_uamqp.string_value(value)
        described_filter_value = c_uamqp.described_value(
            descriptor, filter_value)
        self._filters.append((descriptor, filter_value))
        filter_set[filter_key] = described_filter_value
        self._address.filter_set = filter_set
Exemplo n.º 6
0
def test_dict_value():
    value = c_uamqp.dict_value()
    assert value.type == c_uamqp.AMQPType.DictValue

    one = c_uamqp.string_value(b'One')
    two = c_uamqp.string_value(b'Two')
    i_one = c_uamqp.int_value(1)
    i_two = c_uamqp.int_value(2)
    value[one] = i_one
    value[two] = i_two

    assert len(value) == 2
    assert value[one].value == 1
    assert value[two].value == 2
    with pytest.raises(KeyError):
        value[c_uamqp.null_value()]

    assert value.get(0) == (one, i_one)
    assert value.get(1) == (two, i_two)
    with pytest.raises(IndexError):
        value.get(2)
    
    assert value.value == {b"One": 1, b"Two": 2}
    assert str(value) == "{[One:1],[Two:2]}"