-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfile_structure.py
executable file
·216 lines (159 loc) · 5.43 KB
/
file_structure.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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
class TrieNode(object):
"""
Trie Node having two data members
char: Name of the directory
children: list of directories in a directory
"""
def __init__(self, char):
self.char = char
self.children = []
class FileStructure(object):
"""
File Structure involving Linux Path Traversal
"""
def __init__(self):
"""
File Structure starts from root directory and have 3 data members
root: TrieNode having root directory
current: Current path of a file from root
point: Directory where currently file structure is
"""
self.root = TrieNode('/')
self.current = []
self.point = self.root
def get_pwd(self):
"""
Method required to return the path of current directory from root
"""
if self.point == self.root:
return "PATH: " + self.point.char
s = "/"
return "PATH: /" + s.join(self.current)
def process(self, cmd: str):
"""
Method require to manage which method to call for particular command
"""
if cmd == 'pwd':
return self.get_pwd()
cmd = cmd.strip().split()
if cmd[0] in ['ls', 'dir']:
return self.list_dir()
elif cmd[0] == 'mkdir':
return self.new_dir(cmd[1])
elif cmd[0] == 'cd':
return self.change_dir(cmd[1])
elif cmd[0] == 'rm':
return self.remove_dir(cmd[1])
elif cmd[0].endswith('dir'):
return self.list_dir(cmd)
else:
return "ERR: CANNOT RECOGNIZE INPUT"
def list_dir(self, cmd=None):
"""
Method required to return the list of all the directories
present in a directory
"""
childs = []
start = "DIRS: "
if cmd:
dirs = cmd[0].split('/')
node = self.point
for char in dirs[:-1]:
found_in_child = False
for child in node.children:
if child.char == char:
found_in_child = True
node = child
break
if not found_in_child:
return "ERR: INVALID PATH"
for child in node.children:
childs.append(child.char)
else:
for child in self.point.children:
childs.append(child.char)
return start + " ".join(childs)
def new_dir(self, word):
"""
Method required to return the response whether dirs bean created or not
"""
word = word.strip().split('/')
if len(word) == 1:
for child in self.point.children:
if child.char == word[0]:
return "ERR: DIRECTORY ALREADY EXISTS"
new_node = TrieNode(word[0])
self.point.children.append(new_node)
return "SUCC: CREATED"
node = self.point
count = 0
for char in word:
found_in_child = False
for child in node.children:
if child.char == char:
count += 1
node = child
found_in_child = True
break
if not found_in_child and count == len(word) - 1:
new_node = TrieNode(char)
node.children.append(new_node)
count += 1
break
if count == len(word):
return "SUCC: CREATED"
return "ERR: DIRECTORY ALREADY EXISTS"
def change_dir(self, word):
"""
Method required to return whether file structure can be traverse
through a particular path
"""
word = word.strip().split('/')
if len(word) == 1:
for child in self.point.children:
if child.char == word[0]:
self.point = child
self.current.append(child.char)
return "SUCC: REACHED"
return "ERR: INVALID PATH"
curr = self.current
node = self.point
for char in word:
found_in_child = False
for child in node.children:
if child.char == char:
found_in_child = True
curr.append(child.char)
node = child
break
if not found_in_child:
return "ERR: INVALID PATH"
self.current = curr
self.point = node
return "SUCC: REACHED"
def remove_dir(self, word):
"""
Method required to remove/delete a directory
"""
word = word.strip().split('/')
if len(word) == 1:
for child in self.point.children:
if child.char == word[0]:
self.current.pop(child.char)
return "SUCC: DELETED"
return "ERR: INVALID PATH"
curr = self.current
node = self.point
count = 0
for char in word:
found_in_child = False
for child in node.children:
if child.char == char:
found_in_child = True
count += 1
curr = node
node = child
if found_in_child and count == len(word):
curr.children.remove(child)
return "SUCC: DELETED"
return "ERR: INVALID PATH"