1 # Copyright (C) 2010 W. Trevor King <wking@drexel.edu>
3 # This file is part of Hooke.
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.
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.
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/>.
19 """The `engine` module provides :class:`CommandEngine` for executing
20 :class:`hooke.command.Command`\s.
26 class QueueMessage (object):
28 return self.__class__.__name__
31 class CloseEngine (QueueMessage):
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.
41 def __init__(self, command, arguments=None):
42 self.command = command
45 self.arguments = arguments
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.
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`.
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`.
63 log = logging.getLogger('hooke')
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)
70 'engine closing, placed hooke instance in return queue')
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,