Пример #1
0
    def unregister(self, event, block):
        """Unregister a code block from an event.

        :except NotEventError: Trying to unregister code blocks with something
                               that is not an instance of :class:`Event`.
        :except NotCallableError: Trying to unregister something that is not a
                                  callable.
        :except UnknownCallableError: The callable provided was not known to the
                                      event handler.
        """

        if not isinstance(event, Event):
            raise _errors.NotEventError(
                "Not possible to unregister with non-event")

        if not callable(block):
            raise _errors.NotCallableError(
                "Not possible to unregister a non-callable")

        _LOGGER.debug("Unregistering %s from event %s", block, event)

        try:
            self.__blocks_for[event].remove(block)
            if self.__blocks_for[event]:
                del self.__blocks_for[event]
        except KeyError:
            raise _errors.UnknownCallableError(
                "Not possible to unregister a non-existing block")
Пример #2
0
    def is_registered(self, event, block):
        """Check if a callable is registered with an event.

        Note that the exact callable instance is checked for, so you have to
        pass the instance that you want to check for.

        :param event: Event to check if callable is registered with.
        :param block: Callable to look for.
        :type block: callable

        :return: ``True`` if the callable instance is registered with the event,
                 ``False`` otherwise.
        :rtype: Boolean.
        """

        if not isinstance(event, Event):
            raise _errors.NotEventError(
                "Not possible to check registration for non-event")

        if not callable(block):
            raise _errors.NotCallableError(
                "Not possible to check for non-callable")

        try:
            return block in self.__blocks_for[event]
        except KeyError:
            return False
Пример #3
0
    def register(self, event, blocks):
        """Register code blocks with an event in the event handler.

        This method register the code blocks supplied under the
        event. Each code block is represented as a callable (which
        means either a function or a class supporting the ``__call__``
        method).

        If the event was not previously registered, a new entry will
        be created for it. This involves both registering the name of
        the event (if it has a name) and the event instance.

        :param event: Event to register code blocks for.
        :param blocks: Callable to register for event.
        :type blocks: Callable or sequence of callables

        :except NotEventError: Trying to register code blocks with something
                               that is not an instance of :class:`Event`.
        :except NotCallableError: Trying to register something that is not a
                               callable with an event.
        """

        if not isinstance(event, Event):
            raise _errors.NotEventError(
                "Not possible to register with non-event")

        if callable(blocks):
            blocks = [blocks]

        # Check that all provided blocks are callables.
        try:
            for block in blocks:
                if not callable(block):
                    raise _errors.NotCallableError(
                        "Not possible to register non-callables")
        except TypeError:
            raise _errors.NotCallableError("Expected an iterable")

        _LOGGER.debug("Registering blocks %s for event %s under name %s",
                      blocks, event, event.name)

        # Register the name if not registered
        if event.name is not None and event.name not in self.__instance_for:
            self.__instance_for[event.name] = event

        # Register the callables
        self.__blocks_for.setdefault(event, set()).update(blocks)