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
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
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.
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)
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 _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.')
from .. import version
from ..config import Setting
-from ..engine import CloseEngine, CommandMessage
from ..util.pluggable import IsSubclass, construct_odict
try:
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):
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()