-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.py
96 lines (78 loc) · 3.49 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
#!flask/bin/python
import os
from flask import Flask, request, jsonify
from flask_cors import CORS
from validator.main_validator import PGSMetadataValidator
app = Flask(__name__, static_url_path='/')
# CORS settings
CORS(app)
cors = CORS(app, resources={r"/": {"origins": "*"}})
if not os.getenv('GAE_APPLICATION', None):
app_settings = os.path.join('./', 'app.yaml')
if os.path.exists(app_settings):
import yaml
with open(app_settings) as secrets_file:
secrets = yaml.load(secrets_file, Loader=yaml.FullLoader)
for keyword in secrets['env_variables']:
os.environ[keyword] = secrets['env_variables'][keyword]
else:
print("Error: missing app.yaml file")
exit(1)
@app.route("/robots.txt")
def robots_dot_txt():
return "User-agent: *\nDisallow: /"
@app.route('/', methods=['GET'])
def home():
return "<h1>PGS Catalog metadata validator</h1><p>This service validates the Metadata files schema and content.</p>"
@app.route('/validate', methods=['POST'])
def post_file():
response = {}
post_json = request.get_json()
#print("post_json: "+str(post_json))
filename = post_json['filename']
# Check file extension
expected_file_extension = 'xlsx'
filename_only = os.path.basename(filename)
extension = filename_only.split('.')[-1]
if extension != expected_file_extension:
error_msg = { 'message': f'The expected file extension is [.{expected_file_extension}] but the given file name is "{filename_only}".'}
response = {'status': 'failed', 'error': {} }
response['error']['General'] = [ error_msg ]
return jsonify(response)
metadata_validator = PGSMetadataValidator(filename, 1)
loaded_spreadsheets = metadata_validator.parse_spreadsheets()
if loaded_spreadsheets:
metadata_validator.parse_publication()
metadata_validator.parse_scores()
metadata_validator.parse_cohorts()
metadata_validator.parse_performances()
metadata_validator.parse_samples()
metadata_validator.post_parsing_checks()
status = 'success'
if metadata_validator.report['error']:
status = 'failed'
response['error'] = {}
error_report = metadata_validator.report['error']
for error_spreadsheet in error_report:
response['error'][error_spreadsheet] = []
for error_msg in error_report[error_spreadsheet]:
error_entry = { 'message': error_msg }
if error_report[error_spreadsheet][error_msg][0] != None:
error_entry['lines'] = error_report[error_spreadsheet][error_msg]
response['error'][error_spreadsheet].append(error_entry)
if metadata_validator.report['warning']:
response['warning'] = {}
warning_report = metadata_validator.report['warning']
for warning_spreadsheet in warning_report:
response['warning'][warning_spreadsheet] = []
for warning_msg in warning_report[warning_spreadsheet]:
warning_entry = { 'message': warning_msg }
if warning_report[warning_spreadsheet][warning_msg][0] != None:
warning_entry['lines'] = warning_report[warning_spreadsheet][warning_msg]
response['warning'][warning_spreadsheet].append(warning_entry)
response['status'] = status
#os.remove(metadata_filename)
return jsonify(response)
if __name__ == '__main__':
app.run(debug=False)#, port=5000)
#app.run(debug=True, port=5000)