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