forked from faif/python-patterns
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcommand.py
More file actions
134 lines (105 loc) · 3.49 KB
/
Copy pathcommand.py
File metadata and controls
134 lines (105 loc) · 3.49 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
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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import traceback
import sys
import os
import shutil
from os.path import lexists
class Command(object):
def execute(self):
pass
def undo(self):
pass
class CopyFileCommand(Command):
# TODO handle src parameter as path
def __init__(self, src ):
self.src = src
self.dest = "Copy_"+src
self.name = "Copy " + src
def execute(self):
self.copy(self.src, self.dest)
def undo(self):
print('remove {} '.format( self.dest))
os.remove( self.dest)
def copy(self , src,dest ):
print('copy {} to {}'.format(src, dest))
shutil.copy(src, dest)
class MoveFileCommand(Command):
def __init__(self, src, dest):
self.src = src
self.dest = dest
self.name="Move "+src
def execute(self):
self.rename(self.src, self.dest)
def undo(self):
self.rename(self.dest, self.src)
def rename(self, src, dest):
print('renaming {} to {}'.format(src, dest))
os.rename(src, dest)
def main():
command_stack = []
# commands are just pushed into the command stack
command_stack.append(CopyFileCommand('foo1.txt'))
command_stack.append(CopyFileCommand('Copy_foo1.txt'))
command_stack.append(MoveFileCommand('foo.txt', 'bar.txt'))
command_stack.append(MoveFileCommand('bar.txt', 'baz.txt'))
names=["foo.txt","baz.txt","foo1.txt"]
for name in names:
try:
os.unlink(name)
except:
pass
finally:
pass
# verify that none of the target files exist
assert(not lexists("foo.txt"))
assert(not lexists("bar.txt"))
assert(not lexists("baz.txt"))
try:
with open("foo.txt", "w") as src1: # Creating the file
src1.close()
with open("foo1.txt", "w") as src2: # Creating the file
src2.close()
# they can be executed later on
for cmd in command_stack:
print "******Executing "+cmd.name
cmd.execute()
# and can also be undone at will
for cmd in reversed(command_stack):
print "" \
"------Undo " + cmd.name
cmd.undo()
except :
#e = sys.exc_info()[0]
#print("Error: %s" % e)
exc_type, exc_value, exc_traceback = sys.exc_info()
print "*** print_tb:"
traceback.print_tb(exc_traceback, limit=1, file=sys.stdout)
print "*** print_exception:"
traceback.print_exception(exc_type, exc_value, exc_traceback,
limit=2, file=sys.stdout)
print "*** print_exc:"
traceback.print_exc()
print "*** format_exc, first and last line:"
formatted_lines = traceback.format_exc().splitlines()
print formatted_lines[0]
print formatted_lines[-1]
print "*** format_exception:"
print repr(traceback.format_exception(exc_type, exc_value,
exc_traceback))
print "*** extract_tb:"
print repr(traceback.extract_tb(exc_traceback))
print "*** format_tb:"
print repr(traceback.format_tb(exc_traceback))
print "*** tb_lineno:", exc_traceback.tb_lineno
finally:
print "finaly block "
os.unlink("foo.txt")
os.unlink("foo1.txt")
if __name__ == "__main__":
main()
### OUTPUT ###
# renaming foo.txt to bar.txt
# renaming bar.txt to baz.txt
# renaming baz.txt to bar.txt
# renaming bar.txt to foo.txt