~ubuntu-branches/ubuntu/quantal/pytables/quantal

« back to all changes in this revision

Viewing changes to tables/undoredo.py

  • Committer: Bazaar Package Importer
  • Author(s): Sebastian Dröge
  • Date: 2005-11-27 20:25:34 UTC
  • mfrom: (1.1.1 upstream)
  • Revision ID: james.westby@ubuntu.com-20051127202534-l8jzyd8357krw40h
Tags: 1.1.1-1ubuntu1
* Sync with Debian:
  + Use python 2.4 as default

Show diffs side-by-side

added added

removed removed

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