Change CommandMessage.command from Command instance to command's name.
[hooke.git] / hooke / command_stack.py
index 3888a45e74efca10d1069b26833fecd6eff49fd3..1e6c27c937a790eaeb0b91f68f061e90389c433e 100644 (file)
@@ -27,31 +27,19 @@ class CommandStack (list):
     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'}
@@ -61,14 +49,14 @@ class CommandStack (list):
     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'}
     """
@@ -80,12 +68,13 @@ class CommandStack (list):
         _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