Coverage for /pythoncovmergedfiles/medio/medio/usr/local/lib/python3.8/site-packages/tables/undoredo.py: 23%

77 statements  

« prev     ^ index     » next       coverage.py v7.2.5, created at 2023-05-10 06:15 +0000

1"""Support for undoing and redoing actions. 

2 

3Functions: 

4 

5* undo(file, operation, *args) 

6* redo(file, operation, *args) 

7* move_to_shadow(file, path) 

8* move_from_shadow(file, path) 

9* attr_to_shadow(file, path, name) 

10* attr_from_shadow(file, path, name) 

11 

12Misc variables: 

13 

14`__docformat__` 

15 The format of documentation strings in this module. 

16 

17""" 

18 

19from .path import split_path 

20 

21 

22__docformat__ = 'reStructuredText' 

23"""The format of documentation strings in this module.""" 

24 

25 

26def undo(file_, operation, *args): 

27 if operation == 'CREATE': 

28 undo_create(file_, args[0]) 

29 elif operation == 'REMOVE': 

30 undo_remove(file_, args[0]) 

31 elif operation == 'MOVE': 

32 undo_move(file_, args[0], args[1]) 

33 elif operation == 'ADDATTR': 

34 undo_add_attr(file_, args[0], args[1]) 

35 elif operation == 'DELATTR': 

36 undo_del_attr(file_, args[0], args[1]) 

37 else: 

38 raise NotImplementedError("the requested unknown operation %r can " 

39 "not be undone; please report this to the " 

40 "authors" % operation) 

41 

42 

43def redo(file_, operation, *args): 

44 if operation == 'CREATE': 

45 redo_create(file_, args[0]) 

46 elif operation == 'REMOVE': 

47 redo_remove(file_, args[0]) 

48 elif operation == 'MOVE': 

49 redo_move(file_, args[0], args[1]) 

50 elif operation == 'ADDATTR': 

51 redo_add_attr(file_, args[0], args[1]) 

52 elif operation == 'DELATTR': 

53 redo_del_attr(file_, args[0], args[1]) 

54 else: 

55 raise NotImplementedError("the requested unknown operation %r can " 

56 "not be redone; please report this to the " 

57 "authors" % operation) 

58 

59 

60def move_to_shadow(file_, path): 

61 node = file_._get_node(path) 

62 

63 (shparent, shname) = file_._shadow_name() 

64 node._g_move(shparent, shname) 

65 

66 

67def move_from_shadow(file_, path): 

68 (shparent, shname) = file_._shadow_name() 

69 node = shparent._f_get_child(shname) 

70 

71 (pname, name) = split_path(path) 

72 parent = file_._get_node(pname) 

73 node._g_move(parent, name) 

74 

75 

76def undo_create(file_, path): 

77 move_to_shadow(file_, path) 

78 

79 

80def redo_create(file_, path): 

81 move_from_shadow(file_, path) 

82 

83 

84def undo_remove(file_, path): 

85 move_from_shadow(file_, path) 

86 

87 

88def redo_remove(file_, path): 

89 move_to_shadow(file_, path) 

90 

91 

92def undo_move(file_, origpath, destpath): 

93 (origpname, origname) = split_path(origpath) 

94 

95 node = file_._get_node(destpath) 

96 origparent = file_._get_node(origpname) 

97 node._g_move(origparent, origname) 

98 

99 

100def redo_move(file_, origpath, destpath): 

101 (destpname, destname) = split_path(destpath) 

102 

103 node = file_._get_node(origpath) 

104 destparent = file_._get_node(destpname) 

105 node._g_move(destparent, destname) 

106 

107 

108def attr_to_shadow(file_, path, name): 

109 node = file_._get_node(path) 

110 attrs = node._v_attrs 

111 value = getattr(attrs, name) 

112 

113 (shparent, shname) = file_._shadow_name() 

114 shattrs = shparent._v_attrs 

115 

116 # Set the attribute only if it has not been kept in the shadow. 

117 # This avoids re-pickling complex attributes on REDO. 

118 if shname not in shattrs: 

119 shattrs._g__setattr(shname, value) 

120 

121 attrs._g__delattr(name) 

122 

123 

124def attr_from_shadow(file_, path, name): 

125 (shparent, shname) = file_._shadow_name() 

126 shattrs = shparent._v_attrs 

127 value = getattr(shattrs, shname) 

128 

129 node = file_._get_node(path) 

130 node._v_attrs._g__setattr(name, value) 

131 

132 # Keeping the attribute in the shadow allows reusing it on Undo/Redo. 

133 # shattrs._g__delattr(shname) 

134 

135 

136def undo_add_attr(file_, path, name): 

137 attr_to_shadow(file_, path, name) 

138 

139 

140def redo_add_attr(file_, path, name): 

141 attr_from_shadow(file_, path, name) 

142 

143 

144def undo_del_attr(file_, path, name): 

145 attr_from_shadow(file_, path, name) 

146 

147 

148def redo_del_attr(file_, path, name): 

149 attr_to_shadow(file_, path, name)