f5f6d05df95137e5a83e39bd15bc8c7734fa34a0
[hooke.git] / hooke / engine.py
1 # Copyright
2
3 """The `engine` module provides :class:`CommandEngine` for executing
4 :class:`hooke.command.Command`\s.
5 """
6
7 from .ui import CloseEngine, CommandMessage
8
9 class CommandEngine (object):
10     def run(self, hooke, ui_to_command_queue, command_to_ui_queue):
11         """Get a :class:`hooke.ui.QueueMessage` from the incoming
12         `ui_to_command_queue` and act accordingly.
13
14         If the message is a :class:`hooke.ui.CommandMessage` instance,
15         the command run may read subsequent input from
16         `ui_to_command_queue` (if it is an interactive command).  The
17         command may also put assorted data into `command_to_ui_queue`.
18
19         When the command completes, it will put a
20         :class:`hooke.command.CommandExit` instance into
21         `command_to_ui_queue`, at which point the `CommandEngine` will
22         be ready to receive the next :class:`hooke.ui.QueueMessage`.
23         """
24         while True:
25             msg = ui_to_command_queue.get()
26             if isinstance(msg, CloseEngine):
27                 break
28             assert isinstance(msg, CommandMessage), type(msg)
29             msg.command.run(hooke, ui_to_command_queue, command_to_ui_queue,
30                             **msg.arguments)