1# -*- coding: utf-8 -*- 2 3######################################################################## 4# 5# License: BSD 6# Created: February 15, 2005 7# Author: Ivan Vilata - reverse:net.selidor@ivan 8# 9# $Source$ 10# $Id$ 11# 12######################################################################## 13 14"""Support for undoing and redoing actions. 15 16Functions: 17 18* undo(file, operation, *args) 19* redo(file, operation, *args) 20* move_to_shadow(file, path) 21* move_from_shadow(file, path) 22* attr_to_shadow(file, path, name) 23* attr_from_shadow(file, path, name) 24 25Misc variables: 26 27`__docformat__` 28 The format of documentation strings in this module. 29 30""" 31 32from .path import split_path 33 34 35__docformat__ = 'reStructuredText' 36"""The format of documentation strings in this module.""" 37 38 39def undo(file_, operation, *args): 40 if operation == 'CREATE': 41 undo_create(file_, args[0]) 42 elif operation == 'REMOVE': 43 undo_remove(file_, args[0]) 44 elif operation == 'MOVE': 45 undo_move(file_, args[0], args[1]) 46 elif operation == 'ADDATTR': 47 undo_add_attr(file_, args[0], args[1]) 48 elif operation == 'DELATTR': 49 undo_del_attr(file_, args[0], args[1]) 50 else: 51 raise NotImplementedError("the requested unknown operation %r can " 52 "not be undone; please report this to the " 53 "authors" % operation) 54 55 56def redo(file_, operation, *args): 57 if operation == 'CREATE': 58 redo_create(file_, args[0]) 59 elif operation == 'REMOVE': 60 redo_remove(file_, args[0]) 61 elif operation == 'MOVE': 62 redo_move(file_, args[0], args[1]) 63 elif operation == 'ADDATTR': 64 redo_add_attr(file_, args[0], args[1]) 65 elif operation == 'DELATTR': 66 redo_del_attr(file_, args[0], args[1]) 67 else: 68 raise NotImplementedError("the requested unknown operation %r can " 69 "not be redone; please report this to the " 70 "authors" % operation) 71 72 73def move_to_shadow(file_, path): 74 node = file_._get_node(path) 75 76 (shparent, shname) = file_._shadow_name() 77 node._g_move(shparent, shname) 78 79 80 81def move_from_shadow(file_, path): 82 (shparent, shname) = file_._shadow_name() 83 node = shparent._f_get_child(shname) 84 85 (pname, name) = split_path(path) 86 parent = file_._get_node(pname) 87 node._g_move(parent, name) 88 89 90 91def undo_create(file_, path): 92 move_to_shadow(file_, path) 93 94 95 96def redo_create(file_, path): 97 move_from_shadow(file_, path) 98 99 100 101def undo_remove(file_, path): 102 move_from_shadow(file_, path) 103 104 105 106def redo_remove(file_, path): 107 move_to_shadow(file_, path) 108 109 110 111def undo_move(file_, origpath, destpath): 112 (origpname, origname) = split_path(origpath) 113 114 node = file_._get_node(destpath) 115 origparent = file_._get_node(origpname) 116 node._g_move(origparent, origname) 117 118 119 120def redo_move(file_, origpath, destpath): 121 (destpname, destname) = split_path(destpath) 122 123 node = file_._get_node(origpath) 124 destparent = file_._get_node(destpname) 125 node._g_move(destparent, destname) 126 127 128 129def attr_to_shadow(file_, path, name): 130 node = file_._get_node(path) 131 attrs = node._v_attrs 132 value = getattr(attrs, name) 133 134 (shparent, shname) = file_._shadow_name() 135 shattrs = shparent._v_attrs 136 137 # Set the attribute only if it has not been kept in the shadow. 138 # This avoids re-pickling complex attributes on REDO. 139 if not shname in shattrs: 140 shattrs._g__setattr(shname, value) 141 142 attrs._g__delattr(name) 143 144 145 146def attr_from_shadow(file_, path, name): 147 (shparent, shname) = file_._shadow_name() 148 shattrs = shparent._v_attrs 149 value = getattr(shattrs, shname) 150 151 node = file_._get_node(path) 152 node._v_attrs._g__setattr(name, value) 153 154 # Keeping the attribute in the shadow allows reusing it on Undo/Redo. 155 # shattrs._g__delattr(shname) 156 157 158 159def undo_add_attr(file_, path, name): 160 attr_to_shadow(file_, path, name) 161 162 163 164def redo_add_attr(file_, path, name): 165 attr_from_shadow(file_, path, name) 166 167 168 169def undo_del_attr(file_, path, name): 170 attr_from_shadow(file_, path, name) 171 172 173 174def redo_del_attr(file_, path, name): 175 attr_to_shadow(file_, path, name) 176 177 178 179## Local Variables: 180## mode: python 181## py-indent-offset: 4 182## tab-width: 4 183## End: 184