-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
233 lines (178 loc) · 5.44 KB
/
main.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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
import requests
import json
# Greeting the user and displaying options
def mainPage():
print("Choose an option")
print("1. Open Tab")
print("2. Close Tab")
print("3. Switch Tab")
print("4. Display All Tabs")
print("5. Open Nested Tab")
print("6. Sort All Tabs")
print("7. Save Tabs")
print("8. Import Tabs")
print("9. Exit")
# Adding new tab
def addANewTab(Tabs):
Tab = {}
Tab['Title'] = input("Please enter the title: ")
Tab['URL'] = "https://" + input("Please enter the url: ")
Tab['nestedTabs'] = []
Tabs.append(Tab)
print("Tab is successfully opened.")
# Closing tab
def closeTab(Tabs):
i = input("Input the index of the tab you wish to close: ")
# check if input is empty
if i == "":
Tabs.pop()
print("Tab closed successfully")
# check if input is valid
elif i.isdigit()==True:
if 0 <= int(i) <= len(Tabs):
Tabs.pop(int(i))
print("Tab closed successfully")
else:
print("Invalid input.")
else:
print("Invalid input")
# Displaying parent tab content
def switchTabs(Tabs):
i = input("Input the index of the tab you wish to open: ")
if i == "":
i = len(Tabs) - 1
# Checking for the validity of the index
elif i.isdigit() == True:
if int(i) < 0 or int(i) >= len(Tabs):
print("Invalid index")
return
try:
req = requests.get(Tabs[int(i)]['URL'])
# Checking the success of the request
if req.status_code == 200:
print(req.text)
else:
print("Content can't be loaded")
return
except requests.RequestException:
print("Not available URL")
return
else:
print("Invalid index.")
# Displaying titles of opened tabs
def printTitles(Tabs):
for tab in Tabs:
print(tab['Title'])
if len(tab['nestedTabs']) != 0:
for i in tab['nestedTabs']:
print("\t" + i['Title'])
# Creating nested tabs
def createNestedTabs(Tabs):
tab = {}
# Checking if Tabs list contain any tab inorder to create a nested tab
if len(Tabs) != 0:
i = input("The index of the parent tab where the nested tab is created: ")
# Checking the validity of the index(i should be a positive integer only)
if i.isdigit() == True:
if int(i) >= 0 and int(i) < len(Tabs):
tab['Title'] = input("Please enter the title of nested tab: ")
tab['Content'] = input("Please enter the content of nested tab: ")
Tabs[int(i)]['nestedTabs'].append(tab)
print("Nested tab added successfully")
else:
print("Invalid index")
else:
print("Invalid index")
else:
print("At least one tab should be opened to create a nested tab.")
# Sorting tabs according to titles
def sortTabs(Tabs):
if len(Tabs) > 1:
mid = len(Tabs) // 2
left = Tabs[:mid]
right = Tabs[mid:]
sortTabs(left)
sortTabs(right)
i = j = k = 0
while i < len(left) and j < len(right):
if left[i]['Title'] < right[j]['Title']:
Tabs[k] = left[i]
i += 1
else:
Tabs[k] = right[j]
j += 1
k += 1
while i < len(left):
Tabs[k] = left[i]
i += 1
k += 1
while j < len(right):
Tabs[k] = right[j]
j += 1
k += 1
else:
return
# Sorting nested tabs
def sortNestedTabs(Tabs):
for tab in Tabs:
if len(tab['nestedTabs']) > 1:
sortTabs(tab['nestedTabs'])
# Saving tabs
def saveTabs(Tabs):
# Taking file path from user
filePath = input("Enter the file path: ")
try:
# Opening file
file = open(filePath, 'w')
# Saving tabs in file
json.dump(Tabs, file)
print("Files saved successfully")
except IOError:
print("Error saving tabs")
# Loading tabs from file
def loadTabs(Tabs):
loadedTabs = []
# Taking file path from user
filePath = input("Enter the file path:")
try:
# Openning file
file = open(filePath, 'r')
# Loading tabs from file and adding them to Tabs list
loadedTabs = json.load(file)
Tabs.extend(loadedTabs)
# Adding loaded tabs to Tabs list
print("Tabs loaded successfully")
except IOError:
print("Error loading tabs")
if __name__ == '__main__':
# Declaring list of opened tabs
Tabs = []
# Greeting the user
print("Hello, welcome to my program!")
while True:
mainPage()
choice = input("Input your choice(1---9): ")
if choice == '1':
addANewTab(Tabs)
elif choice == '2':
closeTab(Tabs)
elif choice == '3':
switchTabs(Tabs)
elif choice == '4':
printTitles(Tabs)
elif choice == '5':
createNestedTabs(Tabs)
elif choice == '6':
sortTabs(Tabs)
sortNestedTabs(Tabs)
elif choice == '7':
saveTabs(Tabs)
elif choice == '8':
loadTabs(Tabs)
elif choice == '9':
print("Program closed")
break
# Check validity of choice
else:
print("Input is invalid.")
continue