Change CommandMessage.command from Command instance to command's name.
[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 .command import NullQueue
26
27
28 class QueueMessage (object):
29     def __str__(self):
30         return self.__class__.__name__
31
32
33 class CloseEngine (QueueMessage):
34     pass
35
36
37 class CommandMessage (QueueMessage):
38     """A message storing a command to run, `command` should be the
39     name of a :class:`hooke.command.Command` instance, and `arguments`
40     should be a :class:`dict` with `argname` keys and `value` values
41     to be passed to the command.
42     """
43     def __init__(self, command, arguments=None):
44         self.command = command
45         if arguments == None:
46             arguments = {}
47         self.arguments = arguments
48
49
50 class CommandEngine (object):
51     def run(self, hooke, ui_to_command_queue, command_to_ui_queue):
52         """Get a :class:`QueueMessage` from the incoming
53         `ui_to_command_queue` and act accordingly.
54
55         If the message is a :class:`CommandMessage` instance, the
56         command run may read subsequent input from
57         `ui_to_command_queue` (if it is an interactive command).  The
58         command may also put assorted data into `command_to_ui_queue`.
59
60         When the command completes, it will put a
61         :class:`hooke.command.CommandExit` instance into
62         `command_to_ui_queue`, at which point the `CommandEngine` will
63         be ready to receive the next :class:`QueueMessage`.
64         """
65         log = logging.getLogger('hooke')
66         while True:
67             log.debug('engine waiting for command')
68             msg = ui_to_command_queue.get()
69             if isinstance(msg, CloseEngine):
70                 command_to_ui_queue.put(hooke)
71                 log.debug(
72                     'engine closing, placed hooke instance in return queue')
73                 break
74             assert isinstance(msg, CommandMessage), type(msg)
75             log.debug('engine running %s with %s'
76                       % (msg.command, msg.arguments))
77             cmd = hooke.command_by_name[msg.command]
78             cmd.run(hooke, ui_to_command_queue, command_to_ui_queue,
79                     **msg.arguments)
80
81     def run_command(self, hooke, command, arguments):
82         """Internal command execution.
83
84         This allows commands to execute sub commands and enables
85         :class:`~hooke.command_stack.CommandStack` execution.
86
87         Note that these commands *do not* have access to the usual UI
88         communication queues, so make sure they will not need user
89         interaction.
90         """
91         log.debug('engine running internal %s with %s'
92                   % (command, arguments))
93         cmd = hooke.command_by_name[command]
94         cmd.run(hooke, NullQueue(), NullQueue(), arguments)