-
Notifications
You must be signed in to change notification settings - Fork 3
/
manager.py
358 lines (255 loc) · 9.69 KB
/
manager.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
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
357
358
import os
import sys
import json
import re
from prettytable import PrettyTable
from termcolor import colored
current_working_workspace = None
def write_json(data, filename):
with open(filename,'w') as f:
json.dump(data, f, indent=4)
def list_workspaces(para,need_data):
try:
settings_file = open("/etc/contest-parser/settings.json","r")
except Exception:
print("Missing settings.json")
return None
settings = json.load(settings_file)
settings_file.close()
workspace_no = 1
workspace_list = []
workspaces_table = PrettyTable()
workspaces_table.field_names = ["ID","Workspace Name","Path"]
for workspace in settings["workspaces"]:
workspace_list.append([str(workspace_no),workspace["name"],workspace["path"]])
workspaces_table.add_row([str(workspace_no),workspace["name"],workspace["path"]])
workspace_no+=1
if(len(workspace_list)==0):
print("No Workspace Created!")
return None
print()
print(workspaces_table)
if(need_data):
return workspace_list
return None
def create_workspace(para):
try:
settings_file = open("/etc/contest-parser/settings.json","r")
except Exception:
print("Missing settings.json")
return None
workspace_json = {}
settings = json.load(settings_file)
print()
workspace_name = input("Enter Workspace Name: ")
workspace_name.rstrip()
for workspace in settings["workspaces"]:
if(workspace["name"]==workspace_name):
print("Workspace {} already exists!".format(workspace_name))
return None
workspace_json["name"] = workspace_name
workspace_path = input("Enter Workspace Directory Path: ")
workspace_path.replace('~',"/home/")
for workspace in settings["workspaces"]:
if(workspace["path"]==workspace_path):
print("Workspace with path '{}' already exists!".format(workspace_path))
return None
if(os.path.isfile(workspace_path)):
print("Invalid Path!")
return None
workspace_json["path"] = workspace_path
platform_table = PrettyTable()
platform_list = []
platform_table.field_names = ["ID","Platform"]
id = 1
for platform in settings["platforms"]:
platform_table.add_row([str(id),platform["name"]])
platform_list.append([id,platform["name"]])
id+=1
if(id==1):
print("No Supported Platform Found!")
return None
print(platform_table)
try:
platform_id = int(input("Enter Platform ID: "))
except Exception:
print("Invalid ID!")
return None
max_id = len(platform_list)
if(platform_id<1 or platform_id>max_id):
print("Invalid ID!")
return None
workspace_json["platform"] = platform_list[platform_id-1][1]
if(not os.path.isdir(workspace_path)):
os.mkdir(workspace_path)
"""
I need to get Programming language here, problem code template and cp snippets path here and add it in
workspace settings.json.
"""
if(workspace_path[-1]!='/'):
workspace_path = workspace_path + "/"
# Now create workspace setting.json and update global settings.json
workspace_settings_path = workspace_path + "settings.json"
write_json(workspace_json, workspace_settings_path)
settings["workspaces"].append({"name": workspace_name,"path": workspace_path})
write_json(settings, "settings.json")
print("Workspace Created!")
def set_workspace(para):
workspace_list = list_workspaces({},True)
if(workspace_list==None):
print("Cannot set Workspace!")
return None
print()
try:
workspace_id = int(input("Enter Workspace ID: "))
except Exception:
print("Invalid Workspace ID!")
return None
max_id = len(workspace_list)
if(workspace_id<1 or workspace_id>max_id):
print("Invalid Workspace ID!")
return None
global current_working_workspace
current_working_workspace = workspace_list[workspace_id-1]
print()
print("Workspace Set Successfully!")
return None
def current_workspace(para):
if(current_working_workspace==None):
print("Workspace Not Set!")
return None
print()
print("Workspace ID: {}, Name: {}, Path: '{}'".format(current_working_workspace[0],current_working_workspace[1],current_working_workspace[2]))
def check_flags(user_flags, valid_flags):
for flag in user_flags:
if(flag not in valid_flags):
print("Invalid Command!")
return False
elif((user_flags[flag]==None and valid_flags[flag]!=None) or (user_flags[flag]!=None and valid_flags[flag]==None)):
print("Invalid Command!")
return False
return True
def get_contest(para):
# Run generator here with flag as contest
os.system('python3 /etc/contest-parser/'+'Codeforces'+'/generator.py "'+current_working_workspace[2]+'"')
return None
def get_problem(para):
# Run generator here with flag as problem
return None
def verify_and_execute(main_command,flag_parametres):
# Hardcoded valid main commands list
# Dictionary of Main Command and valid flags
valid_main_commands = {"list workspaces":{},
"set workspace":{},
"current workspace":{},
"create workspace":{},
"get contest":{},
"get problem":{},
"backup current":{}
}
if(main_command not in valid_main_commands):
print("Invalid Command!")
return None
# Now we need to handle both
if(main_command=="list workspaces"):
# Flag validity and type checking
if(check_flags(flag_parametres,valid_main_commands["list workspaces"])):
list_workspaces(flag_parametres,False)
else:
return None
elif(main_command=="set workspace"):
# Flag validity and type checking
if(check_flags(flag_parametres,valid_main_commands["set workspace"])):
set_workspace(flag_parametres)
else:
return None
elif(main_command=="current workspace"):
# Flag validity and type checking
if(check_flags(flag_parametres,valid_main_commands["current workspace"])):
current_workspace(flag_parametres)
else:
return None
elif(main_command=="create workspace"):
# Flag validity and type checking
if(check_flags(flag_parametres,valid_main_commands["create workspace"])):
create_workspace(flag_parametres)
else:
return None
elif(main_command=="get contest"):
# Flag validity and type checking
if(check_flags(flag_parametres,valid_main_commands["get contest"])):
get_contest(flag_parametres)
else:
return None
elif(main_command=="get problem"):
# Flag validity and type checking
if(check_flags(flag_parametres,valid_main_commands["get problem"])):
get_problem(flag_parametres)
else:
return None
elif(main_command=="backup"):
# Flag validity and type checking
if(check_flags(flag_parametres,valid_main_commands["backup"])):
get_problem(flag_parametres)
else:
return None
def parse_command(command):
if(len(command)==0):
return None
main_command = []
for word in command:
if(word[0]!='-'):
main_command.append(word)
else:
break
if(len(main_command)==0 or main_command[-1][0]=="-"):
print("Invalid Command!")
return None
flag_parametres = {}
for idx in range(0,len(command)):
if(command[idx][0]=='-'):
try:
if(command[idx+1][0]=='-'):
flag_parametres[command[idx]] = None
else:
if(command[idx] not in flag_parametres):
flag_parametres[command[idx]] = command[idx+1]
else:
print("Invalid Command!")
return None
except Exception:
flag_parametres[command[idx]]=None
verify_and_execute(" ".join(main_command),flag_parametres)
def start_cmd():
print()
if(current_working_workspace==None):
print(colored("┌──(",'green')+colored('\033[1m'+"!"+'\033[0m','blue')+colored(")-[",'green')+'\033[1m'+"!"+'\033[0m'+colored("]",'green'))
print(colored("└─$ ",'green'),end='')
else:
print(colored("┌──(",'green')+colored('\033[1m'+current_working_workspace[1]+'\033[0m','blue')+colored(")-[",'green')+'\033[1m'+current_working_workspace[2]+'\033[0m'+colored("]",'green'))
print(colored("└─$ ",'green'),end='')
command = input()
command.rstrip()
command.lstrip()
command = command.split()
parse_command(command)
if __name__=="__main__":
print()
print(" ___________________________________________________________________________________________________________")
print("| |")
print("| $$$$$$\\ $$$$$$$\\ $$\\ $$\\ |")
print("| $$ __$$\\ $$ __$$\\ $$$\\ $$$ | |")
print("| $$ / \\__|$$ | $$ | $$$$\\ $$$$ | $$$$$$\\ $$$$$$$\\ $$$$$$\\ $$$$$$\\ $$$$$$\\ $$$$$$\\ |")
print("| $$ | $$$$$$$ | $$\\$$\\$$ $$ | \\____$$\\ $$ __$$\\ \\____$$\\ $$ __$$\\ $$ __$$\\ $$ __$$\\ |")
print("| $$ | $$ ____/ $$ \\$$$ $$ | $$$$$$$ |$$ | $$ | $$$$$$$ |$$ / $$ |$$$$$$$$ |$$ | \\__| |")
print("| $$ | $$\\ $$ | $$ |\\$ /$$ |$$ __$$ |$$ | $$ |$$ __$$ |$$ | $$ |$$ ____|$$ | |")
print("| \\$$$$$$ |$$ | $$ | \\_/ $$ |\\$$$$$$$ |$$ | $$ |\\$$$$$$$ |\\$$$$$$$ |\\$$$$$$$\\ $$ | |")
print("| \\______/ \\__| \\__| \\__| \\_______|\\__| \\__| \\_______| \\____$$ | \\_______|\\__| |")
print("| $$\\ $$ | |")
print("| \\$$$$$$ | |")
print("| \\______/ |")
print("| |")
print("| Version 1.0 |")
print("|___________________________________________________________________________________________________________|")
while(True):
start_cmd()