-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathrun.py
212 lines (172 loc) · 8.02 KB
/
run.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
import os
import sys
import json
import logging
from flask import Flask, render_template, request, g, redirect, url_for
from fm_characterization import FMCharacterization
from fm_characterization import models_info
from fm_characterization.fm_utils import read_fm_file
from fm_characterization.location_stats import LocationStats
import config
STATIC_DIR = 'web'
EXAMPLE_MODELS_DIR = 'fm_models'
app = Flask(__name__,
static_url_path='',
static_folder=STATIC_DIR,
template_folder=STATIC_DIR)
app.config.from_object(config)
# Add version to the global context before each request
@app.before_request
def add_version_to_g():
g.version = app.config['VERSION']
# Automatically add the version parameter in each generated URL
@app.url_defaults
def add_version_to_url(endpoint, values):
if 'v' not in values:
values['v'] = g.version
# Redirect automatically to the URL with the version parameter if not present
@app.before_request
def enforce_version_in_url():
if 'v' not in request.args:
return redirect(url_for(request.endpoint, **request.view_args, v=g.version))
# Get example models
EXAMPLE_MODELS = {m[models_info.NAME]: m for m in models_info.MODELS}
LOCATION_STATS_FILE = 'location_stats.json'
LOCATION_STATS = LocationStats(LOCATION_STATS_FILE)
@app.route('/', methods=['GET', 'POST'])
def index():
version = request.args.get('v')
if version != g.version:
return "Incorrect version", 404
data = {}
data['uploadFM'] = True
data['models'] = EXAMPLE_MODELS
data['usage_stats'] = {'total_fact_labels': LOCATION_STATS.get_total_visits(),
'countries': LOCATION_STATS.get_countries_stats()}
if request.method == 'GET':
return render_template('index.html', data=data)
if request.method == 'POST':
light_fact_label = 'lightFactLabel' in request.form
logging.warning(f'light_fact_label: {light_fact_label}')
fm_file = request.files['inputFM']
fm_name = request.form['inputExample']
name = None
description = None
author = None
year = None
keywords = None
reference = None
domain = None
if not fm_file and not fm_name:
# The file is required and this is controlled in the front-end.
data['file_error'] = 'Please upload a feature model or select one from the examples.'
return render_template('index.html', data=data)
if fm_file:
filename = fm_file.filename
fm_file.save(filename)
elif fm_name in EXAMPLE_MODELS.keys():
filename = os.path.join(STATIC_DIR, EXAMPLE_MODELS_DIR, EXAMPLE_MODELS[fm_name][models_info.FILENAME])
name = EXAMPLE_MODELS[fm_name][models_info.NAME]
description = EXAMPLE_MODELS[fm_name][models_info.DESCRIPTION]
author = EXAMPLE_MODELS[fm_name][models_info.AUTHOR]
reference = EXAMPLE_MODELS[fm_name][models_info.REFERENCE]
keywords = EXAMPLE_MODELS[fm_name][models_info.KEYWORDS]
keywords = ', '.join(keywords)
domain = EXAMPLE_MODELS[fm_name][models_info.DOMAIN]
year = EXAMPLE_MODELS[fm_name][models_info.YEAR]
data['fm_example'] = os.path.join(EXAMPLE_MODELS_DIR, EXAMPLE_MODELS[fm_name][models_info.FILENAME])
else:
data['file_error'] = 'Please upload a feature model or select one from the examples.'
return render_template('index.html', data=data)
if request.form['inputName']:
name = request.form['inputName']
if request.form['inputDescription']:
description = request.form['inputDescription']
description = description.replace(os.linesep, ' ')
if request.form['inputAuthor']:
author = request.form['inputAuthor']
if request.form['inputReference']:
reference = request.form['inputReference']
if request.form['inputKeywords']:
keywords = request.form['inputKeywords']
if request.form['inputDomain']:
domain = request.form['inputDomain']
if request.form['inputYear']:
year = request.form['inputYear']
try:
# Read the feature model
fm = read_fm_file(filename)
if fm is None:
data['file_error'] = 'Feature model format not supported.'
return render_template('index.html', data=data)
if not name:
name = os.path.splitext(os.path.basename(filename))[0]
characterization = FMCharacterization(fm, light_fact_label)
characterization.metadata.name = name
characterization.metadata.description = description
characterization.metadata.author = author
characterization.metadata.year = year
characterization.metadata.tags = keywords
characterization.metadata.reference = reference
characterization.metadata.domains = domain
#json_characterization = interfaces.to_json(fm_characterization, FM_FACT_JSON_FILE)
json_characterization = characterization.to_json()
json_str_characterization = characterization.to_json_str()
str_characterization = str(characterization)
data['fm_facts'] = json_characterization
data['fm_characterization_json_str'] = json_str_characterization
data['fm_characterization_str'] = str_characterization
characterization.clean()
except Exception as e:
data = None
print(e)
raise e
if os.path.exists(filename) and filename == fm_file.filename:
os.remove(filename)
location = LOCATION_STATS.get_location_from_ip(request.remote_addr)
LOCATION_STATS.add_location(location['continent_code'], location['country_name'], location['city'])
data['usage_stats'] = {'total_fact_labels': LOCATION_STATS.get_total_visits(),
'countries': LOCATION_STATS.get_countries_stats()}
return render_template('index.html', data=data)
@app.route('/uploadJSON', methods=['GET', 'POST'])
def uploadJSON():
version = request.args.get('v')
if version != g.version:
return "Incorrect version", 404
data = {}
data['uploadJSON'] = True
data['models'] = EXAMPLE_MODELS
data['usage_stats'] = {'total_fact_labels': LOCATION_STATS.get_total_visits(),
'countries': LOCATION_STATS.get_countries_stats()}
if request.method == 'GET':
return render_template('index.html', data=data)
if request.method == 'POST':
json_file = request.files['inputJSON']
if not json_file:
# The file is required and this is controlled in the front-end.
data['file_error'] = 'Please upload a JSON file.'
return render_template('index.html', data=data)
filename = json_file.filename
json_file.save(filename)
try:
# Read the json
json_characterization = json.load(open(filename))
if json_characterization is None:
data['file_error'] = 'JSON format not supported.'
return render_template('index.html', data=data)
data['fm_facts'] = json_characterization
except Exception as e:
data = None
print(e)
raise e
if os.path.exists(filename) and filename == json_file.filename:
os.remove(filename)
location = LOCATION_STATS.get_location_from_ip(request.remote_addr)
LOCATION_STATS.add_location(location['continent_code'], location['country_name'], location['city'])
data['usage_stats'] = {'total_fact_labels': LOCATION_STATS.get_total_visits(),
'countries': LOCATION_STATS.get_countries_stats()}
return render_template('index.html', data=data)
if __name__ == '__main__':
sys.set_int_max_str_digits(0)
#logging.basicConfig(filename='app.log', level=logging.INFO)
app.run(host='0.0.0.0', debug=True)