Change CommandMessage.command from Command instance to command's name.
authorW. Trevor King <wking@drexel.edu>
Thu, 12 Aug 2010 15:33:41 +0000 (11:33 -0400)
committerW. Trevor King <wking@drexel.edu>
Thu, 12 Aug 2010 15:33:41 +0000 (11:33 -0400)
No sense in building up large numbers of Command copies if you're
passing CommandMessages through a multiprocess.Queue and storing them
in CommandStacks.

hooke/command_stack.py
hooke/engine.py
hooke/hooke.py
hooke/ui/__init__.py
hooke/ui/commandline.py
hooke/ui/gui/__init__.py

index 3888a45e74efca10d1069b26833fecd6eff49fd3..1e6c27c937a790eaeb0b91f68f061e90389c433e 100644 (file)
@@ -27,31 +27,19 @@ class CommandStack (list):
     Examples
     --------
     >>> from .engine import CommandMessage
     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
 
     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 = execute
 
-    >>> c.execute()  # doctest: +ELLIPSIS
+    >>> c.execute(hooke=None)  # doctest: +ELLIPSIS
     EXECUTE CommandA {'param': 'A'}
     EXECUTE CommandB {'param': 'B'}
     EXECUTE CommandA {'param': 'C'}
     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
     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.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 CommandB {'param': 'B'}
     EXECUTE CommandB {'param': 'D'}
     """
@@ -80,12 +68,13 @@ class CommandStack (list):
         _execute, filter
         """
         for command_message in self:
         _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)
 
                 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
 
         """
         return True
 
index 5bc2f6efb5728d8f83c047374714e96327b95273..e5fd184c3fed2181b923f2dd67c6dc7bea262861 100644 (file)
@@ -35,10 +35,10 @@ class CloseEngine (QueueMessage):
 
 
 class CommandMessage (QueueMessage):
 
 
 class CommandMessage (QueueMessage):
-    """A message storing a command to run, `command` should be a
-    :class:`hooke.command.Command` instance, and `arguments` should be
-    a :class:`dict` with `argname` keys and `value` values to be
-    passed to the command.
+    """A message storing a command to run, `command` should be the
+    name of a :class:`hooke.command.Command` instance, and `arguments`
+    should be a :class:`dict` with `argname` keys and `value` values
+    to be passed to the command.
     """
     def __init__(self, command, arguments=None):
         self.command = command
     """
     def __init__(self, command, arguments=None):
         self.command = command
@@ -73,9 +73,10 @@ class CommandEngine (object):
                 break
             assert isinstance(msg, CommandMessage), type(msg)
             log.debug('engine running %s with %s'
                 break
             assert isinstance(msg, CommandMessage), type(msg)
             log.debug('engine running %s with %s'
-                      % (msg.command.name, msg.arguments))
-            msg.command.run(hooke, ui_to_command_queue, command_to_ui_queue,
-                            **msg.arguments)
+                      % (msg.command, msg.arguments))
+            cmd = hooke.command_by_name[msg.command]
+            cmd.run(hooke, ui_to_command_queue, command_to_ui_queue,
+                    **msg.arguments)
 
     def run_command(self, hooke, command, arguments):
         """Internal command execution.
 
     def run_command(self, hooke, command, arguments):
         """Internal command execution.
@@ -88,5 +89,6 @@ class CommandEngine (object):
         interaction.
         """
         log.debug('engine running internal %s with %s'
         interaction.
         """
         log.debug('engine running internal %s with %s'
-                  % (command.name, arguments))
-        command.run(hooke, NullQueue(), NullQueue(), arguments)
+                  % (command, arguments))
+        cmd = hooke.command_by_name[command]
+        cmd.run(hooke, NullQueue(), NullQueue(), arguments)
index b16a800c738005b2e3ecc50079430f914daad8da..cf6600ed8714fc8e6b4916a317935d79bb3e5a61 100644 (file)
@@ -107,6 +107,8 @@ class Hooke (object):
         self.commands = []
         for plugin in self.plugins:
             self.commands.extend(plugin.commands())
         self.commands = []
         for plugin in self.plugins:
             self.commands.extend(plugin.commands())
+        self.command_by_name = dict(
+            [(c.name, c) for c in self.commands])
 
     def load_drivers(self):
         self.drivers = plugin_mod.load_graph(
 
     def load_drivers(self):
         self.drivers = plugin_mod.load_graph(
@@ -170,7 +172,7 @@ class HookeRunner (object):
     def _cleanup_run(self, ui_to_command, command_to_ui, command):
         log = logging.getLogger('hooke')
         log.debug('cleanup sending CloseEngine')
     def _cleanup_run(self, ui_to_command, command_to_ui, command):
         log = logging.getLogger('hooke')
         log.debug('cleanup sending CloseEngine')
-        ui_to_command.put(ui.CloseEngine())
+        ui_to_command.put(engine.CloseEngine())
         hooke = None
         while not isinstance(hooke, Hooke):
             log.debug('cleanup waiting for Hooke instance from the engine.')
         hooke = None
         while not isinstance(hooke, Hooke):
             log.debug('cleanup waiting for Hooke instance from the engine.')
index 1b939144b6f2c1c1f693bca8088891507661e982..28e523f713e360908d4509236d8b609d5a886b7f 100644 (file)
@@ -23,7 +23,6 @@ import ConfigParser as configparser
 
 from .. import version
 from ..config import Setting
 
 from .. import version
 from ..config import Setting
-from ..engine import CloseEngine, CommandMessage
 from ..util.pluggable import IsSubclass, construct_odict
 
 try:
 from ..util.pluggable import IsSubclass, construct_odict
 
 try:
index 4b4914398b896682b0b6edee0e30ec3432e6a407..14236ca24580e883e4e38596cc162ffb29f292fc 100644 (file)
@@ -130,7 +130,7 @@ class DoCommand (CommandMethod):
             self.cmd.stdout.write('Failure\n')
             return
         self.log.debug('executing %s with %s' % (self.command.name, args))
             self.cmd.stdout.write('Failure\n')
             return
         self.log.debug('executing %s with %s' % (self.command.name, args))
-        self.cmd.inqueue.put(CommandMessage(self.command, args))
+        self.cmd.inqueue.put(CommandMessage(self.command.name, args))
         while True:
             msg = self.cmd.outqueue.get()
             if isinstance(msg, Exit):
         while True:
             msg = self.cmd.outqueue.get()
             if isinstance(msg, Exit):
index 8f1e41bfcf81635bf999539bc221d359dfcbae27..f8851774b06fee9ebb248816fb0c902fc6285c4d 100644 (file)
@@ -336,7 +336,7 @@ class HookeFrame (wx.Frame):
                     if len(args[arg.name]) == 0:
                         args[arg.name] = arg.default
         self.log.debug('executing %s with %s' % (command.name, args))
                     if len(args[arg.name]) == 0:
                         args[arg.name] = arg.default
         self.log.debug('executing %s with %s' % (command.name, args))
-        self.inqueue.put(CommandMessage(command, args))
+        self.inqueue.put(CommandMessage(command.name, args))
         results = []
         while True:
             msg = self.outqueue.get()
         results = []
         while True:
             msg = self.outqueue.get()