-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathundo.py
More file actions
66 lines (55 loc) · 1.67 KB
/
undo.py
File metadata and controls
66 lines (55 loc) · 1.67 KB
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
import logging
import types
import os
import traceback
undo = []
isundoing = False
def prettyaction(action):
args = ""
if len(action) > 1:
argsar = []
for arg in action[1:]:
if isinstance(arg, types.FunctionType):
argsar.append(arg.__name__)
else:
str(argsar.append(arg))
args = ",".join([str(arg) for arg in argsar])
if len(action) > 0:
return "%s (%s)" % (action[0].__name__, args)
else:
return "EMPTY ACTION - this will fail"
def pretty():
actions = []
for action in undo:
actions.append(prettyaction(action))
return os.linesep + os.linesep.join(actions) + os.linesep
def push(*kw):
# logging.debug("pushing %s to undo" % str(kw))
# traceback.print_stack()
if not isundoing:
undo.append(kw)
def pop():
return undo.pop()
def rollback():
global isundoing
dothis = None
erroredactions = []
try:
isundoing = True
logging.info("undoing: %s" % pretty())
while len(undo) > 0:
try:
dothis = pop()
dothis[0](*dothis[1:])
except Exception as e:
# if dothis is not None:
# undo.append(dothis)
logging.warn("FAILED TO UNDO %s: REMAINING%s moving on..." % (e, pretty()))
erroredactions.append((prettyaction(dothis), e))
logging.info("roll back complete")
finally:
isundoing = False
if len(erroredactions) > 0:
logging.info("Failed to rollback some items")
for erroredaction in erroredactions:
print erroredaction[0], erroredaction[1]