Пример #1
0
    def rpc(self, method, callback=None, acceptable_replies=None):
        """
        Shortcut wrapper to the Connection's rpc command using its callback
        stack, passing in our channel number
        """
        # Make sure the channel is open
        self._ensure()

        # If we're blocking, add subsequent commands to our stack
        if self.blocking:
            self._blocked.append([method, callback, acceptable_replies])
            return

        # Validate we got None or a list of acceptable_replies
        if acceptable_replies and not isinstance(acceptable_replies, list):
            raise TypeError("acceptable_replies should be list or None")

        # Validate the callback is callable
        if callback and not is_callable(callback):
            raise TypeError("callback should be None, a function or method.")

        # If this is a synchronous method, block connections until we're done
        if method.synchronous:
            self.blocking = method.NAME

        if acceptable_replies:
            for reply in acceptable_replies:
                self.callbacks.add(self.channel_number, reply,
                                   self._on_synchronous_complete)
                if callback:
                    self.callbacks.add(self.channel_number, reply, callback)

        self.send_method(method)
Пример #2
0
    def rpc(self, method, callback=None, acceptable_replies=None):
        """
        Shortcut wrapper to the Connection's rpc command using its callback
        stack, passing in our channel number
        """
        # Make sure the channel is open
        self._ensure()

        # If we're blocking, add subsequent commands to our stack
        if self.blocking:
            self._blocked.append([method, callback, acceptable_replies])
            return

        # Validate we got None or a list of acceptable_replies
        if acceptable_replies and not isinstance(acceptable_replies, list):
            raise TypeError("acceptable_replies should be list or None")

        # Validate the callback is callable
        if callback and not is_callable(callback):
            raise TypeError("callback should be None, a function or method.")

        # If this is a synchronous method, block connections until we're done
        if method.synchronous:
            self.blocking = method.NAME

        if acceptable_replies:
            for reply in acceptable_replies:
                self.callbacks.add(self.channel_number, reply,
                                   self._on_synchronous_complete)
                if callback:
                    self.callbacks.add(self.channel_number, reply, callback)

        self.send_method(method)
Пример #3
0
    def _rpc(self, channel_number, method_frame,
             callback_method=None, acceptable_replies=None):
        """Make an RPC call for the given callback, channel number and method.
        acceptable_replies lists out what responses we'll process from the
        server with the specified callback.

        :param int channel_number: The channel number for the RPC call
        :param pika.object.Method method_frame: The method frame to call
        :param method callback_method: The callback for the RPC response
        :param list acceptable_replies: The replies this RPC call expects

        """
        # Validate that acceptable_replies is a list or None
        if acceptable_replies and not isinstance(acceptable_replies, list):
            raise TypeError("acceptable_replies should be list or None")

        # Validate the callback is callable
        if callback_method:
            if not utils.is_callable(callback_method):
                raise TypeError("callback should be None, function or method.")

            for reply in acceptable_replies:
                self.callbacks.add(channel_number, reply, callback_method)

        # Send the rpc call to RabbitMQ
        self._send_method(channel_number, method_frame)
Пример #4
0
    def _rpc(self,
             channel_number,
             method,
             callback=None,
             acceptable_replies=None):
        """
        Make an RPC call for the given callback, channel number and method.
        acceptable_replies lists out what responses we'll process from the
        server with the specified callback.
        """
        # Validate that acceptable_replies is a list or None
        if acceptable_replies and not isinstance(acceptable_replies, list):
            raise TypeError("acceptable_replies should be list or None")

        # Validate the callback is callable
        if callback and not is_callable(callback):
            raise TypeError("callback should be None, a function or method.")

        # If we were passed a callback, add it to our stack
        if callback:
            for reply in acceptable_replies:
                self.callbacks.add(channel_number, reply, callback)

        # Send the rpc call to RabbitMQ
        self._send_method(channel_number, method)
Пример #5
0
    def _rpc(self,
             channel_number,
             method_frame,
             callback_method=None,
             acceptable_replies=None):
        """Make an RPC call for the given callback, channel number and method.
        acceptable_replies lists out what responses we'll process from the
        server with the specified callback.

        :param int channel_number: The channel number for the RPC call
        :param pika.object.Method method_frame: The method frame to call
        :param method callback_method: The callback for the RPC response
        :param list acceptable_replies: The replies this RPC call expects

        """
        # Validate that acceptable_replies is a list or None
        if acceptable_replies and not isinstance(acceptable_replies, list):
            raise TypeError('acceptable_replies should be list or None')

        # Validate the callback is callable
        if callback_method:
            if not utils.is_callable(callback_method):
                raise TypeError('callback should be None, function or method.')

            for reply in acceptable_replies:
                self.callbacks.add(channel_number, reply, callback_method)

        # Send the rpc call to RabbitMQ
        self._send_method(channel_number, method_frame)
Пример #6
0
    def _validate_callback(self, callback):
        """Validate the value passed in is a method or function.

        :param method callback callback: The method to validate
        :raises: TypeError

        """
        if callback is not None and not utils.is_callable(callback):
            raise TypeError("Callback should be a function or method, is %s", type(callback))
Пример #7
0
    def _validate_callback(self, callback):
        """Validate the value passed in is a method or function.

        :param method callback callback: The method to validate
        :raises: TypeError

        """
        if (callback is not None and not utils.is_callable(callback)):
            raise TypeError("Callback should be a function or method, is %s",
                            type(callback))
Пример #8
0
    def bind_callback(self, message):
        if not is_callable(self._on_message_callback):
            LOGGER.info('Wrong callback is binded')
            raise CallBackError('The callback is not callable')

        try:
            self._on_message_callback(message)
            LOGGER.info('Received message # %s #', message)
        except TypeError as error:
            LOGGER.info('Error on the callback definition. Message is not '
                        'acknowledge. And it will be keep on the RabbitMQ')
            error_message = 'You should implement your callback ' \
                            'like my_callback(content)'
            raise CallBackError(error_message, original_exception=str(error))
Пример #9
0
    def _rpc(self, method_frame, callback=None, acceptable_replies=None):
        """Shortcut wrapper to the Connection's rpc command using its callback
        stack, passing in our channel number.

        :param pika.amqp_object.Method method_frame: The method frame to call
        :param method callback: The callback for the RPC response
        :param list acceptable_replies: The replies this RPC call expects

        """
        # Make sure the channel is open
        if self.is_closed:
            raise exceptions.ChannelClosed

        # If the channel is blocking, add subsequent commands to our stack
        if self._blocking:
            return self._blocked.append(
                [method_frame, callback, acceptable_replies])

        # Validate we got None or a list of acceptable_replies
        if acceptable_replies and not isinstance(acceptable_replies, list):
            raise TypeError("acceptable_replies should be list or None")

        # Validate the callback is callable
        if callback and not is_callable(callback):
            raise TypeError("callback should be None, a function or method.")

        # Block until a response frame is received for synchronous frames
        if method_frame.synchronous:
            self.blocking = method_frame.NAME

        # If acceptable replies are set, add callbacks
        if acceptable_replies:
            for reply in acceptable_replies or list():
                if isinstance(reply, tuple):
                    reply, arguments = reply
                else:
                    arguments = None
                LOGGER.debug('Adding in on_synchronous_complete callback')
                self.callbacks.add(self.channel_number,
                                   reply,
                                   self._on_synchronous_complete,
                                   arguments=arguments)
                if callback:
                    LOGGER.debug('Adding passed in callback')
                    self.callbacks.add(self.channel_number,
                                       reply,
                                       callback,
                                       arguments=arguments)

        self._send_method(method_frame)
Пример #10
0
    def _rpc(self, method_frame, callback=None, acceptable_replies=None):
        """Shortcut wrapper to the Connection's rpc command using its callback
        stack, passing in our channel number.

        :param pika.amqp_object.Method method_frame: The method frame to call
        :param method callback: The callback for the RPC response
        :param list acceptable_replies: The replies this RPC call expects

        """
        # Make sure the channel is open
        if self.is_closed:
            raise exceptions.ChannelClosed

        # If the channel is blocking, add subsequent commands to our stack
        if self._blocking:
            return self._blocked.append([method_frame,
                                         callback,
                                         acceptable_replies])

        # Validate we got None or a list of acceptable_replies
        if acceptable_replies and not isinstance(acceptable_replies, list):
            raise TypeError("acceptable_replies should be list or None")

        # Validate the callback is callable
        if callback and not is_callable(callback):
            raise TypeError("callback should be None, a function or method.")

        # Block until a response frame is received for synchronous frames
        if method_frame.synchronous:
            self.blocking = method_frame.NAME

        # If acceptable replies are set, add callbacks
        if acceptable_replies:
            for reply in acceptable_replies or list():
                if isinstance(reply, tuple):
                    reply, arguments = reply
                else:
                    arguments = None
                LOGGER.debug('Adding in on_synchronous_complete callback')
                self.callbacks.add(self.channel_number, reply,
                                   self._on_synchronous_complete,
                                   arguments=arguments)
                if callback:
                    LOGGER.debug('Adding passed in callback')
                    self.callbacks.add(self.channel_number, reply, callback,
                                       arguments=arguments)

        self._send_method(method_frame)
Пример #11
0
    def _rpc(self, channel_number, method, callback=None, acceptable_replies=None):
        """
        Make an RPC call for the given callback, channel number and method.
        acceptable_replies lists out what responses we'll process from the
        server with the specified callback.
        """
        # Validate that acceptable_replies is a list or None
        if acceptable_replies and not isinstance(acceptable_replies, list):
            raise TypeError("acceptable_replies should be list or None")

        # Validate the callback is callable
        if callback and not is_callable(callback):
            raise TypeError("callback should be None, a function or method.")

        # If we were passed a callback, add it to our stack
        if callback:
            for reply in acceptable_replies:
                self.callbacks.add(channel_number, reply, callback)

        # Send the rpc call to RabbitMQ
        self._send_method(channel_number, method)
Пример #12
0
 def _validate_channel_and_callback(self, callback):
     if not self.is_open:
         raise exceptions.ChannelClosed()
     if callback is not None and not is_callable(callback):
         raise ValueError('callback must be a function or method')
Пример #13
0
 def test_is_callable_false(self):
     self.assertFalse(utils.is_callable(1))
Пример #14
0
 def test_is_callable_true(self):
     self.assertTrue(utils.is_callable(utils.is_callable))
Пример #15
0
 def test_is_callable_false_no_callable(self):
     callable = utils.Callable
     utils.Callable = None
     self.assertFalse(utils.is_callable(1))
     utils.Callable = callable
Пример #16
0
 def _validate_channel_and_callback(self, callback):
     if not self.is_open:
         raise exceptions.ChannelClosed()
     if callback is not None and not is_callable(callback):
         raise ValueError('callback must be a function or method')
Пример #17
0
 def test_is_callable_true_no_callable(self):
     callable = utils.Callable
     utils.Callable = None
     self.assertTrue(utils.is_callable(utils.is_callable))
     utils.Callable = callable
Пример #18
0
 def test_is_callable_false(self):
     self.assertFalse(utils.is_callable(1))
Пример #19
0
 def test_is_callable_true(self):
     self.assertTrue(utils.is_callable(utils.is_callable))
Пример #20
0
 def test_is_callable_true_no_callable(self):
     callable = utils.Callable
     utils.Callable = None
     self.assertTrue(utils.is_callable(utils.is_callable))
     utils.Callable = callable
Пример #21
0
 def test_is_callable_false_no_callable(self):
     callable = utils.Callable
     utils.Callable = None
     self.assertFalse(utils.is_callable(1))
     utils.Callable = callable