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
6 # modify it under the terms of the GNU Lesser General Public
7 # License as published by the Free Software Foundation, either
8 # version 3 of the License, or (at your option) any later version.
10 # Hooke is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU Lesser General 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.
23 from .ui import CloseEngine, CommandMessage
25 class CommandEngine (object):
26 def run(self, hooke, ui_to_command_queue, command_to_ui_queue):
27 """Get a :class:`hooke.ui.QueueMessage` from the incoming
28 `ui_to_command_queue` and act accordingly.
30 If the message is a :class:`hooke.ui.CommandMessage` instance,
31 the command run may read subsequent input from
32 `ui_to_command_queue` (if it is an interactive command). The
33 command may also put assorted data into `command_to_ui_queue`.
35 When the command completes, it will put a
36 :class:`hooke.command.CommandExit` instance into
37 `command_to_ui_queue`, at which point the `CommandEngine` will
38 be ready to receive the next :class:`hooke.ui.QueueMessage`.
41 msg = ui_to_command_queue.get()
42 if isinstance(msg, CloseEngine):
43 command_to_ui_queue.put(hooke)
45 assert isinstance(msg, CommandMessage), type(msg)
46 msg.command.run(hooke, ui_to_command_queue, command_to_ui_queue,