X-Git-Url: http://git.tremily.us/?a=blobdiff_plain;f=hooke%2Fcommand_stack.py;h=202e599a7ffafcaf703dd48768016c04401c7cf6;hb=90c034314299eeca540286cc87dfdeedb60eac0a;hp=234b3f3d85dfbd0ed7f42584c1d04f51517cf6be;hpb=f1b96b0334501099017e9665d60ff238c37a01fa;p=hooke.git diff --git a/hooke/command_stack.py b/hooke/command_stack.py index 234b3f3..202e599 100644 --- a/hooke/command_stack.py +++ b/hooke/command_stack.py @@ -1,4 +1,4 @@ -# Copyright (C) 2010 W. Trevor King +# Copyright (C) 2010-2011 W. Trevor King # # This file is part of Hooke. # @@ -39,7 +39,7 @@ class CommandStack (list): >>> c.append(CommandMessage('CommandB', {'param':'D'})) Implement a dummy :meth:`execute_command` for testing. - + >>> def execute_cmd(hooke, command_message, stack=None): ... cm = command_message ... print 'EXECUTE', cm.command, cm.arguments @@ -59,11 +59,10 @@ class CommandStack (list): >>> def filter(hooke, command_message): ... return command_message.command == 'CommandB' - >>> c.filter = filter Apply the stack to the current curve. - >>> c.execute(hooke=None) # doctest: +ELLIPSIS + >>> c.execute(hooke=None, filter=filter) # doctest: +ELLIPSIS EXECUTE CommandB {'param': 'B'} EXECUTE CommandB {'param': 'D'} @@ -80,47 +79,127 @@ class CommandStack (list): '', ''] - The data-type is also pickleable, to ensure we can move it between - processes with :class:`multiprocessing.Queue`\s and easily save it - to disk. + The data-type is also pickleable, which ensures we can move it + between processes with :class:`multiprocessing.Queue`\s and easily + save it to disk. We must remove the unpickleable dummy executor + before testing though. + + >>> c.execute_command # doctest: +ELLIPSIS + + >>> del(c.__dict__['execute_command']) + >>> c.execute_command # doctest: +ELLIPSIS + + + Lets also attach a child command message to demonstrate recursive + serialization (we can't append `c` itself because of + `Python issue 1062277`_). + + .. _Python issue 1062277: http://bugs.python.org/issue1062277 + + >>> import copy + >>> c.append(CommandMessage('CommandD', {'param': copy.deepcopy(c)})) + + Run the pickle (and YAML) tests. >>> import pickle >>> s = pickle.dumps(c) >>> z = pickle.loads(s) - >>> print [repr(cm) for cm in c] # doctest: +NORMALIZE_WHITESPACE - ['', - '', - '', - '', - ''] + >>> print '\\n'.join([repr(cm) for cm in c] + ... ) # doctest: +NORMALIZE_WHITESPACE, + + + + + + , + , + , + , + ]}> + >>> import yaml + >>> print yaml.dump(c) # doctest: +REPORT_UDIFF + !!python/object/new:hooke.command_stack.CommandStack + listitems: + - !!python/object:hooke.engine.CommandMessage + arguments: {param: A} + command: CommandA + explicit_user_call: true + - !!python/object:hooke.engine.CommandMessage + arguments: {param: B} + command: CommandB + explicit_user_call: true + - !!python/object:hooke.engine.CommandMessage + arguments: {param: C} + command: CommandA + explicit_user_call: true + - !!python/object:hooke.engine.CommandMessage + arguments: {param: D} + command: CommandB + explicit_user_call: true + - !!python/object:hooke.engine.CommandMessage + arguments: {param: E} + command: CommandC + explicit_user_call: true + - !!python/object:hooke.engine.CommandMessage + arguments: + param: !!python/object/new:hooke.command_stack.CommandStack + listitems: + - !!python/object:hooke.engine.CommandMessage + arguments: {param: A} + command: CommandA + explicit_user_call: true + - !!python/object:hooke.engine.CommandMessage + arguments: {param: B} + command: CommandB + explicit_user_call: true + - !!python/object:hooke.engine.CommandMessage + arguments: {param: C} + command: CommandA + explicit_user_call: true + - !!python/object:hooke.engine.CommandMessage + arguments: {param: D} + command: CommandB + explicit_user_call: true + - !!python/object:hooke.engine.CommandMessage + arguments: {param: E} + command: CommandC + explicit_user_call: true + command: CommandD + explicit_user_call: true + There is also a convenience function for clearing the stack. >>> c.clear() >>> print [repr(cm) for cm in c] [] - """ - def __getstate__(self): - state = [{'command':cm.command, 'arguments':cm.arguments} - for cm in self] - return state - def __setstate__(self, state): - self.clear() - for cm_state in state: - self.append(CommandMessage( - command=cm_state['command'], - arguments=cm_state['arguments'])) - - def execute(self, hooke, stack=False): + YAMLize a curve argument. + + >>> from .curve import Curve + >>> c.append(CommandMessage('curve info', {'curve': Curve(path=None)})) + >>> print yaml.dump(c) # doctest: +REPORT_UDIFF + !!python/object/new:hooke.command_stack.CommandStack + listitems: + - !!python/object:hooke.engine.CommandMessage + arguments: + curve: !!python/object:hooke.curve.Curve {} + command: curve info + explicit_user_call: true + + """ + def execute(self, hooke, filter=None, stack=False): """Execute a stack of commands. See Also -------- - _execute, filter + execute_command, filter """ + if filter == None: + filter = self.filter for command_message in self: - if self.filter(hooke, command_message) == True: + if filter(hooke, command_message) == True: self.execute_command( hooke=hooke, command_message=command_message, stack=stack) @@ -151,20 +230,10 @@ class FileCommandStack (CommandStack): super(FileCommandStack, self).__init__(*args, **kwargs) self.name = self.path = None - def __getstate__(self): - command_stack = super(FileCommandStack, self).__getstate__() - state = { - 'command stack': command_stack, - 'path': self.path, - 'name': self.name, - } - return state - def __setstate__(self, state): - super(FileCommandStack, self).__setstate__( - state.get('command stack', [])) - self.name = state.get('name', None) - self.path = None + self.name = self.path = None + for key,value in state.items(): + setattr(self, key, value) self.set_path(state.get('path', None)) def set_path(self, path):