Merged my unitary FFT wrappers (FFT_tools) as hooke.util.fft.
[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
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.
9 #
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.
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)