/
index.py
161 lines (136 loc) · 4.59 KB
/
index.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
import json
from files import File
import os.path
import sys
makeChanges = 0
def file_id_assigner():
if not list(JSON_structure["files"].keys()):
file_id = 0
else:
file_id = int(list(JSON_structure["files"].keys())[-1])
file_id += 1
return file_id
def chunk_id_assigner(fileIndexes):
if not list(JSON_structure["files"][fileIndexes]["chunks"].keys()):
chunk_id = 0
else:
chunk_id = int(list(JSON_structure["files"][fileIndexes]["chunks"].keys())[-1])
chunk_id += 1
return chunk_id
def load_JSON():
global JSON_structure
if os.path.isfile('file_structure.json'):
with open('file_structure.json') as JSON_Infile:
JSON_structure = json.load(JSON_Infile)
else:
print("File not exist")
def create_file():
global makeChanges
file_name = input("Enter File name: ")
file = File(file_id_assigner(), file_name, 0, {})
JSON_structure["files"].update(file.create_f())
print("File Created Successfully!")
makeChanges = 1
def delete_file():
global makeChanges
file_name = input("Enter File name: ")
FnF = False
for fileIndexes in list(JSON_structure["files"]):
if JSON_structure["files"][fileIndexes]["name"] != file_name:
FnF = True
else:
FnF = False
JSON_structure["meta_data"]["storage"] -= JSON_structure["files"][fileIndexes]["size"]
del JSON_structure["files"][fileIndexes]
break
if FnF:
print("File not found")
if not FnF:
print("File Deleted Successfully!")
makeChanges = 1
def open_for_write(file_name):
global makeChanges
makeChanges = 1
chunkSize = 20
FnF = False
for fileIndexes in JSON_structure["files"].keys():
if JSON_structure["files"][fileIndexes]["name"] != file_name:
FnF = True
else:
FnF = False
Text = input("Enter Text: ")
JSON_structure["files"][fileIndexes]["size"] += len(Text)
JSON_structure["meta_data"]["storage"] += JSON_structure["files"][fileIndexes]["size"]
for i in range(0, len(Text), chunkSize):
JSON_structure["files"][fileIndexes]["chunks"].update({str(chunk_id_assigner(fileIndexes)): Text[i:i + chunkSize]})
break
if FnF:
print("File not found")
if not FnF:
print("Data writing Successful!")
def open_for_read(file_name):
fullData = ""
FnF = False
for fileIndexes in JSON_structure["files"].keys():
if JSON_structure["files"][fileIndexes]["name"] != file_name:
FnF = True
else:
FnF = False
for data in JSON_structure["files"][fileIndexes]["chunks"].keys():
fullData += JSON_structure["files"][fileIndexes]["chunks"][data] + ""
break
if FnF:
print("File not found")
if not FnF:
print(fullData)
def open_file():
while True:
file_name = input("Enter file name: ")
openOptions = "\n1. Open for Read\n2. Open for Write\n3. Close File"
print(openOptions)
openChoice = input("Enter value: ")
if openChoice == "1":
open_for_read(file_name)
if openChoice == "2":
open_for_write(file_name)
if openChoice == "3":
break
def show_map():
print(json.dumps(JSON_structure, indent=4))
def dump_JSON():
with open('file_structure.json', "w") as JSON_Outfile:
json.dump(JSON_structure, JSON_Outfile, indent=4)
print("Changes Saved!")
def close_program():
if makeChanges > 0:
print("Do you want save changes to file_structure.json?\n1.Yes\n2.No")
haltInput = input("Enter value: ")
if haltInput == "1":
dump_JSON()
sys.exit()
elif haltInput == "2":
sys.exit()
else:
print("Invalid input")
else:
sys.exit()
startProgram = 1
while True:
if startProgram == 1:
load_JSON()
menu_options = "\n1. Create File\n2. Delete File\n3. Open File\n4. Show Map\n5. Kill Program"
print(menu_options)
user_choice = input("Enter value: ")
if user_choice == "1":
create_file()
elif user_choice == "2":
delete_file()
elif user_choice == "3":
open_file()
elif user_choice == "4":
show_map()
elif user_choice == "5":
close_program()
else:
print("Invalid input")
startProgram += 1