-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcommandpatterns.py
142 lines (107 loc) · 3.72 KB
/
commandpatterns.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
"""Implementation of linux commands - ls, touch, rm"""
import abc
import os
history = []
class Command(object):
"""Command interface"""
__metaclass__ = abc.ABCMeta
@abc.abstractmethod
def execute(self):
"""Method to execute the command"""
pass
@abc.abstractmethod
def undo(self):
"""A method to undo the command"""
pass
class LsCommand(Command):
"""Concrete command that emulates ls unix command behavior"""
def __init__(self, receiver):
self.receiver = receiver
def execute(self):
"""The command delegates the call to its receiver"""
self.receiver.show_current_dir()
def undo(self):
"""can not undo ls command"""
pass
class LsReceiver(object):
def show_current_dir(self):
"""The receiver knows how to execute the command"""
cur_dir = './'
filenames = []
for filename in os.listdir(cur_dir):
if os.path.isfile(os.path.join(cur_dir, filename)):
filenames.append(filename)
print filenames
# print 'Content of dir: ', ' '.(os.path.join(filenames))
class TouchCommand(Command):
"""Concrete command to emulate touch"""
def __init__(self, receiver):
self.receiver = receiver
def execute(self):
self.receiver.create_file()
def undo(self):
self.receiver.delete_file()
class TouchReceiver(object):
def __init__(self, filename):
self.filename = filename
def create_file(self):
"""Actual implementation of touch command"""
with file(self.filename, 'a'):
os.utime(self.filename, None)
def delete_file(self):
"""Undo unix touch command."""
os.remove(self.filename)
class RmCommand(Command):
"""Concrete command that emulates rm unix command"""
def __init__(self, receiver):
self.receiver = receiver
def execute(self):
self.receiver.delete_file()
def undo(self):
self.receiver.undo()
class RmReceiver(object):
def __init__(self, filename):
self.filename = filename
self.backup_name = None
def delete_file(self):
self.backup_name = '.' + self.filename
os.rename(self.filename, self.backup_name)
def undo(self):
original_name = self.backup_name[1:]
os.rename(self.backup_name, original_name)
self.backup_name = None
class Invoker(object):
def __init__(self, create_file_commands, delete_file_commands):
self.create_file_commands = create_file_commands
self.delete_file_commands = delete_file_commands
self.history = []
def create_file(self):
print 'Creating file....'
for command in self.create_file_commands:
command.execute()
self.history.append(command)
print 'File created \n'
def delete_file(self):
print 'Deleteing file...'
for command in self.delete_file_commands:
command.execute()
self.history.append(command)
print 'File deleted \n'
def undo_all(self):
print 'Undo all. ...'
for command in reversed(self.history):
command.undo()
print 'Undo all finished.'
if __name__ == '__main__':
ls_receiver = LsReceiver()
ls_command = LsCommand(ls_receiver)
touch_receiver = TouchReceiver('test_file')
touch_command = TouchCommand(touch_receiver)
rm_receiver = RmReceiver('test_file')
rm_command = RmCommand(rm_receiver)
create_file_commands = [ls_command, touch_command, ls_command]
delete_file_commands = [ls_command, rm_command, ls_command]
invoker = Invoker(create_file_commands, delete_file_commands)
invoker.create_file()
invoker.delete_file()
invoker.undo_all()