1 # Copyright (C) 2010 W. Trevor King <wking@drexel.edu>
3 # This file is part of Hooke.
5 # Hooke is free software: you can redistribute it and/or modify it
6 # under the terms of the GNU Lesser General Public License as
7 # published by the Free Software Foundation, either version 3 of the
8 # License, or (at your option) any later version.
10 # Hooke is distributed in the hope that it will be useful, but WITHOUT
11 # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
12 # or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General
13 # Public License for more details.
15 # You should have received a copy of the GNU Lesser General Public
16 # License along with Hooke. If not, see
17 # <http://www.gnu.org/licenses/>.
19 """The `engine` module provides :class:`CommandEngine` for executing
20 :class:`hooke.command.Command`\s.
24 from Queue import Queue, Empty
26 from .command import NullQueue
29 class QueueMessage (object):
31 return self.__class__.__name__
34 class CloseEngine (QueueMessage):
38 class CommandMessage (QueueMessage):
39 """A message storing a command to run, `command` should be the
40 name of a :class:`hooke.command.Command` instance, and `arguments`
41 should be a :class:`dict` with `argname` keys and `value` values
42 to be passed to the command.
44 def __init__(self, command, arguments=None):
45 self.command = command
48 self.arguments = arguments
51 return str(self.__unicode__())
53 def __unicode__(self):
54 """Return a unicode representation of the `CommandMessage`.
58 >>> from .compat.odict import odict
59 >>> cm = CommandMessage('command A')
61 <CommandMessage command A>
62 >>> cm.arguments = odict([('param a','value a'), ('param b', 'value b')])
64 <CommandMessage command A {param a: value a, param b: value b}>
66 The parameters are sorted by name, so you won't be surprised
69 >>> cm.arguments = odict([('param b','value b'), ('param a', 'value a')])
71 <CommandMessage command A {param a: value a, param b: value b}>
73 if len(self.arguments) > 0:
74 return u'<%s %s {%s}>' % (
75 self.__class__.__name__, self.command,
76 ', '.join(['%s: %s' % (key, value)
77 for key,value in sorted(self.arguments.items())]))
78 return u'<%s %s>' % (self.__class__.__name__, self.command)
84 class CommandEngine (object):
85 def run(self, hooke, ui_to_command_queue, command_to_ui_queue):
86 """Get a :class:`QueueMessage` from the incoming
87 `ui_to_command_queue` and act accordingly.
89 If the message is a :class:`CommandMessage` instance, the
90 command run may read subsequent input from
91 `ui_to_command_queue` (if it is an interactive command). The
92 command may also put assorted data into `command_to_ui_queue`.
94 When the command completes, it will put a
95 :class:`hooke.command.CommandExit` instance into
96 `command_to_ui_queue`, at which point the `CommandEngine` will
97 be ready to receive the next :class:`QueueMessage`.
99 log = logging.getLogger('hooke')
100 log.debug('engine starting')
101 for playlist in hooke.playlists: # Curve._hooke is not pickled.
102 for curve in playlist:
103 curve.set_hooke(hooke)
105 log.debug('engine waiting for command')
106 msg = ui_to_command_queue.get()
107 if isinstance(msg, CloseEngine):
108 command_to_ui_queue.put(hooke)
110 'engine closing, placed hooke instance in return queue')
112 assert isinstance(msg, CommandMessage), type(msg)
113 log.debug('engine running %s' % msg)
114 cmd = hooke.command_by_name[msg.command]
115 cmd.run(hooke, ui_to_command_queue, command_to_ui_queue,
118 def run_command(self, hooke, command, arguments):
119 """Run the command named `command` with `arguments` using
120 :meth:`~hooke.engine.CommandEngine.run_command`.
122 This allows commands to execute sub commands and enables
123 :class:`~hooke.command_stack.CommandStack` execution.
125 Note that these commands *do not* have access to the usual UI
126 communication queues, so make sure they will not need user
129 log = logging.getLogger('hooke')
130 log.debug('engine running internal %s'
131 % CommandMessage(command, arguments))
133 cmd = hooke.command_by_name[command]
134 cmd.run(hooke, NullQueue(), outqueue, **arguments)
137 msg = outqueue.get(block=False)
140 log.debug('engine message from %s (%s): %s' % (command, type(msg), msg))