webfront.py

changeset 32
75f44d3063da
parent 29
db6ca177c6c4
child 41
4d87bc126368
child 57
c147116768f4
equal deleted inserted replaced
31:02e7e1d73ebb 32:75f44d3063da
1 #!/usr/bin/env python3
1 from flask import Flask, render_template, redirect, request 2 from flask import Flask, render_template, redirect, request
2 from ldcheck import load_config, load_colours, find_ldconfig_ldr_paths 3 from ldcheck import load_config, load_colours, find_ldconfig_ldr_paths
3 from ldcheck import read_ldraw 4 from ldcheck import read_ldraw
4 from testsuite import load_tests, check_model, format_report_html 5 from testsuite import load_tests, check_model, problem_text
5 6
6 app = Flask('LDCheck') 7 app = Flask('LDCheck')
7 8
8 @app.route('/', methods = ['GET', 'POST']) 9 @app.route('/', methods = ['GET', 'POST'])
9 def web_front(): 10 def web_front():
10 if request.method == 'POST': 11 if request.method == 'POST':
11 # check if the post request has the file part 12 # check if the post request has the file part
12 if 'file' not in request.files or not request.files['file'].filename: 13 if 'file' not in request.files or not request.files['file'].filename:
13 return redirect(request.url) 14 return redirect(request.url)
14 file = request.files['file'] 15 file = request.files['file']
15 print(type(file))
16 config = load_config('ldcheck.cfg') 16 config = load_config('ldcheck.cfg')
17 for ldconfig_ldr_path in find_ldconfig_ldr_paths(config): 17 for ldconfig_ldr_path in find_ldconfig_ldr_paths(config):
18 with ldconfig_ldr_path.open() as ldconfig_ldr: 18 with ldconfig_ldr_path.open() as ldconfig_ldr:
19 load_colours(ldconfig_ldr) 19 load_colours(ldconfig_ldr)
20 model = read_ldraw( 20 model = read_ldraw(
22 name = file.filename, 22 name = file.filename,
23 config = config, 23 config = config,
24 ) 24 )
25 test_suite = load_tests() 25 test_suite = load_tests()
26 report = check_model(model, test_suite) 26 report = check_model(model, test_suite)
27 return str.format(
28 '<!doctype html><html><body><ul>{report}</ul></body></html>',
29 report = format_report_html(report, model, test_suite)
30 )
31 return '''
32 <!doctype html>
33 <title>Upload new File</title>
34 <h1>Upload new File</h1>
35 <form method=post enctype=multipart/form-data>
36 <p><input type=file name=file>
37 <input type=submit value=Upload>
38 </form>
39 '''
40 27
41 app.run() 28 # Amend human-readable messages into the report
29 for problem in report['problems']:
30 object = model.body[problem['body-index']]
31 problem['message'] = problem_text(problem, test_suite)
32 problem['ldraw-code'] = object.textual_representation()
33 else:
34 report = None
35 return render_template('webfront.html',
36 report = report,
37 )
38
39 @app.route('/static/<path:path>')
40 def static_file(path):
41 from flask import send_from_directory
42 from os import path
43 return send_from_directory(path.join('static', path))
44
45 if __name__ == '__main__':
46 from argparse import ArgumentParser
47 parser = ArgumentParser()
48 parser.add_argument('-p', '--port', type = int, default = 5000)
49 parser.add_argument('-d', '--debug', action = 'store_true')
50 args = parser.parse_args()
51 app.run(port = args.port, debug = args.debug)

mercurial