Examples
--------
>>> from .engine import CommandMessage
-
- Define two dummy commands for testing.
-
- >>> class CommandA (object):
- ... name = 'CommandA'
- >>> ca = CommandA()
- >>> class CommandB (CommandA):
- ... name = 'CommandB'
- >>> cb = CommandB()
-
- Show off `CommandStack`\s functionality.
-
- >>> c = CommandStack([CommandMessage(ca, {'param':'A'})])
- >>> c.append(CommandMessage(cb, {'param':'B'}))
- >>> c.append(CommandMessage(ca, {'param':'C'}))
- >>> c.append(CommandMessage(cb, {'param':'D'}))
+ >>> c = CommandStack([CommandMessage('CommandA', {'param':'A'})])
+ >>> c.append(CommandMessage('CommandB', {'param':'B'}))
+ >>> c.append(CommandMessage('CommandA', {'param':'C'}))
+ >>> c.append(CommandMessage('CommandB', {'param':'D'}))
Implement a dummy :meth:`_execute` for testing.
>>> def execute(hooke, command_message):
... cm = command_message
- ... print 'EXECUTE', cm.command.name, cm.arguments
+ ... print 'EXECUTE', cm.command, cm.arguments
>>> c._execute = execute
- >>> c.execute() # doctest: +ELLIPSIS
+ >>> c.execute(hooke=None) # doctest: +ELLIPSIS
EXECUTE CommandA {'param': 'A'}
EXECUTE CommandB {'param': 'B'}
EXECUTE CommandA {'param': 'C'}
If, for example, you are applying a set of commands to the current
:class:`~hooke.curve.Curve`, you may only want to execute
instances of :class:`~hooke.plugin.curve.CurveCommand`. Here we
- only execute instances of `CommandB`.
+ only execute commands named `CommandB`.
- >>> def filter(command_message):
- ... return isinstance(command_message.command, CommandB)
+ >>> def filter(hooke, command_message):
+ ... return command_message.command == 'CommandB'
>>> c.filter = filter
Apply the stack to the current curve
- >>> c.execute() # doctest: +ELLIPSIS
+ >>> c.execute(hooke=None) # doctest: +ELLIPSIS
EXECUTE CommandB {'param': 'B'}
EXECUTE CommandB {'param': 'D'}
"""
_execute, filter
"""
for command_message in self:
- if self.filter(command_message) == True:
+ if self.filter(hooke, command_message) == True:
self._execute(hooke, command_message)
- def filter(self, command_message):
- """Any commands in the stack that are not subclasses of
- :class:`~hooke.plugin.curve.CurveCommand` are ignored.
+ def filter(self, hooke, command_message):
+ """Return `True` to execute `command_message`, `False` otherwise.
+
+ The default implementation always returns `True`.
"""
return True