Ejemplo n.º 1
0
    def _get_handler(self, target, interaction):
        """ Return a callable for handling an interaction for a given target.

        This is an implementation for an abstract method.

        Parameters
        ----------
        target : any
            The UI target being operated on.
        interaction : any
            Any interaction object.

        Returns
        -------
        handler : callable(UIWrapper, interaction) -> any
            The function to handle the given interaction on a target.

        Raises
        ------
        InteractionNotSupported
            If the given target and interaction types are not supported
            by this registry.
        """
        if interaction.__class__ not in self._get_interactions(target):
            raise InteractionNotSupported(
                target_class=target.__class__,
                interaction_class=interaction.__class__,
                supported=list(self._get_interactions(target)),
            )
        return self.interaction_to_handler[interaction.__class__]
Ejemplo n.º 2
0
    def _get_interaction_doc(self, target, interaction_class):
        """ Return the documentation for the given target and interaction type.

        This is an implementation for an abstract method.

        Parameters
        ----------
        target : any
            The UI target for which the interaction will be applied.
        interaction_class : subclass of type
            Any class.

        Returns
        -------
        doc : str

        Raises
        ------
        InteractionNotSupported
            If the given target and interaction types are not supported
            by this registry.
        """
        if interaction_class not in self._get_interactions(target):
            raise InteractionNotSupported(
                target_class=target.__class__,
                interaction_class=interaction_class,
                supported=list(self._get_interactions(target)),
            )
        return inspect.getdoc(interaction_class)
Ejemplo n.º 3
0
 def _get_handler(self, target, interaction):
     if interaction.__class__ not in self.supported_interaction_classes:
         raise InteractionNotSupported(
             target_class=target.__class__,
             interaction_class=interaction.__class__,
             supported=list(self.supported_interaction_classes),
         )
     return self.handler
Ejemplo n.º 4
0
 def __init__(self):
     self._interaction_registry = _TargetToKeyRegistry(
         exception_maker=(lambda target_class, key, available_keys:
                          (InteractionNotSupported(
                              target_class=target_class,
                              interaction_class=key,
                              supported=available_keys,
                          ))), )
     self._location_registry = _TargetToKeyRegistry(exception_maker=(
         lambda target_class, key, available_keys: LocationNotSupported(
             target_class=target_class,
             locator_class=key,
             supported=available_keys,
         )), )
Ejemplo n.º 5
0
    def _perform_or_inspect(self, interaction):
        """Perform a user interaction or a user inspection.

        Parameters
        ----------
        interaction : instance of interaction type
            An object defining the interaction.

        Returns
        -------
        value : any

        Raises
        ------
        InteractionNotSupported
            If the given interaction does not have a corresponding
            implementation for the wrapped UI target.
        """
        supported = []
        for registry in self._registries:
            try:
                handler = registry._get_handler(
                    target=self._target,
                    interaction=interaction,
                )
            except InteractionNotSupported as e:
                supported.extend(e.supported)
                continue
            else:
                context = (
                    _event_processed
                    if self._auto_process_events
                    else _nullcontext
                )
                with context():
                    return handler(self, interaction)

        raise InteractionNotSupported(
            target_class=self._target.__class__,
            interaction_class=interaction.__class__,
            supported=supported,
        )
Ejemplo n.º 6
0
 def get_handler(self, target_class, interaction_class):
     raise InteractionNotSupported(
         target_class=None,
         interaction_class=None,
         supported=[],
     )