Initial work on hooke.command_stack and hooke.playlist integration.
[hooke.git] / hooke / command_stack.py
index cebf0b9b6e89113a613e4baf2d9fa429e08c83f4..fc8dc751add0a9bece435ca37c27673ecbd853d0 100644 (file)
 # License along with Hooke.  If not, see
 # <http://www.gnu.org/licenses/>.
 
-"""The ``command_stack`` module provides tools for managing stacks of
-:class:`~hooke.engine.CommandMessage`\s and applying those stacks to
-:class:`~hooke.curve.Curve`\s.
+"""The ``command_stack`` module provides tools for managing and
+executing stacks of :class:`~hooke.engine.CommandMessage`\s.
 """
 
-from .command import Command, Argument, Failure
-from .engine import CommandMessage
-from .playlist import NoteIndexList
+
+class CommandStack (list):
+    """Store a stack of commands.
+
+    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'}))
+
+    Implement a dummy :meth:`_execute` for testing.  This would
+    usually call :meth:`hooke.command.Command.run` with appropriate
+    arguments.
+    
+    >>> def execute(command_message):
+    ...     cm = command_message
+    ...     print 'EXECUTE', cm.command.name, cm.arguments
+    >>> c._execute = execute
+
+    >>> c.execute()  # doctest: +ELLIPSIS
+    EXECUTE CommandA {'param': 'A'}
+    EXECUTE CommandB {'param': 'B'}
+    EXECUTE CommandA {'param': 'C'}
+    EXECUTE CommandB {'param': 'D'}
+
+    :meth:`filter` allows you to select which commands get executed.
+    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`.
+    
+    >>> def filter(command_message):
+    ...     return isinstance(command_message.command, CommandB)
+    >>> c.filter = filter
+
+    Apply the stack to the current curve
+    >>> c.execute()  # doctest: +ELLIPSIS
+    EXECUTE CommandB {'param': 'B'}
+    EXECUTE CommandB {'param': 'D'}
+    """
+    def execute(self, *args, **kwargs):
+        """Execute a stack of commands.
+
+        See Also
+        --------
+        _execute, filter
+        """
+        for command_message in self:
+            if self.filter(command_message) == True:
+                self._execute(command_message, *args, **kwargs)
+
+    def filter(self, command_message):
+        """Any commands in the stack that are not subclasses of
+        :class:`~hooke.plugin.curve.CurveCommand` are ignored.
+        """
+        return True
+
+    def _execute(self, command_message):
+        raise NotImplementedError()