Moved QueueMessage and subclasses from hooke.ui to the more central hooke.engine.
[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
26 class QueueMessage (object):
27     def __str__(self):
28         return self.__class__.__name__
29
30
31 class CloseEngine (QueueMessage):
32     pass
33
34
35 class CommandMessage (QueueMessage):
36     """A message storing a command to run, `command` should be a
37     :class:`hooke.command.Command` instance, and `arguments` should be
38     a :class:`dict` with `argname` keys and `value` values to be
39     passed to the command.
40     """
41     def __init__(self, command, arguments=None):
42         self.command = command
43         if arguments == None:
44             arguments = {}
45         self.arguments = arguments
46
47
48 class CommandEngine (object):
49     def run(self, hooke, ui_to_command_queue, command_to_ui_queue):
50         """Get a :class:`QueueMessage` from the incoming
51         `ui_to_command_queue` and act accordingly.
52
53         If the message is a :class:`CommandMessage` instance, the
54         command run may read subsequent input from
55         `ui_to_command_queue` (if it is an interactive command).  The
56         command may also put assorted data into `command_to_ui_queue`.
57
58         When the command completes, it will put a
59         :class:`hooke.command.CommandExit` instance into
60         `command_to_ui_queue`, at which point the `CommandEngine` will
61         be ready to receive the next :class:`QueueMessage`.
62         """
63         log = logging.getLogger('hooke')
64         while True:
65             log.debug('engine waiting for command')
66             msg = ui_to_command_queue.get()
67             if isinstance(msg, CloseEngine):
68                 command_to_ui_queue.put(hooke)
69                 log.debug(
70                     'engine closing, placed hooke instance in return queue')
71                 break
72             assert isinstance(msg, CommandMessage), type(msg)
73             log.debug('engine running %s' % msg.command.name)
74             msg.command.run(hooke, ui_to_command_queue, command_to_ui_queue,
75                             **msg.arguments)