-
Notifications
You must be signed in to change notification settings - Fork 0
/
undo_stack.py
48 lines (36 loc) · 1.22 KB
/
undo_stack.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
from copy import deepcopy
import inspect
class UndoStack:
"""
Basic stack really, returns a deepcopy due to annoying dict copying
"""
def __init__(self):
self.stack = []
self.redo_stack = []
def undo_push(self, item):
#print 'caller name:', inspect.stack()[1][3]
self.stack.insert(0, deepcopy(item))
self.history()
def undo_pop(self):
self.redo_push(deepcopy(self.stack.pop(0)))
return deepcopy(self.stack[0][1])
def redo_push(self, item):
self.redo_stack.insert(0, deepcopy(item))
def redo_pop(self):
item = deepcopy(self.redo_stack.pop(0))
self.undo_push(deepcopy(item))
return deepcopy(item[1])
def __len__(self):
return len(self.stack)
def reorder(self):
self.stack.sort()
self.stack.reverse()
return deepcopy(self.stack.pop(0)[1])
def timestamps(self):
undo = [clock for clock, session in self.stack]
redo = [clock for clock, session in self.redo_stack]
#print "Undo:", undo
#print "Redo:", redo
def history(self):
num_annotations = [len(session['annotations']) for clock, session in self.stack]
#print num_annotations