Source code for AYABInterface.actions

"""These are the actions that can be executed by the users."""
from .utils import camel_case_to_under_score

_doc_base = """Test whether this is a {}.

:rtype: bool
:return: :obj:`{}`

def _new_test(name, container, result, clsname):
    """Create the "is_*" functions for the Actions."""
    def test(self):
        return result
    test.__name__ = name
    test.__qualname__ = container.__qualname__ + "." + name
    test.__doc__ = _doc_base.format(clsname, result)
    setattr(container, name, test)

Action = None

[docs]class ActionMetaClass(type): """Metaclass for the actions. This class makes sure each :class:`Action` has tests. If a class is named ``MyAction``, each :class:`Action` gets the method ``is_my_action()`` which returns :obj:`False` for all :class:`Actions <Action>` expcept for ``MyAction`` it returns :obj:`True`. """
[docs] def __init__(cls, name, bases, attributes): """Create a new :class:`Action` subclass.""" super().__init__(name, bases, attributes) test_name = "is_" + camel_case_to_under_score(name) if Action is not None: _new_test(test_name, Action, False, cls.__name__) _new_test(test_name, cls, True, cls.__name__)
[docs]class Action(object, metaclass=ActionMetaClass): """A base class for actions."""
[docs] def __init__(self, *arguments): """Create a new :class:`Action`. :param tuple arguments: The arguments passed to the action. These are also used to determine :meth:`equality <__eq__>` and the :meth:`hash <__hash__>`. """ self._arguments = arguments
[docs] def __hash__(self): """The hash of the object. :rtype: int :return: the :func:`hash` of the object """ return hash(self.__class__) ^ hash(self._arguments)
[docs] def __eq__(self, other): """Whether this object is equal to the other. :rtype: bool """ return other == (self.__class__, self._arguments)
[docs] def __repr__(self): """Return this object as string. :rtype: str """ return self.__class__.__name__ + repr(self._arguments)
[docs]class SwitchOnMachine(Action): """The user switches on the machine."""
[docs]class SwitchOffMachine(Action): """The user switches off the machine."""
[docs]class MoveNeedlesIntoPosition(Action): """The user moves needles into position."""
[docs]class PutColorInNutB(Action): """The user puts a color into nut B."""
[docs]class PutColorInNutA(Action): """The user puts a color into nut A."""
[docs]class MoveCarriageToTheRight(Action): """The user moves the carriage to the right."""
[docs]class MoveCarriageToTheLeft(Action): """The user moves the carriage to the left."""
[docs]class MoveCarriageOverLeftHallSensor(Action): """The user moves the carriage over the left hall sensor."""
[docs]class SwitchCarriageToModeNl(Action): """The user switches the mode of the carriage to NL."""
[docs]class SwitchCarriageToModeKc(Action): """The user switches the mode of the carriage to KC."""
__all__ = ["ActionMetaClass", "Action", "SwitchCarriageToModeKc", "SwitchCarriageToModeNl", "MoveCarriageOverLeftHallSensor", "MoveCarriageToTheLeft", "MoveCarriageToTheRight", "PutColorInNutA", "PutColorInNutB", "MoveNeedlesIntoPosition", "SwitchOffMachine", "SwitchOnMachine"]