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
49 self.explicit_user_call = True
50 """Message is explicitly user-executed. This is useful for
51 distinguishing auto-generated calls (for which
52 `explicit_user_call` should be `False` such as the GUIs
53 current status requests.
57 return str(self.__unicode__())
59 def __unicode__(self):
60 """Return a unicode representation of the `CommandMessage`.
64 >>> from .compat.odict import odict
65 >>> cm = CommandMessage('command A')
67 <CommandMessage command A>
68 >>> cm.arguments = odict([('param a','value a'), ('param b', 'value b')])
70 <CommandMessage command A {param a: value a, param b: value b}>
72 The parameters are sorted by name, so you won't be surprised
75 >>> cm.arguments = odict([('param b','value b'), ('param a', 'value a')])
77 <CommandMessage command A {param a: value a, param b: value b}>
79 if len(self.arguments) > 0:
80 return u'<%s %s {%s}>' % (
81 self.__class__.__name__, self.command,
82 ', '.join(['%s: %s' % (key, value)
83 for key,value in sorted(self.arguments.items())]))
84 return u'<%s %s>' % (self.__class__.__name__, self.command)
90 class CommandEngine (object):
91 def run(self, hooke, ui_to_command_queue, command_to_ui_queue):
92 """Get a :class:`QueueMessage` from the incoming
93 `ui_to_command_queue` and act accordingly.
95 If the message is a :class:`CommandMessage` instance, the
96 command run may read subsequent input from
97 `ui_to_command_queue` (if it is an interactive command). The
98 command may also put assorted data into `command_to_ui_queue`.
100 When the command completes, it will put a
101 :class:`hooke.command.CommandExit` instance into
102 `command_to_ui_queue`, at which point the `CommandEngine` will
103 be ready to receive the next :class:`QueueMessage`.
105 log = logging.getLogger('hooke')
106 log.debug('engine starting')
107 for playlist in hooke.playlists: # Curve._hooke is not pickled.
108 for curve in playlist:
109 curve.set_hooke(hooke)
111 log.debug('engine waiting for command')
112 msg = ui_to_command_queue.get()
113 if isinstance(msg, CloseEngine):
114 command_to_ui_queue.put(hooke)
116 'engine closing, placed hooke instance in return queue')
118 assert isinstance(msg, CommandMessage), type(msg)
119 log.debug('engine running %s' % msg)
120 cmd = hooke.command_by_name[msg.command]
121 cmd.run(hooke, ui_to_command_queue, command_to_ui_queue,
124 def run_command(self, hooke, command, arguments):
125 """Run the command named `command` with `arguments` using
126 :meth:`~hooke.engine.CommandEngine.run_command`.
128 This allows commands to execute sub commands and enables
129 :class:`~hooke.command_stack.CommandStack` execution.
131 Note that these commands *do not* have access to the usual UI
132 communication queues, so make sure they will not need user
135 log = logging.getLogger('hooke')
136 log.debug('engine running internal %s'
137 % CommandMessage(command, arguments))
139 cmd = hooke.command_by_name[command]
140 cmd.run(hooke, NullQueue(), outqueue, **arguments)
143 msg = outqueue.get(block=False)
146 log.debug('engine message from %s (%s): %s' % (command, type(msg), msg))