X-Git-Url: http://git.tremily.us/?p=hooke.git;a=blobdiff_plain;f=hooke%2Fengine.py;h=e024c737357f9abc8aa9d57110c1218d4b3dc2af;hp=75f74fe2a48908ee5787559b80649a8b194200d0;hb=b90995fb4b6d8151df862d40edc8c369d7052cfa;hpb=28a01c259df23e587352035f08e97295e01b3fe2 diff --git a/hooke/engine.py b/hooke/engine.py index 75f74fe..e024c73 100644 --- a/hooke/engine.py +++ b/hooke/engine.py @@ -47,6 +47,39 @@ class CommandMessage (QueueMessage): arguments = {} self.arguments = arguments + def __str__(self): + return str(self.__unicode__()) + + def __unicode__(self): + """Return a unicode representation of the `CommandMessage`. + + Examples + -------- + >>> from .compat.odict import odict + >>> cm = CommandMessage('command A') + >>> print unicode(cm) + + >>> cm.arguments = odict([('param a','value a'), ('param b', 'value b')]) + >>> print unicode(cm) + + + The parameters are sorted by name, so you won't be surprised + in any doctests, etc. + + >>> cm.arguments = odict([('param b','value b'), ('param a', 'value a')]) + >>> print unicode(cm) + + """ + if len(self.arguments) > 0: + return u'<%s %s {%s}>' % ( + self.__class__.__name__, self.command, + ', '.join(['%s: %s' % (key, value) + for key,value in sorted(self.arguments.items())])) + return u'<%s %s>' % (self.__class__.__name__, self.command) + + def __repr__(self): + return self.__str__() + class CommandEngine (object): def run(self, hooke, ui_to_command_queue, command_to_ui_queue): @@ -73,14 +106,14 @@ class CommandEngine (object): 'engine closing, placed hooke instance in return queue') break assert isinstance(msg, CommandMessage), type(msg) - log.debug('engine running %s with %s' - % (msg.command, msg.arguments)) + log.debug('engine running %s' % msg) cmd = hooke.command_by_name[msg.command] cmd.run(hooke, ui_to_command_queue, command_to_ui_queue, **msg.arguments) def run_command(self, hooke, command, arguments): - """Internal command execution. + """Run the command named `command` with `arguments` using + :meth:`~hooke.engine.CommandEngine.run_command`. This allows commands to execute sub commands and enables :class:`~hooke.command_stack.CommandStack` execution. @@ -90,10 +123,10 @@ class CommandEngine (object): interaction. """ log = logging.getLogger('hooke') - log.debug('engine running internal %s with %s' - % (command, arguments)) - cmd = hooke.command_by_name[command] + log.debug('engine running internal %s' + % CommandMessage(command, arguments)) outqueue = Queue() + cmd = hooke.command_by_name[command] cmd.run(hooke, NullQueue(), outqueue, **arguments) while True: try: