Ran update_copyright.py
[hooke.git] / hooke / engine.py
1 # Copyright (C) 2010 W. Trevor King <wking@drexel.edu>
2 #
3 # This file is part of Hooke.
4 #
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.
9 #
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.
14 #
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/>.
18
19 """The `engine` module provides :class:`CommandEngine` for executing
20 :class:`hooke.command.Command`\s.
21 """
22
23 from .ui import CloseEngine, CommandMessage
24
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.
29
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`.
34
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`.
39         """
40         while True:
41             msg = ui_to_command_queue.get()
42             if isinstance(msg, CloseEngine):
43                 command_to_ui_queue.put(hooke)
44                 break
45             assert isinstance(msg, CommandMessage), type(msg)
46             msg.command.run(hooke, ui_to_command_queue, command_to_ui_queue,
47                             **msg.arguments)