def bind(self, destination='', source='', routing_key='', arguments=None): """Bind an Exchange. :param str destination: :param str source: :param str routing_key: :param dict arguments: :raises AMQPInvalidArgument: Invalid Parameters :raises AMQPChannelError: Raises if the channel encountered an error. :raises AMQPConnectionError: Raises if the connection encountered an error. :rtype: dict """ if not compatibility.is_string(destination): raise AMQPInvalidArgument('destination should be a string') elif not compatibility.is_string(source): raise AMQPInvalidArgument('source should be a string') elif not compatibility.is_string(routing_key): raise AMQPInvalidArgument('routing_key should be a string') elif arguments is not None and not isinstance(arguments, dict): raise AMQPInvalidArgument('arguments should be a dict or None') bind_frame = pamqp_exchange.Bind(destination=destination, source=source, routing_key=routing_key, arguments=arguments) return self._channel.rpc_request(bind_frame)
def _validate_publish_parameters(body, exchange, immediate, mandatory, properties, routing_key): """Validate Publish Parameters. :param bytes,str,unicode body: Message payload :param str routing_key: Message routing key :param str exchange: The exchange to publish the message to :param dict properties: Message properties :param bool mandatory: Requires the message is published :param bool immediate: Request immediate delivery :raises AMQPInvalidArgument: Invalid Parameters :return: """ if not compatibility.is_string(body): raise AMQPInvalidArgument('body should be a string') elif not compatibility.is_string(routing_key): raise AMQPInvalidArgument('routing_key should be a string') elif not compatibility.is_string(exchange): raise AMQPInvalidArgument('exchange should be a string') elif properties is not None and not isinstance(properties, dict): raise AMQPInvalidArgument('properties should be a dict or None') elif not isinstance(mandatory, bool): raise AMQPInvalidArgument('mandatory should be a boolean') elif not isinstance(immediate, bool): raise AMQPInvalidArgument('immediate should be a boolean')
def unbind(self, queue='', exchange='', routing_key='', arguments=None): """Unbind a Queue. :param str queue: Queue name :param str exchange: Exchange name :param str routing_key: The routing key used :param dict arguments: Unbind key/value arguments :raises AMQPInvalidArgument: Invalid Parameters :raises AMQPChannelError: Raises if the channel encountered an error. :raises AMQPConnectionError: Raises if the connection encountered an error. :rtype: dict """ if not compatibility.is_string(queue): raise AMQPInvalidArgument('queue should be a string') elif not compatibility.is_string(exchange): raise AMQPInvalidArgument('exchange should be a string') elif not compatibility.is_string(routing_key): raise AMQPInvalidArgument('routing_key should be a string') elif arguments is not None and not isinstance(arguments, dict): raise AMQPInvalidArgument('arguments should be a dict or None') unbind_frame = pamqp_queue.Unbind(queue=queue, exchange=exchange, routing_key=routing_key, arguments=arguments) return self._channel.rpc_request(unbind_frame)
def bind(self, destination='', source='', routing_key='', arguments=None): """Bind an Exchange. :param str destination: Exchange name :param str source: Exchange to bind to :param str routing_key: The routing key to use :param dict arguments: Bind key/value arguments :raises AMQPInvalidArgument: Invalid Parameters :raises AMQPChannelError: Raises if the channel encountered an error. :raises AMQPConnectionError: Raises if the connection encountered an error. :rtype: dict """ if not compatibility.is_string(destination): raise AMQPInvalidArgument('destination should be a string') elif not compatibility.is_string(source): raise AMQPInvalidArgument('source should be a string') elif not compatibility.is_string(routing_key): raise AMQPInvalidArgument('routing_key should be a string') elif arguments is not None and not isinstance(arguments, dict): raise AMQPInvalidArgument('arguments should be a dict or None') bind_frame = pamqp_exchange.Bind(destination=destination, source=source, routing_key=routing_key, arguments=arguments) return self._channel.rpc_request(bind_frame)
def _validate_publish_parameters(body, exchange, immediate, mandatory, properties, routing_key): """Validate Publish Parameters. :param bytes|str|unicode body: Message payload :param str routing_key: Message routing key :param str exchange: The exchange to publish the message to :param dict properties: Message properties :param bool mandatory: Requires the message is published :param bool immediate: Request immediate delivery :raises AMQPInvalidArgument: Invalid Parameters :return: """ if not compatibility.is_string(body): raise AMQPInvalidArgument('body should be a string') elif not compatibility.is_string(routing_key): raise AMQPInvalidArgument('routing_key should be a string') elif not compatibility.is_string(exchange): raise AMQPInvalidArgument('exchange should be a string') elif properties is not None and not isinstance(properties, dict): raise AMQPInvalidArgument('properties should be a dict or None') elif not isinstance(mandatory, bool): raise AMQPInvalidArgument('mandatory should be a boolean') elif not isinstance(immediate, bool): raise AMQPInvalidArgument('immediate should be a boolean')
def bind(self, queue='', exchange='', routing_key='', arguments=None): """Bind a Queue. :param str queue: :param str exchange: :param str routing_key: :param dict arguments: :raises AMQPInvalidArgument: Invalid Parameters :raises AMQPChannelError: Raises if the channel encountered an error. :raises AMQPConnectionError: Raises if the connection encountered an error. :rtype: dict """ if not compatibility.is_string(queue): raise AMQPInvalidArgument('queue should be a string') elif not compatibility.is_string(exchange): raise AMQPInvalidArgument('exchange should be a string') elif not compatibility.is_string(routing_key): raise AMQPInvalidArgument('routing_key should be a string') elif arguments is not None and not isinstance(arguments, dict): raise AMQPInvalidArgument('arguments should be a dict or None') bind_frame = pamqp_queue.Bind(queue=queue, exchange=exchange, routing_key=routing_key, arguments=arguments) return self._channel.rpc_request(bind_frame)
def declare(self, exchange='', exchange_type='direct', passive=False, durable=False, auto_delete=False, arguments=None): """Declare exchange. :param str exchange: :param str exchange_type: :param bool passive: :param bool durable: :param bool auto_delete: :param dict arguments: :rtype: dict """ if not compatibility.is_string(exchange): raise AMQPInvalidArgument('exchange should be a string') elif not compatibility.is_string(exchange_type): raise AMQPInvalidArgument('exchange_type should be a string') elif not isinstance(passive, bool): raise AMQPInvalidArgument('passive should be a boolean') elif not isinstance(durable, bool): raise AMQPInvalidArgument('durable should be a boolean') elif not isinstance(auto_delete, bool): raise AMQPInvalidArgument('auto_delete should be a boolean') elif arguments is not None and not isinstance(arguments, dict): raise AMQPInvalidArgument('arguments should be a dict or None') declare_frame = pamqp_exchange.Declare(exchange=exchange, exchange_type=exchange_type, passive=passive, durable=durable, auto_delete=auto_delete, arguments=arguments) return self._channel.rpc_request(declare_frame)
def _validate_publish_parameters(body, exchange, immediate, mandatory, properties, routing_key): """Validate Publish Parameters. :param str|unicode body: :param str routing_key: :param str exchange: :param dict properties: :param bool mandatory: :param bool immediate: :raises AMQPInvalidArgument: Invalid Parameters :return: """ if not compatibility.is_string(body): raise AMQPInvalidArgument('body should be a string') elif not compatibility.is_string(routing_key): raise AMQPInvalidArgument('routing_key should be a string') elif not compatibility.is_string(exchange): raise AMQPInvalidArgument('exchange should be a string') elif properties and not isinstance(properties, dict): raise AMQPInvalidArgument('properties should be a dict or None') elif not isinstance(mandatory, bool): raise AMQPInvalidArgument('mandatory should be a boolean') elif not isinstance(immediate, bool): raise AMQPInvalidArgument('immediate should be a boolean')
def consume(self, callback=None, queue='', consumer_tag='', exclusive=False, no_ack=False, no_local=False, arguments=None): """Start a queue consumer. :param function callback: Message callback :param str queue: Queue name :param str consumer_tag: Consumer tag :param bool no_local: Do not deliver own messages :param bool no_ack: No acknowledgement needed :param bool exclusive: Request exclusive access :param dict arguments: Arguments for declaration :raises AMQPInvalidArgument: Invalid Parameters :raises AMQPChannelError: Raises if the channel encountered an error. :raises AMQPConnectionError: Raises if the connection encountered an error. :returns: Consumer tag :rtype: str """ if not compatibility.is_string(queue): raise AMQPInvalidArgument('queue should be a string') elif not compatibility.is_string(consumer_tag): raise AMQPInvalidArgument('consumer_tag should be a string') elif not isinstance(exclusive, bool): raise AMQPInvalidArgument('exclusive should be a boolean') elif not isinstance(no_ack, bool): raise AMQPInvalidArgument('no_ack should be a boolean') elif not isinstance(no_local, bool): raise AMQPInvalidArgument('no_local should be a boolean') elif arguments is not None and not isinstance(arguments, dict): raise AMQPInvalidArgument('arguments should be a dict or None') self._channel.consumer_callback = callback consume_frame = pamqp_spec.Basic.Consume(queue=queue, consumer_tag=consumer_tag, exclusive=exclusive, no_local=no_local, no_ack=no_ack, arguments=arguments) result = self._channel.rpc_request(consume_frame) consumer_tag = result['consumer_tag'] self._channel.add_consumer_tag(consumer_tag) return consumer_tag
def get(self, queue='', no_ack=False, to_dict=False, auto_decode=True): """Fetch a single message. :param str queue: Queue name :param bool no_ack: No acknowledgement needed :param bool to_dict: Should incoming messages be converted to a dictionary before delivery. :param bool auto_decode: Auto-decode strings when possible. :raises AMQPInvalidArgument: Invalid Parameters :raises AMQPChannelError: Raises if the channel encountered an error. :raises AMQPConnectionError: Raises if the connection encountered an error. :returns: Returns a single message, as long as there is a message in the queue. If no message is available, returns None. :rtype: amqpstorm.Message,dict,None """ if not compatibility.is_string(queue): raise AMQPInvalidArgument('queue should be a string') elif not isinstance(no_ack, bool): raise AMQPInvalidArgument('no_ack should be a boolean') elif self._channel.consumer_tags: raise AMQPChannelError("Cannot call 'get' when channel is " "set to consume") get_frame = specification.Basic.Get(queue=queue, no_ack=no_ack) with self._channel.lock and self._channel.rpc.lock: message = self._get_message(get_frame, auto_decode=auto_decode) if message and to_dict: return message.to_dict() return message
def close(self, reply_code=0, reply_text=''): """Close Channel. :param int reply_code: Close reply code (e.g. 200) :param str reply_text: Close reply text :raises AMQPInvalidArgument: Invalid Parameters :raises AMQPChannelError: Raises if the channel encountered an error. :raises AMQPConnectionError: Raises if the connection encountered an error. :return: """ LOGGER.debug('Channel #%d Closing', self.channel_id) if not compatibility.is_integer(reply_code): raise AMQPInvalidArgument('reply_code should be an integer') elif not compatibility.is_string(reply_text): raise AMQPInvalidArgument('reply_text should be a string') if not self._connection.is_open or not self.is_open: self.remove_consumer_tag() self.set_state(self.CLOSED) return self.set_state(self.CLOSING) self.stop_consuming() self.rpc_request( pamqp_spec.Channel.Close(reply_code=reply_code, reply_text=reply_text)) del self._inbound[:] self.set_state(self.CLOSED) LOGGER.debug('Channel #%d Closed', self.channel_id)
def close(self, reply_code=0, reply_text=''): """Close Channel. :param int reply_code: Close reply code (e.g. 200) :param str reply_text: Close reply text :raises AMQPInvalidArgument: Invalid Parameters :raises AMQPChannelError: Raises if the channel encountered an error. :raises AMQPConnectionError: Raises if the connection encountered an error. :return: """ LOGGER.debug('Channel #%d Closing', self.channel_id) if not compatibility.is_integer(reply_code): raise AMQPInvalidArgument('reply_code should be an integer') elif not compatibility.is_string(reply_text): raise AMQPInvalidArgument('reply_text should be a string') if not self._connection.is_open or not self.is_open: self.remove_consumer_tag() self.set_state(self.CLOSED) return self.set_state(self.CLOSING) self.stop_consuming() self.rpc_request(pamqp_spec.Channel.Close( reply_code=reply_code, reply_text=reply_text)) del self._inbound[:] self.set_state(self.CLOSED) LOGGER.debug('Channel #%d Closed', self.channel_id)
def close(self, reply_code=200, reply_text=''): """Close Channel. :param int reply_code: Close reply code (e.g. 200) :param str reply_text: Close reply text :raises AMQPInvalidArgument: Invalid Parameters :raises AMQPChannelError: Raises if the channel encountered an error. :raises AMQPConnectionError: Raises if the connection encountered an error. :return: """ if not compatibility.is_integer(reply_code): raise AMQPInvalidArgument('reply_code should be an integer') elif not compatibility.is_string(reply_text): raise AMQPInvalidArgument('reply_text should be a string') try: if self._connection.is_closed or self.is_closed: self.stop_consuming() LOGGER.debug('Channel #%d forcefully Closed', self.channel_id) return self.set_state(self.CLOSING) LOGGER.debug('Channel #%d Closing', self.channel_id) self.stop_consuming() self.rpc_request(specification.Channel.Close( reply_code=reply_code, reply_text=reply_text) ) finally: if self._inbound: del self._inbound[:] self.set_state(self.CLOSED) LOGGER.debug('Channel #%d Closed', self.channel_id)
def delete(self, queue='', if_unused=False, if_empty=False): """Delete a Queue. :param str queue: Queue name :param bool if_unused: Delete only if unused :param bool if_empty: Delete only if empty :raises AMQPInvalidArgument: Invalid Parameters :raises AMQPChannelError: Raises if the channel encountered an error. :raises AMQPConnectionError: Raises if the connection encountered an error. :rtype: dict """ if not compatibility.is_string(queue): raise AMQPInvalidArgument('queue should be a string') elif not isinstance(if_unused, bool): raise AMQPInvalidArgument('if_unused should be a boolean') elif not isinstance(if_empty, bool): raise AMQPInvalidArgument('if_empty should be a boolean') delete_frame = pamqp_queue.Delete(queue=queue, if_unused=if_unused, if_empty=if_empty) return self._channel.rpc_request(delete_frame)
def close(self, reply_code=200, reply_text=''): """Close Channel. :param int reply_code: Close reply code (e.g. 200) :param str reply_text: Close reply text :raises AMQPInvalidArgument: Invalid Parameters :raises AMQPChannelError: Raises if the channel encountered an error. :raises AMQPConnectionError: Raises if the connection encountered an error. :return: """ if not compatibility.is_integer(reply_code): raise AMQPInvalidArgument('reply_code should be an integer') elif not compatibility.is_string(reply_text): raise AMQPInvalidArgument('reply_text should be a string') try: if self._connection.is_closed or self.is_closed: self.stop_consuming() LOGGER.debug('Channel #%d forcefully Closed', self.channel_id) return self.set_state(self.CLOSING) LOGGER.debug('Channel #%d Closing', self.channel_id) self.stop_consuming() self.rpc_request( specification.Channel.Close(reply_code=reply_code, reply_text=reply_text)) finally: if self._inbound: del self._inbound[:] self.set_state(self.CLOSED) LOGGER.debug('Channel #%d Closed', self.channel_id)
def close(self, reply_code=0, reply_text=''): """Close Channel. :param int reply_code: :param str reply_text: :return: """ LOGGER.debug('Channel #%d Closing.', self.channel_id) if not compatibility.is_integer(reply_code): raise AMQPInvalidArgument('reply_code should be an integer') elif not compatibility.is_string(reply_text): raise AMQPInvalidArgument('reply_text should be a string') if not self._connection.is_open or not self.is_open: self.remove_consumer_tag() self.set_state(self.CLOSED) return self.set_state(self.CLOSING) self.stop_consuming() self.rpc_request(pamqp_spec.Channel.Close( reply_code=reply_code, reply_text=reply_text)) del self._inbound[:] self.set_state(self.CLOSED) LOGGER.debug('Channel #%d Closed.', self.channel_id)
def get(self, queue='', no_ack=False, to_dict=False, auto_decode=False): """Fetch a single message. :param str queue: Queue name :param bool no_ack: No acknowledgement needed :param bool to_dict: Should incoming messages be converted to a dictionary before delivery. :param bool auto_decode: Auto-decode strings when possible. :raises AMQPInvalidArgument: Invalid Parameters :raises AMQPChannelError: Raises if the channel encountered an error. :raises AMQPConnectionError: Raises if the connection encountered an error. :returns: Returns a single message, as long as there is a message in the queue. If no message is available, returns None. :rtype: dict|Message|None """ if not compatibility.is_string(queue): raise AMQPInvalidArgument('queue should be a string') elif not isinstance(no_ack, bool): raise AMQPInvalidArgument('no_ack should be a boolean') elif self._channel.consumer_tags: raise AMQPChannelError("Cannot call 'get' when channel is " "set to consume") get_frame = pamqp_spec.Basic.Get(queue=queue, no_ack=no_ack) with self._channel.lock and self._channel.rpc.lock: message = self._get_message(get_frame, auto_decode=auto_decode) if message and to_dict: return message.to_dict() return message
def consume(self, callback=None, queue='', consumer_tag='', exclusive=False, no_ack=False, no_local=False, arguments=None): """Start a queue consumer. :param typing.Callable callback: Message callback :param str queue: Queue name :param str consumer_tag: Consumer tag :param bool no_local: Do not deliver own messages :param bool no_ack: No acknowledgement needed :param bool exclusive: Request exclusive access :param dict arguments: Consume key/value arguments :raises AMQPInvalidArgument: Invalid Parameters :raises AMQPChannelError: Raises if the channel encountered an error. :raises AMQPConnectionError: Raises if the connection encountered an error. :returns: Consumer tag :rtype: str """ if not compatibility.is_string(queue): raise AMQPInvalidArgument('queue should be a string') elif not compatibility.is_string(consumer_tag): raise AMQPInvalidArgument('consumer_tag should be a string') elif not isinstance(exclusive, bool): raise AMQPInvalidArgument('exclusive should be a boolean') elif not isinstance(no_ack, bool): raise AMQPInvalidArgument('no_ack should be a boolean') elif not isinstance(no_local, bool): raise AMQPInvalidArgument('no_local should be a boolean') elif arguments is not None and not isinstance(arguments, dict): raise AMQPInvalidArgument('arguments should be a dict or None') consume_rpc_result = self._consume_rpc_request(arguments, consumer_tag, exclusive, no_ack, no_local, queue) tag = self._consume_add_and_get_tag(consume_rpc_result) self._channel._consumer_callbacks[tag] = callback return tag
def declare(self, exchange='', exchange_type='direct', passive=False, durable=False, auto_delete=False, arguments=None): """Declare an Exchange. :param str exchange: Exchange name :param str exchange_type: Exchange type :param bool passive: Do not create :param bool durable: Durable exchange :param bool auto_delete: Automatically delete when not in use :param dict arguments: Exchange key/value arguments :raises AMQPInvalidArgument: Invalid Parameters :raises AMQPChannelError: Raises if the channel encountered an error. :raises AMQPConnectionError: Raises if the connection encountered an error. :rtype: dict """ if not compatibility.is_string(exchange): raise AMQPInvalidArgument('exchange should be a string') elif not compatibility.is_string(exchange_type): raise AMQPInvalidArgument('exchange_type should be a string') elif not isinstance(passive, bool): raise AMQPInvalidArgument('passive should be a boolean') elif not isinstance(durable, bool): raise AMQPInvalidArgument('durable should be a boolean') elif not isinstance(auto_delete, bool): raise AMQPInvalidArgument('auto_delete should be a boolean') elif arguments is not None and not isinstance(arguments, dict): raise AMQPInvalidArgument('arguments should be a dict or None') declare_frame = pamqp_exchange.Declare(exchange=exchange, exchange_type=exchange_type, passive=passive, durable=durable, auto_delete=auto_delete, arguments=arguments) return self._channel.rpc_request(declare_frame)
def _validate_parameters(self): """Validate Connection Parameters. :return: """ if not compatibility.is_string(self.parameters['hostname']): raise AMQPInvalidArgument('hostname should be a string') elif not compatibility.is_integer(self.parameters['port']): raise AMQPInvalidArgument('port should be an integer') elif not compatibility.is_string(self.parameters['username']): raise AMQPInvalidArgument('username should be a string') elif not compatibility.is_string(self.parameters['password']): raise AMQPInvalidArgument('password should be a string') elif not compatibility.is_string(self.parameters['virtual_host']): raise AMQPInvalidArgument('virtual_host should be a string') elif not isinstance(self.parameters['timeout'], (int, float)): raise AMQPInvalidArgument('timeout should be an integer or float') elif not compatibility.is_integer(self.parameters['heartbeat']): raise AMQPInvalidArgument('heartbeat should be an integer')
def publish(self, body, routing_key, exchange='', properties=None, mandatory=False, immediate=False): """Publish Message. :param str|unicode body: :param str routing_key: :param str exchange: :param dict properties: :param bool mandatory: :param bool immediate: :rtype: bool|None """ if not compatibility.is_string(body): raise AMQPInvalidArgument('body should be a string') elif not compatibility.is_string(routing_key): raise AMQPInvalidArgument('routing_key should be a string') elif not compatibility.is_string(exchange): raise AMQPInvalidArgument('exchange should be a string') elif properties and not isinstance(properties, dict): raise AMQPInvalidArgument('properties should be a dict or None') elif not isinstance(mandatory, bool): raise AMQPInvalidArgument('mandatory should be a boolean') elif not isinstance(immediate, bool): raise AMQPInvalidArgument('immediate should be a boolean') properties = properties or {} body = self._handle_utf8_payload(body, properties) properties = pamqp_spec.Basic.Properties(**properties) method_frame = pamqp_spec.Basic.Publish(exchange=exchange, routing_key=routing_key, mandatory=mandatory, immediate=immediate) header_frame = pamqp_header.ContentHeader(body_size=len(body), properties=properties) send_buffer = [method_frame, header_frame] for body_frame in self._create_content_body(body): send_buffer.append(body_frame) if self._channel.confirming_deliveries: with self._channel.rpc.lock: return self._publish_confirm(send_buffer) else: self._channel.write_frames(send_buffer)
def add_consumer_tag(self, tag): """Add a Consumer tag. :param str tag: Consumer tag. :return: """ if not is_string(tag): raise AMQPChannelError('consumer tag needs to be a string') if tag not in self._consumer_tags: self._consumer_tags.append(tag)
def purge(self, queue=''): """Purge queue. :param str queue: :rtype: dict """ if not compatibility.is_string(queue): raise AMQPInvalidArgument('queue should be a string') purge_frame = pamqp_queue.Purge(queue=queue) return self._channel.rpc_request(purge_frame)
def cancel(self, consumer_tag=''): """Cancel a queue consumer. :param str consumer_tag: Consumer tag :rtype: dict """ if not compatibility.is_string(consumer_tag): raise AMQPInvalidArgument('consumer_tag should be a string') cancel_frame = pamqp_spec.Basic.Cancel(consumer_tag=consumer_tag) result = self._channel.rpc_request(cancel_frame) self._channel.remove_consumer_tag(consumer_tag) return result
def delete(self, exchange='', if_unused=False): """Delete exchange. :param str exchange: :param bool if_unused: :rtype: dict """ if not compatibility.is_string(exchange): raise AMQPInvalidArgument('exchange should be a string') delete_frame = pamqp_exchange.Delete(exchange=exchange, if_unused=if_unused) return self._channel.rpc_request(delete_frame)
def consume(self, callback=None, queue='', consumer_tag='', exclusive=False, no_ack=False, no_local=False, arguments=None): """Start a queue consumer. :param function callback: Message callback :param str queue: Queue name :param str consumer_tag: Consumer tag :param bool no_local: Do not deliver own messages :param bool no_ack: No acknowledgement needed :param bool exclusive: Request exclusive access :param dict arguments: Consume key/value arguments :raises AMQPInvalidArgument: Invalid Parameters :raises AMQPChannelError: Raises if the channel encountered an error. :raises AMQPConnectionError: Raises if the connection encountered an error. :returns: Consumer tag :rtype: str """ if not compatibility.is_string(queue): raise AMQPInvalidArgument('queue should be a string') elif not compatibility.is_string(consumer_tag): raise AMQPInvalidArgument('consumer_tag should be a string') elif not isinstance(exclusive, bool): raise AMQPInvalidArgument('exclusive should be a boolean') elif not isinstance(no_ack, bool): raise AMQPInvalidArgument('no_ack should be a boolean') elif not isinstance(no_local, bool): raise AMQPInvalidArgument('no_local should be a boolean') elif arguments is not None and not isinstance(arguments, dict): raise AMQPInvalidArgument('arguments should be a dict or None') consume_rpc_result = self._consume_rpc_request(arguments, consumer_tag, exclusive, no_ack, no_local, queue) tag = self._consume_add_and_get_tag(consume_rpc_result) self._channel._consumer_callbacks[tag] = callback return tag
def unbind(self, queue='', exchange='', routing_key='', arguments=None): """Unbind queue. :param str queue: :param str exchange: :param str routing_key: :param dict arguments: :rtype: dict """ if not compatibility.is_string(queue): raise AMQPInvalidArgument('queue should be a string') elif not compatibility.is_string(exchange): raise AMQPInvalidArgument('exchange should be a string') elif not compatibility.is_string(routing_key): raise AMQPInvalidArgument('routing_key should be a string') elif arguments is not None and not isinstance(arguments, dict): raise AMQPInvalidArgument('arguments should be a dict or None') unbind_frame = pamqp_queue.Unbind(queue=queue, exchange=exchange, routing_key=routing_key, arguments=arguments) return self._channel.rpc_request(unbind_frame)
def unbind(self, destination='', source='', routing_key='', arguments=None): """Unbind exchange. :param str destination: :param str source: :param str routing_key: :param dict arguments: :rtype: dict """ if not compatibility.is_string(destination): raise AMQPInvalidArgument('destination should be a string') elif not compatibility.is_string(source): raise AMQPInvalidArgument('source should be a string') elif not compatibility.is_string(routing_key): raise AMQPInvalidArgument('routing_key should be a string') elif arguments is not None and not isinstance(arguments, dict): raise AMQPInvalidArgument('arguments should be a dict or None') unbind_frame = pamqp_exchange.Unbind(destination=destination, source=source, routing_key=routing_key, arguments=arguments) return self._channel.rpc_request(unbind_frame)
def get(self, queue='', no_ack=False): """Fetch a single message. :param str queue: :param bool no_ack: No acknowledgement needed :rtype: dict or None """ if not compatibility.is_string(queue): raise AMQPInvalidArgument('queue should be a string') elif not isinstance(no_ack, bool): raise AMQPInvalidArgument('no_ack should be a boolean') elif self._channel.consumer_tags: raise AMQPChannelError('Cannot call \'get\' when set to consume.') get_frame = pamqp_spec.Basic.Get(queue=queue, no_ack=no_ack) with self._channel.lock and self._channel.rpc.lock: return self._get_message(get_frame)
def cancel(self, consumer_tag=''): """Cancel a queue consumer. :param str consumer_tag: Consumer tag :raises AMQPInvalidArgument: Invalid Parameters :raises AMQPChannelError: Raises if the channel encountered an error. :raises AMQPConnectionError: Raises if the connection encountered an error. :rtype: dict """ if not compatibility.is_string(consumer_tag): raise AMQPInvalidArgument('consumer_tag should be a string') cancel_frame = pamqp_spec.Basic.Cancel(consumer_tag=consumer_tag) result = self._channel.rpc_request(cancel_frame) self._channel.remove_consumer_tag(consumer_tag) return result
def purge(self, queue=''): """Purge a Queue. :param str queue: :raises AMQPInvalidArgument: Invalid Parameters :raises AMQPChannelError: Raises if the channel encountered an error. :raises AMQPConnectionError: Raises if the connection encountered an error. :rtype: dict """ if not compatibility.is_string(queue): raise AMQPInvalidArgument('queue should be a string') purge_frame = pamqp_queue.Purge(queue=queue) return self._channel.rpc_request(purge_frame)
def delete(self, queue='', if_unused=False, if_empty=False): """Delete queue. :param str queue: :param bool if_unused: Delete only if unused :param bool if_empty: Delete only if empty :rtype: dict """ if not compatibility.is_string(queue): raise AMQPInvalidArgument('queue should be a string') elif not isinstance(if_unused, bool): raise AMQPInvalidArgument('if_unused should be a boolean') elif not isinstance(if_empty, bool): raise AMQPInvalidArgument('if_empty should be a boolean') delete_frame = pamqp_queue.Delete(queue=queue, if_unused=if_unused, if_empty=if_empty) return self._channel.rpc_request(delete_frame)
def cancel(self, consumer_tag=''): """Cancel a queue consumer. :param str consumer_tag: Consumer tag :raises AMQPInvalidArgument: Invalid Parameters :raises AMQPChannelError: Raises if the channel encountered an error. :raises AMQPConnectionError: Raises if the connection encountered an error. :rtype: dict """ if not compatibility.is_string(consumer_tag): raise AMQPInvalidArgument('consumer_tag should be a string') cancel_frame = specification.Basic.Cancel(consumer_tag=consumer_tag) result = self._channel.rpc_request(cancel_frame) self._channel.remove_consumer_tag(consumer_tag) return result
def purge(self, queue): """Purge a Queue. :param str queue: Queue name :raises AMQPInvalidArgument: Invalid Parameters :raises AMQPChannelError: Raises if the channel encountered an error. :raises AMQPConnectionError: Raises if the connection encountered an error. :rtype: dict """ if not compatibility.is_string(queue): raise AMQPInvalidArgument('queue should be a string') purge_frame = pamqp_queue.Purge(queue=queue) return self._channel.rpc_request(purge_frame)
def delete(self, exchange='', if_unused=False): """Delete an Exchange. :param str exchange: Exchange name :param bool if_unused: Delete only if unused :raises AMQPInvalidArgument: Invalid Parameters :raises AMQPChannelError: Raises if the channel encountered an error. :raises AMQPConnectionError: Raises if the connection encountered an error. :rtype: dict """ if not compatibility.is_string(exchange): raise AMQPInvalidArgument('exchange should be a string') delete_frame = pamqp_exchange.Delete(exchange=exchange, if_unused=if_unused) return self._channel.rpc_request(delete_frame)
def declare(self, queue='', passive=False, durable=False, exclusive=False, auto_delete=False, arguments=None): """Declare a Queue. :param str queue: :param bool passive: :param bool durable: :param bool exclusive: :param bool auto_delete: :param dict arguments: :raises AMQPInvalidArgument: Invalid Parameters :raises AMQPChannelError: Raises if the channel encountered an error. :raises AMQPConnectionError: Raises if the connection encountered an error. :rtype: dict """ if not compatibility.is_string(queue): raise AMQPInvalidArgument('queue should be a string') elif not isinstance(passive, bool): raise AMQPInvalidArgument('passive should be a boolean') elif not isinstance(durable, bool): raise AMQPInvalidArgument('durable should be a boolean') elif not isinstance(exclusive, bool): raise AMQPInvalidArgument('exclusive should be a boolean') elif not isinstance(auto_delete, bool): raise AMQPInvalidArgument('auto_delete should be a boolean') elif arguments is not None and not isinstance(arguments, dict): raise AMQPInvalidArgument('arguments should be a dict or None') declare_frame = pamqp_queue.Declare(queue=queue, passive=passive, durable=durable, exclusive=exclusive, auto_delete=auto_delete, arguments=arguments) return self._channel.rpc_request(declare_frame)
def delete(self, exchange='', if_unused=False): """Delete an Exchange. :param str exchange: :param bool if_unused: :raises AMQPInvalidArgument: Invalid Parameters :raises AMQPChannelError: Raises if the channel encountered an error. :raises AMQPConnectionError: Raises if the connection encountered an error. :rtype: dict """ if not compatibility.is_string(exchange): raise AMQPInvalidArgument('exchange should be a string') delete_frame = pamqp_exchange.Delete(exchange=exchange, if_unused=if_unused) return self._channel.rpc_request(delete_frame)
def get(self, queue='', no_ack=False, to_dict=True): """Fetch a single message. :param str queue: :param bool no_ack: No acknowledgement needed :param bool to_dict: Should incoming messages be converted to a dictionary before delivery. :rtype: dict|None """ if not compatibility.is_string(queue): raise AMQPInvalidArgument('queue should be a string') elif not isinstance(no_ack, bool): raise AMQPInvalidArgument('no_ack should be a boolean') elif self._channel.consumer_tags: raise AMQPChannelError('Cannot call \'get\' when set to consume.') get_frame = pamqp_spec.Basic.Get(queue=queue, no_ack=no_ack) with self._channel.lock and self._channel.rpc.lock: message = self._get_message(get_frame) if message and to_dict: return message.to_dict() return message
def delete(self, queue='', if_unused=False, if_empty=False): """Delete a Queue. :param str queue: :param bool if_unused: Delete only if unused :param bool if_empty: Delete only if empty :raises AMQPInvalidArgument: Invalid Parameters :raises AMQPChannelError: Raises if the channel encountered an error. :raises AMQPConnectionError: Raises if the connection encountered an error. :rtype: dict """ if not compatibility.is_string(queue): raise AMQPInvalidArgument('queue should be a string') elif not isinstance(if_unused, bool): raise AMQPInvalidArgument('if_unused should be a boolean') elif not isinstance(if_empty, bool): raise AMQPInvalidArgument('if_empty should be a boolean') delete_frame = pamqp_queue.Delete(queue=queue, if_unused=if_unused, if_empty=if_empty) return self._channel.rpc_request(delete_frame)
def test_compatibility_is_not_string(self): x = 0 self.assertFalse(compatibility.is_string(x))
def test_unicode_string(self): x = unicode("") self.assertTrue(compatibility.is_string(x))
def test_is_not_string(self): x = 0 self.assertFalse(compatibility.is_string(x))
def test_compatibility_normal_string(self): x = '' self.assertTrue(compatibility.is_string(x))
def test_compatibility_byte_string(self): x = b'' self.assertTrue(compatibility.is_string(x))
def test_compatibility_unicode_string(self): x = u'Mor, lilla mor, vem är väl som du' self.assertTrue(compatibility.is_string(x))