-
Notifications
You must be signed in to change notification settings - Fork 0
/
undopy.py
457 lines (398 loc) · 14.6 KB
/
undopy.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
def deepw(element, callbacks=[], undocallbacks=[], wrap=None, hoproot=False):
""" Wrap nested list and dict. """
if wrap:
result = wrap(element)
if result is not None:
return result
if type(element) == list:
inner = [deepw(subelem, callbacks, undocallbacks, wrap)
for subelem in element]
if hoproot:
return inner
return undo_list(inner, callbacks=callbacks, undocallbacks=undocallbacks)
elif type(element) == dict:
inner = dict((key, deepw(value, callbacks, undocallbacks, wrap))
for key, value in element.items())
if hoproot:
return inner
return undo_dict(inner, callbacks=callbacks, undocallbacks=undocallbacks)
else:
return element
class UndoLog(object):
def __init__(self):
self.root = self.rootundo = watched_tree("undo root")
self.observed = []
self.index = -1
def add(self, element):
""" Add element to observe. """
self.observed.append(element)
element.undocallbacks.append(self.log)
def log(self, element, undoelem, redoelem):
if element.skiplog > 0:
return
self.void_redo()
self.rootundo.append(watched_tree(name=(element, undoelem, redoelem)))
def void_redo(self):
if self.rootundo == self.root and self.index != -1:
del self.root[self.index+1:]
self.index = -1
def start_group(self, label, brand_new=False):
if brand_new and self.rootundo.name == label:
return False
self.void_redo()
self.rootundo.append(watched_tree(label))
self.index = -1
self.rootundo = self.rootundo[-1]
return True
def end_group(self, label, skip_unused=False, delete_void=False):
if label and self.rootundo.name != label:
if skip_unused: return
raise Exception("Ending group %s but the current group is %s!" % \
(label, self.rootundo.name))
if not self.rootundo.parent:
raise Exception("Attempting to end root group!")
self.rootundo = self.rootundo.parent
if delete_void and len(self.rootundo) == 0:
self.rootundo.pop()
self.index = -1
def undo(self, vertex=None):
if vertex is None:
vertex = self.root[self.index]
self.index -= 1
if type(vertex.name) == str:
for child in reversed(vertex):
self.undo(child)
else:
self.unredo_event(vertex.name[0], vertex.name[1])
def redo(self, vertex=None):
if vertex is None:
vertex = self.root[self.index + 1]
self.index += 1
if type(vertex.name) == str:
for child in vertex:
self.redo(child)
else:
self.unredo_event(vertex.name[0], vertex.name[2])
def unredo_event(self, element, item):
element.skiplog += 1
getattr(element, item[0])(*item[1:])
element.skiplog -= 1
def pprint(self, vertex=None):
for line in self.pprint_string(vertex):
print(line)
def pprint_string(self, vertex=None, indent=0):
if vertex is None:
vertex = self.root
if type(vertex.name) != str:
yield "%s%s" % (indent *" ", vertex.name[2][0])
return
name = vertex.name if vertex.name else ""
yield "%s%s" % (indent*" ", name)
for child in vertex:
for line in self.pprint_string(child, indent + 2):
yield line
class undo_list(list):
"""
undo_list that calls functions from self.undocallbacks
the undo,redo pair is used whenever an operation is applied to a list.
ehile self.callbacks is called redo
self.replace is not in python's list function itself, but is included due for convenient purposes
"""
def __init__(self, *args, **kwargs):
list.__init__(self, *args)
self.callbacks = kwargs.get("callbacks", [])
self.undocallbacks = kwargs.get("undocallbacks", [])
self.skiplog = 0
def callback(self, undo, redo):
for callback in self.callbacks:
callback(self, *redo)
for callback in self.undocallbacks:
callback(self, undo, redo)
def __deepcopy__(self, memo):
return undo_list(self)
def __setitem__(self, key, value):
try:
prevvalue = self.__getitem__(key)
except KeyError:
list.__setitem__(self, key, value)
self.callback(("__delitem__", key), ("__setitem__", key, value))
else:
list.__setitem__(self, key, value)
self.callback(("__setitem__", key, prevvalue),
("__setitem__", key, value))
def __delitem__(self, key):
prevvalue = list.__getitem__(self, key)
list.__delitem__(self, key)
self.callback(("__setitem__", key, prevvalue), ("__delitem__", key))
def __setslice__(self, i, j, seq):
prevvalue = list.__getslice__(self, i, j)
self.callback(("__setslice__", i, j, prevvalue),
("__setslice__", i, j, seq))
list.__setslice__(self, i, j, seq)
def __delslice__(self, i, j):
prevvalue = list.__getitem__(self, slice(i, j))
list.__delslice__(self, i, j)
self.callback(("__setslice__", i, i, prevvalue), ("__delslice__", i, j))
def append(self, val):
list.append(self, val)
self.callback(("pop",), ("append", val))
def pop(self, index=-1):
prevvalue = list.pop(self, index)
self.callback(("append", prevvalue), ("pop", index))
return prevvalue
def extend(self, newval):
oldlen = len(self)
list.extend(self, newval)
self.callback(("__delslice__", oldlen, len(self)),
("extend", self[oldlen:]))
def insert(self, i, elem):
list.insert(self, i, elem)
self.callback(("pop", i), ("insert", i, elem))
def remove(self, elem):
if elem in self:
oldindex = self.index(elem)
list.remove(self, elem)
self.callback(("insert", oldindex, elem), ("remove", elem))
def reverse(self):
list.reverse(self)
self.callback(("reverse",), ("reverse",))
def sort(self, *args, **kwargs):
prevlist = self[:]
list.sort(self, *args, **kwargs)
self.callback(("replace", prevlist), ("replace", self[:]))
def replace(self, nextlist):
prevlist = self[:]
self.skiplog += 1
del self[:]
try:
self.extend(nextlist)
except:
self.replace(prevlist) # check for inf loops
self.skiplog -= 1
raise
self.skiplog -= 1
self.callback(("replace", prevlist), ("replace", nextlist))
class undo_dict(dict):
def __init__(self, *args, **kwargs):
self.callbacks = kwargs.pop("callbacks", [])
self.undocallbacks = kwargs.pop("undocallbacks", [])
dict.__init__(self, *args, **kwargs)
self.skiplog = 0
def callback(self, undo, redo):
for callback in self.callbacks:
callback(self, *redo)
for callback in self.undocallbacks:
callback(self, undo, redo)
def __deepcopy__(self, memo):
return undo_dict(self)
def __setitem__(self, key, val):
try:
prevvalue = self.__getitem__(key)
except KeyError:
dict.__setitem__(self, key, val)
self.callback(("__delitem__", key), ("__setitem__", key, val))
else:
dict.__setitem__(self, key, val)
self.callback(("__setitem__", key, prevvalue),
("__setitem__", key, val))
def __delitem__(self, key):
prevvalue = self[key]
dict.__delitem__(self, key)
self.callback(("__setitem__", key, prevvalue), ("__delitem__", key))
def clear(self):
prevvalue = self.copy()
dict.clear(self)
self.callback(("update", prevvalue), ("clear",))
def update(self, update_dict):
prevvalue = self.copy()
dict.update(self, update_dict)
self.callback(("replace", prevvalue), ("update", update_dict))
def setdefault(self, key, val=None):
if key not in self:
dict.setdefault(self, key, val)
self.callback(("__delitem__", key), ("setdefault", key, val))
return val
else:
return self[key]
def pop(self, key, default=None):
if key in self:
val = dict.pop(self, key, default)
self.callback(("__setitem__", key, val), ("pop", key, default))
return val
else:
return default
def popitem(self):
key, val = dict.popitem(self)
self.callback(("__setitem__", key, val), ("popitem",))
return key, val
def replace(self, nextdict):
prevvalue = self.copy()
self.skiplog += 1
self.clear()
try:
self.update(nextdict)
except:
self.replace(prevvalue)
self.skiplog -= 1
raise
self.skiplog -= 1
self.callback(("replace", nextdict), ("replace", prevvalue))
class watched_tree(list):
"""Ordered list of children with parent pointers for consistency checking
Should contain one element
Could be used for modelling an xml or JSON document"""
def __init__(self, name=None, val=[], parent=None,
callbacks=None, undocallbacks=None):
list.__init__(self, val)
self.parent = parent
self.name = name
self.callbacks = callbacks if callbacks else []
self.undocallbacks = undocallbacks if undocallbacks else []
self.skiplog = 0
def callback(self, undo, redo, source=None):
# pass self here
if source is None:
source = self
for callback in self.callbacks:
callback(source, *redo)
for callback in self.undocallbacks:
callback(source, undo, redo)
if self.parent:
self.parent.callback(undo, redo, source)
def _reparent(self, nextparent, remove=False):
if remove and self.parent:
self.parent.remove(self, reparent=False)
self.parent = nextparent
def __setitem__(self, key, val):
try:
prevvalue = self.__getitem__(key)
except KeyError:
val._reparent(self, True)
list.__setitem__(self, key, val)
# check reparent undo
self.callback(("__delitem__", key), ("__setitem__", key, val))
else:
prevvalue._reparent(None)
val._reparent(self, True)
list.__setitem__(self, key, val)
self.callback(("__setitem__", key, prevvalue),
("__setitem__", key, val))
def __delitem__(self, key):
prevvalue = list.__getitem__(self, key)
list.__delitem__(self, key)
prevvalue._reparent(None)
# undo on the reparent needs checked here
# __setitem__ actually handles it for now
self.callback(("__setitem__", key, prevvalue), ("__delitem__", key))
def __setslice__(self, i, j, seq):
prevvalue = list.__getslice__(self, i, j)
self.callback(("__setslice__", i, j, prevvalue),
("__setslice__", i, j, seq))
for child in prevvalue:
child._reparent(None, True)
for child in seq:
child._reparent(self, True)
list.__setslice__(self, i, j, seq)
def __delslice__(self, i, j):
prevvalue = list.__getitem__(self, slice(i, j))
for child in prevvalue:
child._reparent(None, True)
list.__delslice__(self, i, j)
self.callback(("__setslice__", i, i, prevvalue), ("__delslice__", i, j))
def __eq__(self, other):
return self is other
def append(self, val):
list.append(self, val)
val._reparent(self, True)
self.callback(("pop",), ("append", val))
def pop(self, index=-1):
prevvalue = list.pop(self, index)
prevvalue._reparent(None)
self.callback(("append", prevvalue), ("pop", index))
return prevvalue
def extend(self, nextvalue):
prevlen = len(self)
list.extend(self, nextvalue)
for value in nextvalue:
value._reparent(self, True)
self.callback(("__delslice__", prevlen, len(self)),
("extend", self[prevlen:]))
def insert(self, i, elem):
elem._reparent(self, True)
list.insert(self, i, elem)
self.callback(("pop", i), ("insert", i, elem))
def remove(self, elem, reparent=True):
if elem in self:
previndex = self.index(elem)
list.remove(self, elem)
if reparent:
elem._reparent(None)
self.callback(("insert", previndex, elem), ("remove", elem))
def reverse(self):
list.reverse(self)
self.callback(("reverse",), ("reverse",))
def sort(self, *args, **kwargs):
prevlist = self[:]
list.sort(self, *args, **kwargs)
self.callback(("replace", prevlist), ("replace", self[:]))
def replace(self, nextlist):
prevlist = self[:]
self.skiplog += 1
del self[:]
try:
self.extend(nextlist)
except:
self.replace(prevlist) # check for inf loops
self.skiplog -= 1
raise
self.skiplog -= 1
self.callback(("replace", prevlist), ("replace", nextlist))
#Helper func
def maximal(self, condition):
""" Return maximal non chain among desc that satisfy conditions"""
for child in self:
if condition(child):
yield child
else:
for gc in child.maximal(condition):
yield gc
@property
def children(self):
for child in self:
for gc in child.children:
yield gc
yield child
# debugging for callbacks
def printargs(*args):
print(args)
if __name__ == '__main__':
# test cases
t = UndoLog()
d = undo_dict({1: "VU", 2: "UvA"})
l = undo_list([3, 3, 3])
t.add(d)
t.add(l)
l.append(1)
print(l)
t.undo()
print(l)
t.redo()
print(l)
d[3] = "CWI"
print(d)
t.undo()
print(d)
t.redo()
print(d)
t.start_group("foo")
d[53] = "person"
del d[1]
t.end_group("foo")
t.start_group("foo")
d["bar"] = "baz"
t.end_group("foo")
deep = {"cdf": "abc", "alist": [4, 5, 6]}
obs_deep = deepw(deep, undocallbacks=[t.log])
t.observed.append(obs_deep)
obs_deep["test"] = "1"
obs_deep["alist"].append(4)