From e8a7630e498137249d3ff9a2a47df4452070da97 Mon Sep 17 00:00:00 2001 From: "W. Trevor King" Date: Thu, 12 Aug 2010 11:33:41 -0400 Subject: [PATCH] Change CommandMessage.command from Command instance to command's name. 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 | 41 +++++++++++++++------------------------- hooke/engine.py | 20 +++++++++++--------- hooke/hooke.py | 4 +++- hooke/ui/__init__.py | 1 - hooke/ui/commandline.py | 2 +- hooke/ui/gui/__init__.py | 2 +- 6 files changed, 31 insertions(+), 39 deletions(-) diff --git a/hooke/command_stack.py b/hooke/command_stack.py index 3888a45..1e6c27c 100644 --- a/hooke/command_stack.py +++ b/hooke/command_stack.py @@ -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 diff --git a/hooke/engine.py b/hooke/engine.py index 5bc2f6e..e5fd184 100644 --- a/hooke/engine.py +++ b/hooke/engine.py @@ -35,10 +35,10 @@ class CloseEngine (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 @@ -73,9 +73,10 @@ class CommandEngine (object): 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. @@ -88,5 +89,6 @@ class CommandEngine (object): 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) diff --git a/hooke/hooke.py b/hooke/hooke.py index b16a800..cf6600e 100644 --- a/hooke/hooke.py +++ b/hooke/hooke.py @@ -107,6 +107,8 @@ class Hooke (object): 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( @@ -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') - 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.') diff --git a/hooke/ui/__init__.py b/hooke/ui/__init__.py index 1b93914..28e523f 100644 --- a/hooke/ui/__init__.py +++ b/hooke/ui/__init__.py @@ -23,7 +23,6 @@ import ConfigParser as configparser from .. import version from ..config import Setting -from ..engine import CloseEngine, CommandMessage from ..util.pluggable import IsSubclass, construct_odict try: diff --git a/hooke/ui/commandline.py b/hooke/ui/commandline.py index 4b49143..14236ca 100644 --- a/hooke/ui/commandline.py +++ b/hooke/ui/commandline.py @@ -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.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): diff --git a/hooke/ui/gui/__init__.py b/hooke/ui/gui/__init__.py index 8f1e41b..f885177 100644 --- a/hooke/ui/gui/__init__.py +++ b/hooke/ui/gui/__init__.py @@ -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)) - self.inqueue.put(CommandMessage(command, args)) + self.inqueue.put(CommandMessage(command.name, args)) results = [] while True: msg = self.outqueue.get() -- 2.26.2