Exemplo n.º 1
0
    def fire(self, name, *params):
        """Calls all callbacks with given event name.

        Keyword arguments:
          name -- Event name to fire.
          params -- Params to pass to callbacks.

        Raises:
          EventDoesNotExistError -- If fire is called a nonexistent event.

        Returns:
          boolean -- Whether a callback (or multiple) was called successfully.
        """
        self.logger.debug("Attempting to fire event: %s" % name)

        if name not in self.registered_events:
            self.logger.debug("Event does not exist: %s" % name)
            raise EventDoesNotExistError(
                "Can't call an event that does not exist: %s" % name
            )

        callbacks = self.registered_callbacks[name]
        self.logger.info(
            "Calling %d callbacks for event: %s" %
            (len(callbacks), name)
        )

        accepted = False
        for callback_id, callback in callbacks.iteritems():
            try:
                callback(self.cardinal, *params)
                self.logger.debug(
                    "Callback %s accepted event: %s" %
                    (callback_id, name)
                )
                accepted = True
            # If this exception is received, the plugin told us not to set the
            # called flag true, so we can just log it and continue on. This
            # might happen if a plugin realizes the event does not apply to it
            # and wants the original caller to handle it normally.
            except EventRejectedMessage:
                self.logger.debug(
                    "Callback %s rejected event: %s" %
                    (callback_id, name)
                )
            except Exception:
                self.logger.exception(
                    "Exception during callback %s for event: %s" %
                    (callback_id, name)
                )

        return accepted
Exemplo n.º 2
0
    def remove(self, name):
        """Removes a registered event."""
        self.logger.debug("Attempting to unregister event: %s" % name)

        if name not in self.registered_events:
            self.logger.debug("Event does not exist: %s" % name)
            raise EventDoesNotExistError("Can't remove nonexistent event: %s" %
                                         name)

        del self.registered_events[name]
        del self.registered_callbacks[name]

        self.logger.info("Removed event: %s" % name)
Exemplo n.º 3
0
    def fire(self, name, *params):
        """Calls all callbacks with given event name.

        Keyword arguments:
          name -- Event name to fire.
          params -- Params to pass to callbacks.

        Raises:
          EventDoesNotExistError -- If fire is called a nonexistent event.

        Returns:
          boolean -- Whether a callback (or multiple) was called successfully.
        """
        self.logger.debug("Attempting to fire event: %s" % name)

        if name not in self.registered_events:
            self.logger.debug("Event does not exist: %s" % name)
            raise EventDoesNotExistError(
                "Can't call an event that does not exist: %s" % name)

        callbacks = self.registered_callbacks[name]
        self.logger.debug("Calling %d callbacks for event: %s" %
                          (len(callbacks), name))

        cb_deferreds = []
        for callback_id, callback in callbacks.items():
            d = defer.maybeDeferred(callback, self.cardinal, *params)

            # It is necessary to pass callback_id in to this function in order
            # to make sure it doesn't change when the loop iterates
            def success(_result, callback_id=callback_id):
                self.logger.debug("Callback {} accepted event '{}'".format(
                    callback_id, name))

                return True

            d.addCallback(success)

            # It is necessary to pass callback_id in to this function in order
            # to make sure it doesn't change when the loop iterates
            def eventRejectedErrback(failure, callback_id=callback_id):
                # If this exception is received, the plugin told us not to set
                # the called flag true, so we can just log it and continue on.
                # This might happen if a plugin realizes the event does not
                # apply to it and wants the original caller to handle it
                # normally.
                failure.trap(EventRejectedMessage)

                self.logger.debug("Callback {} rejected event '{}'".format(
                    callback_id, name))

                return False

            d.addErrback(eventRejectedErrback)

            # It is necessary to pass callback_id in to this function in order
            # to make sure it doesn't change when the loop iterates
            def errback(failure, callback_id=callback_id):
                self.logger.error(
                    "Unhandled error during callback {} for event '{}': {}".
                    format(callback_id, name, failure))

                return False

            d.addErrback(errback)

            cb_deferreds.append(d)

        dl = defer.DeferredList(cb_deferreds)
        dl.addCallback(self._reduce_callback_accepted_statuses)
        return dl