-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcw2.py
95 lines (87 loc) · 3.35 KB
/
cw2.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
#!/usr/bin/evn python3
# -*- coding: utf-8 -*-
""" Main, the entrance of the program. """
__author__ = "Jiancheng Zhang and Haoran Hong"
import sys
import getopt
import os
import re
from Task2 import t2
from Task3 import t3
from Task4 import t4
from Task5 import t5
from Task7 import t7
def help():
print("###########################################################################################\n")
print("options:")
print("\t-u --userID: input the userID that need to query (Optional)")
print("\t-d --docID: input the documentID that need to query")
print("\t-t --taskID: input one of the task that want to run, [2a, 2b, 3a, 3b, 4, 5d, 6, 7]")
print("\t-f --filename: input the JSON file that contains the data")
print("\t-h --help: ask for help")
print("\t-q --quit: exit immediately\n")
print("###########################################################################################")
if __name__ == "__main__":
try:
opts, args = getopt.getopt(sys.argv[1:], 'u:d:t:f:hq',
['userID=', 'docID=', 'taskID=', 'filename=', 'help', 'quit'])
fileName = ""
userID = 0
docID = 0
task_id = 0
for opt_name, opt_value in opts:
if opt_name in ('-u', '--userID'):
userID = opt_value
elif opt_name in ('-d', '--docID'):
docID = opt_value
elif opt_name in ('-t', '--taskID'):
if opt_value in ('2a', '2b', '3a', '3b', '4', '5d', '6', '7'):
task_id = opt_value
else:
print("Unknown task ID", opt_value)
sys.exit()
elif opt_name in ('-f', '--filename'):
file_exists = os.path.isfile(opt_value)
if file_exists:
pattern = re.compile(r'\.json\Z')
t = pattern.search(opt_value)
if t is not None:
fileName = os.path.abspath(opt_value)
else:
print("Wrong file type:", opt_value, "not a json file.")
sys.exit()
else:
print("Cannot find file", opt_value, "because it does not exist.")
sys.exit()
elif opt_name in ('-h', '--help'):
help()
sys.exit()
elif opt_name in ('-q', '--quit'):
sys.exit()
# fileName and task id must have
if fileName == "":
print("No file input!")
sys.exit()
if task_id == 0:
print("No given task ID!")
sys.exit()
if task_id == "2a":
t2.view_by_country(docID, fileName)
elif task_id == "2b":
t2.view_by_continent(docID, fileName)
elif task_id == "3a":
t3.view_by_browser_all(fileName)
elif task_id == "3b":
t3.view_by_browser(fileName)
elif task_id == "4":
t4.top_10_reader(fileName)
elif task_id == "5d":
t5.alsolikes_sorted(userID, docID, fileName)
elif task_id == "6":
t5.generate_graph(userID, docID, fileName)
elif task_id == "7":
t7.GUI(fileName, userID, docID)
except getopt.GetoptError as error:
print(error, "\n")
help()
sys.exit(1)