-
Notifications
You must be signed in to change notification settings - Fork 5
/
commands.py
113 lines (90 loc) · 2.84 KB
/
commands.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
import pickle
import os
class Commands:
def __init__(self, path, task):
self.task = {}
self.path = path
self.dat = []
try:
self.del_in = int(task[0])
except:
pass
self.task = task
self.commands = {}
self.commands["store"] = self.store
self.commands["purge"] = self.purge
self.commands["show"] = self.show
self.commands["delete"]=self.delete
self.commands["search"] = self.search
self.commands["exit"] = self.exit
self.commands["cd"] = self.cd
self.commands["ls"] = self.ls
def exit(self):
exit()
def load_data(self):
try:
if os.path.isfile(self.path+"/dat"):
self.dat = pickle.load(open(self.path+"/dat", "rb"))
else:
self.dat = []
pickle.dump(self.dat,open(self.path+"/dat", "wb"))
except Exception as e:
print(e)
self.dat = []
pickle.dump(self.dat,open(self.path+"/dat", "wb"))
def store(self):
self.load_data()
coods = self.dat
path = self.path
task = " ".join(self.task)
tasks = pickle.load(open(path+"/dat", "rb"))
try:
tasks.append(task)
except KeyError:
tasks = [task]
pickle.dump(tasks, open(path+"/dat", "wb"))
return 1
def purge(self):
pickle.dump([], open( self.path+"/dat", "wb"))
return 1
def show(self):
self.load_data()
if len(self.dat) == 0:
print("Empty!")
else:
for i in range(len(self.dat)):
print(str(len(self.dat)-i) + " -> " + self.dat[len(self.dat)-i-1])
def delete(self):
self.load_data()
del self.dat[self.del_in-1]
pickle.dump(self.dat, open(self.path+"/dat", "wb"))
self.show()
def search(self):
self.load_data()
que = " ".join(self.task)
j = 0
for i in range(len(self.dat)):
word = self.dat[len(self.dat)-i-1]
if word.find(que) != -1:
j += 1
print(str(len(self.dat) - i) + " -> " + word, end = "\n")
if j == 0:
print("No matches found !!")
def get_dict(self):
return self.commands
def cd(self):
self.load_data()
os.system("echo -n %s > curr_dir"%self.task[0])
def ls(self):
self.load_data()
dirs = os.listdir(self.path+"/../")
curr_dir = self.path.split('/')[-1]
if 'debug' in dirs:
dirs.remove('debug')
if 'dummy' in dirs:
dirs.remove('dummy')
for dir in dirs:
if dir == curr_dir:
print(dir+" <---- curr")
else:
print(dir)