Add test/get_curve.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 import logging
24
25 from .ui import CloseEngine, CommandMessage
26
27
28 class CommandEngine (object):
29     def run(self, hooke, ui_to_command_queue, command_to_ui_queue):
30         """Get a :class:`hooke.ui.QueueMessage` from the incoming
31         `ui_to_command_queue` and act accordingly.
32
33         If the message is a :class:`hooke.ui.CommandMessage` instance,
34         the command run may read subsequent input from
35         `ui_to_command_queue` (if it is an interactive command).  The
36         command may also put assorted data into `command_to_ui_queue`.
37
38         When the command completes, it will put a
39         :class:`hooke.command.CommandExit` instance into
40         `command_to_ui_queue`, at which point the `CommandEngine` will
41         be ready to receive the next :class:`hooke.ui.QueueMessage`.
42         """
43         log = logging.getLogger('hooke')
44         while True:
45             log.debug('engine waiting for command')
46             msg = ui_to_command_queue.get()
47             if isinstance(msg, CloseEngine):
48                 command_to_ui_queue.put(hooke)
49                 log.debug(
50                     'engine closing, placed hooke instance in return queue')
51                 break
52             assert isinstance(msg, CommandMessage), type(msg)
53             log.debug('engine running %s' % msg.command.name)
54             msg.command.run(hooke, ui_to_command_queue, command_to_ui_queue,
55                             **msg.arguments)