-
Notifications
You must be signed in to change notification settings - Fork 0
/
json_parser.py
152 lines (124 loc) · 4.64 KB
/
json_parser.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
'''
The module creates UI using flask.
'''
from json.decoder import JSONDecodeError
from flask import Flask, render_template, request
from api import api_file_retriever
from json_parser_program import find_value
application = Flask(__name__)
JSON_FILE = None
OPTION = None
key_value = None
@application.route('/')
def index():
'''
Returns main template of the site.
'''
return render_template('index.html')
@application.route('/submit', methods = ['POST'])
def submit():
'''
Returns a site for navigation
in the file.
'''
global OPTION, JSON_FILE
if not request.form.get('submission_user_name') or \
not request.form.get('submission_resource_url') or \
not request.form.get('option'):
return render_template('error.html')
json_file_name = request.form.get('submission_user_name')
json_resource_url_appendix = request.form.get('submission_resource_url')
try:
OPTION = bool(int(request.form.get('option')) - 1)
if OPTION not in (1, 0):
raise render_template('error.html')
JSON_FILE = api_file_retriever(json_resource_url_appendix, json_file_name)
if 'errors' in JSON_FILE:
raise render_template('error.html')
except (JSONDecodeError, Exception, ValueError):
return render_template('error.html')
return render_template('navigation.html',
file = JSON_FILE,
option = OPTION,
keys = JSON_FILE.keys(),
message = 1
)
@application.route('/submit_key', methods = ['POST'])
def submit_key():
'''
Returns a site corresponding to a button.
'''
global OPTION, JSON_FILE
if not request.form.get('required_key'):
return render_template('error.html')
key = request.form.get('required_key')
try:
result, requested_value = find_value(JSON_FILE, key)
if result:
return render_template('result_option_1.html',
result = requested_value,
option = True)
return render_template('failure_option_1.html',
result = requested_value,
option = False)
except KeyError:
return render_template('error.html')
@application.route('/submit_option_2', methods = ['POST'])
def submit_option_2():
'''
Returns a site corresponding to a button.
'''
global JSON_FILE, key_value
used_key = None
if request.form['key_button'] == 'Show':
key_value = JSON_FILE
return render_template('show_container.html', container = JSON_FILE)
if isinstance(JSON_FILE, dict):
for key in JSON_FILE:
if request.form['key_button'] == key:
used_key = key
key_value = JSON_FILE[used_key]
else:
for key in range(len(JSON_FILE)):
if request.form['key_button'] == str(key):
used_key = key
key_value = JSON_FILE[int(used_key)]
if isinstance(key_value, dict):
JSON_FILE = key_value
return render_template('navigation.html',
keys = key_value.keys(),
message=True,
option = True)
if isinstance(key_value, list):
JSON_FILE = key_value
if len(key_value) == 0:
return render_template('result_option_2.html', result = [])
return render_template('navigation.html',
keys = list(range(len(key_value))),
message=False,
option = True)
return render_template('result_option_2.html', result = key_value)
@application.route('/get_back', methods = ['POST'])
def get_back():
global key_value
'''
Returns a main site template.
'''
if request.form['back_'] == 'Starting page':
return render_template('index.html')
if isinstance(key_value, dict):
JSON_FILE = key_value
return render_template('navigation.html',
keys = key_value.keys(),
message=True,
option = True)
if isinstance(key_value, list):
JSON_FILE = key_value
if len(key_value) == 0:
return render_template('result_option_2.html', result = [])
return render_template('navigation.html',
keys = list(range(len(key_value))),
message=False,
option = True)
if __name__ == '__main__':
application.run()