-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathFileManagementSystem.py
More file actions
356 lines (304 loc) · 12 KB
/
FileManagementSystem.py
File metadata and controls
356 lines (304 loc) · 12 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
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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
import os
import pickle
from FileClass import File
from DirectoryClass import Directory
from MainMemory import MainMemory
class FileManagementSystem:
def __init__(self):
self.root = Directory("root")
self.current_directory = self.root
self.files = {}
self.Memory = MainMemory()
self.subdirectories = {}
def create_file(self, name):
file = File(name)
self.files[name] = file
self.current_directory.add_file(file)
def create_directory(self, name):
directoryPath = name.split("/")
current_directory = self.current_directory
for i in directoryPath:
if i == "":
continue
for directory in current_directory.subdirectories:
if directory.name == i:
current_directory = directory
break
if directoryPath[-1] == "":
directory = Directory(directoryPath[-2])
else:
directory = Directory(directoryPath[-1])
current_directory.add_subdirectory(directory)
self.subdirectories[name] = directory
def delete_file(self, name):
try:
current_directory = self.current_directory
for file in current_directory.files:
if file.name == name:
file.delete(self.Memory)
current_directory.remove_file(file)
self.files.pop(name)
return
except:
print("file not found")
def delete_directory(self, directoryName):
try:
directoryPath = directoryName.split("/")
current_directory = self.current_directory
for i in directoryPath:
if i == "":
continue
for directory in current_directory.subdirectories:
if directory.name == i:
current_directory = directory
break
for file in current_directory.files:
file.delete(self.Memory)
current_directory.parent.subdirectories.remove(current_directory)
except:
print("directory not found")
def append_file(self, name, data):
try:
file = self.files[name]
if file.file_size == 0:
file.write(data, self.Memory)
else:
file.append(data, self.Memory)
except KeyError:
print("The current directory has no such file")
except:
print("Something went wrong")
def print_used_blocks(self):
self.Memory.print_blocks()
def write_file(self, name, data):
try:
current_directory = self.current_directory
my_file = current_directory.find_file(name)
my_file.write(data, self.Memory)
return my_file
except:
print("The current directory has no such file")
def truncate_file(self, name, size):
try:
current_directory = self.current_directory
my_file = current_directory.find_file(name)
my_file.truncatefile(self.Memory, size)
except KeyError:
print("The current directory has no such file")
except:
print("Something went wrong")
def readFile(self, name):
try:
current_directory = self.current_directory
my_file = current_directory.find_file(name)
if my_file == None:
print("The current directory has no such file")
else:
return my_file.read(self.Memory)
except KeyError:
print("The current directory has no such file")
except ValueError:
print("The file is empty")
except:
print("Something went wrong, file not found")
def MoveContent(self, name, start, end, newstart):
try:
current_directory = self.current_directory
my_file = current_directory.find_file(name)
my_file.moveContentWithinFile(self.Memory, start, end, newstart)
except:
print("No file found")
def change_directory(self, name):
if self.current_directory.parent == None and name == "..":
return
if name == "..":
self.current_directory = self.current_directory.parent
else:
pathToDirectory = name.split("/")
for name in pathToDirectory:
if name == "":
continue
for directory in self.current_directory.subdirectories:
if directory.name == name:
self.current_directory = directory
break
def moveFileInDirectory(self, fileName, newDirectory):
try:
current_directory = self.current_directory
my_file = current_directory.find_file(fileName)
current_directory.remove_file(my_file)
except:
print("File not found")
return
if newDirectory == "..":
if current_directory.parent == None:
return
current_directory = current_directory.parent
duplicate = current_directory.find_file(fileName)
if duplicate != None:
current_directory.remove_file(duplicate)
print("Duplicate file found, deleting duplicate")
current_directory.add_file(my_file)
return
newFileDirectory = newDirectory.split("/")
for i in newFileDirectory:
if i == "":
continue
for directory in current_directory.subdirectories:
if directory.name == i:
current_directory = directory
break
duplicate = current_directory.find_file(fileName)
if duplicate != None:
current_directory.remove_file(duplicate)
print("Duplicate file found, deleting duplicate")
current_directory.add_file(my_file)
def passWorkingDirectory(self):
current_directory = self.current_directory
path = ""
while current_directory != None and current_directory.parent is not None:
path = "/" + current_directory.name + path
current_directory = current_directory.parent
if path == "":
path = "/"
return path
def MemoryMap(self, current_directory=None):
if current_directory is None:
current_directory = self.root
print("/")
spaces = current_directory.level * 2
for file in current_directory.files:
blocks = ""
for block in file.blocks:
blocks += str(block) + ","
print(" " * spaces + file.name + " "
+ str(file.file_size) + " " + blocks)
for directory in current_directory.subdirectories:
if directory.name == self.current_directory.name:
print(" " * spaces + "*" + directory.name + "/")
else:
print(" " * spaces + directory.name + "/")
self.MemoryMap(directory)
def save(self):
with open("file_system.pickle", "wb") as file:
pickle.dump(self, file)
def load(self):
with open("file_system.pickle", "rb") as file:
return pickle.load(file)
def listAll(self):
for i in self.current_directory.subdirectories:
print(i)
for i in self.current_directory.files:
print(i)
'''
Adding the Terminal Interface
'''
def terminal(self):
while True:
prompt = input("\n> ").split(" ")
command = prompt[0]
# Exit the terminal: exit
if command == "exit":
break
# Print the help menu: help
elif command == "help":
print(
"""Commands:
ls - list all files and directories
pwd - print working directory
cd - change directory
mkdir - make directory
rmdir - remove directory
touch - create file
rm - remove file
wr - write to file
ap - append to file
tr - truncate file
mvc - move content within file
mvf - move file to another directory
cat - read file
mmap - show memory map
help - show list of commands
save - save file system
exit - exit terminal
""")
pass
# List all files and directories in the current directory: ls
elif command == "ls":
self.listAll()
pass
# Pass the working directory: pwd
elif command == "pwd":
print(self.passWorkingDirectory())
# Change Directory: cd <dirname>
elif command == "cd":
self.change_directory(prompt[1])
pass
# Create a directory (add to the current directory): mkdir <dirname>
elif command == "mkdir":
self.create_directory(prompt[1])
pass
elif command == "blocks":
self.print_used_blocks()
# Delete a directory (remove from the current directory): rmdir <dirname>
elif command == "rmdir":
try:
self.delete_directory(prompt[1])
pass
except:
print("Directory not found")
# Create a file (add to the directory): touch <filename>
elif command == "touch":
self.create_file(prompt[1])
pass
# Delete a file (remove from the directory): rm <filename>
elif command == "rm":
self.delete_file(prompt[1])
pass
# Write to a file (overwrite): wr <filename> <text>
elif command == "wr":
written_text = ""
for i in range(2, len(prompt)):
written_text += prompt[i] + ""
self.write_file(prompt[1], written_text)
# Append to a file (add to the end): ap <filename> <text>
elif command == "ap":
appended_text = ""
for i in range(2, len(prompt)):
appended_text += prompt[i] + ""
self.append_file(prompt[1], appended_text)
# Truncate a file (remove content): tr <filename> <end bit>
elif command == "tr":
try:
self.truncate_file(prompt[1], int(prompt[2]))
except:
print("Please enter prompts correctly")
# Move content within a file: mvc <filename> <start> <size> <newstart/target>
elif command == "mvc":
try:
self.MoveContent(prompt[1], int(
prompt[2]), int(prompt[3]), int(prompt[4]))
except:
print("Please enter prompts correctly")
# Move a file to a different directory: mvf <filename> <new directory>
elif command == "mvf":
self.moveFileInDirectory(prompt[1], prompt[2])
# Read a file: cat <filename>
elif command == "cat":
try:
if self.readFile(prompt[1]) != None:
print(self.readFile(prompt[1]))
except:
print("Please enter prompts correctly")
pass
# Show the memory map: mmap
elif command == "mmap":
self.MemoryMap()
pass
# Save the file system: save
elif command == "save":
self.save()
# Invalid command
else:
print("Invalid Command")
print("Type 'help' for a list of commands")