FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
Design-Patterns-in-Python/command.py at master · coder-zh/Design-Patterns-in-Python · GitHub
coder-zh
/
Design-Patterns-in-Python
Public
forked from
gennad/Design-Patterns-in-Python
Notifications
You must be signed in to change notification settings
Fork
0
Star
0
Code
Pull requests
0
Actions
Projects
Wiki
Security and quality
0
Insights
Additional navigation options
Code
Pull requests
Actions
Projects
Wiki
Security and quality
Insights
Expand file tree
Breadcrumbs
Design-Patterns-in-Python
/
command.py
Copy path
More file actions
More file actions
Latest commit
History
History
History
133 lines (96 loc) · 2.89 KB
Breadcrumbs
Design-Patterns-in-Python
/
command.py
Copy path
File metadata and controls
133 lines (96 loc) · 2.89 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
# -*- coding: utf8 -*-
from
sys
import
stdout
as
console
# Handling 'exit' command
class
SessionClosed
(
Exception
):
def
__init__
(
self
,
value
):
self
.
value
=
value
# Interface
class
Command
:
def
execute
(
self
):
raise
NotImplementedError
()
def
cancel
(
self
):
raise
NotImplementedError
()
def
name
():
raise
NotImplementedError
()
# rm command
class
RmCommand
(
Command
):
def
execute
(
self
):
console
.
write
(
"You are executed
\"
rm
\"
command
\n
"
)
def
cancel
(
self
):
console
.
write
(
"You are canceled
\"
rm
\"
command
\n
"
)
def
name
(
self
):
return
"rm"
# uptime command
class
UptimeCommand
(
Command
):
def
execute
(
self
):
console
.
write
(
"You are executed
\"
uptime
\"
command
\n
"
)
def
cancel
(
self
):
console
.
write
(
"You are canceled
\"
uptime
\"
command
\n
"
)
def
name
(
self
):
return
"uptime"
# undo command
class
UndoCommand
(
Command
):
def
execute
(
self
):
try
:
cmd
=
HISTORY
.
pop
()
TRASH
.
append
(
cmd
)
console
.
write
(
"Undo command
\"
{0}
\"
\n
"
.
format
(
cmd
.
name
()))
cmd
.
cancel
()
except
IndexError
:
console
.
write
(
"ERROR: HISTORY is empty
\n
"
)
def
name
(
self
):
return
"undo"
# redo command
class
RedoCommand
(
Command
):
def
execute
(
self
):
try
:
cmd
=
TRASH
.
pop
()
HISTORY
.
append
(
cmd
)
console
.
write
(
"Redo command
\"
{0}
\"
\n
"
.
format
(
cmd
.
name
()))
cmd
.
execute
()
except
IndexError
:
console
.
write
(
"ERROR: TRASH is empty
\n
"
)
def
name
(
self
):
return
"redo"
# history command
class
HistoryCommand
(
Command
):
def
execute
(
self
):
i
=
0
for
cmd
in
HISTORY
:
console
.
write
(
"{0}: {1}
\n
"
.
format
(
i
,
cmd
.
name
()))
i
=
i
+
1
def
name
(
self
):
print
"history"
# exit command
class
ExitCommand
(
Command
):
def
execute
(
self
):
raise
SessionClosed
(
"Good bay!"
)
def
name
(
self
):
return
"exit"
# available commands
COMMANDS
=
{
'rm'
:
RmCommand
(),
'uptime'
:
UptimeCommand
(),
'undo'
:
UndoCommand
(),
'redo'
:
RedoCommand
(),
'history'
:
HistoryCommand
(),
'exit'
:
ExitCommand
()}
HISTORY
=
list
()
TRASH
=
list
()
# Shell
def
main
():
try
:
while
True
:
console
.
flush
()
console
.
write
(
"pysh >> "
)
cmd
=
raw_input
()
try
:
command
=
COMMANDS
[
cmd
]
command
.
execute
()
if
(
not
isinstance
(
command
,
UndoCommand
)
and
not
isinstance
(
command
,
RedoCommand
)
and
not
isinstance
(
command
,
HistoryCommand
)):
TRASH
=
list
()
HISTORY
.
append
(
command
)
except
KeyError
:
console
.
write
(
"ERROR: Command
\"
%s
\"
not found
\n
"
%
cmd
)
except
SessionClosed
as
e
:
console
.
write
(
e
.
value
)
if
__name__
==
"__main__"
:
main
()
Back
|
FazBrowse Home
|
New Git URL