1 #!/usr/bin/env python3 |
1 #!/usr/bin/env python3 |
2 from flask import Flask, render_template, redirect, request |
2 import flask |
3 from ldcheck import load_config, load_colours, find_ldconfig_ldr_paths |
3 import testsuite |
4 from parse import read_ldraw |
4 import config |
5 from testsuite import load_tests, check_model, problem_text, all_problem_types |
5 import parse |
|
6 import colours |
6 |
7 |
7 app = Flask('LDCheck') |
8 app = flask.Flask('LDCheck') |
8 |
9 |
9 @app.route('/', methods = ['GET', 'POST']) |
10 @app.route('/', methods = ['GET', 'POST']) |
10 def web_front(): |
11 def web_front(): |
11 test_suite = load_tests() |
12 test_suite = testsuite.load_tests() |
|
13 request = flask.request |
12 if request.method == 'POST': |
14 if request.method == 'POST': |
13 # check if the post request has the file part |
15 # check if the post request has the file part |
14 if 'file' not in request.files or not request.files['file'].filename: |
16 if 'file' not in request.files or not request.files['file'].filename: |
15 return redirect(request.url) |
17 return flask.redirect(request.url) |
16 file = request.files['file'] |
18 file = request.files['file'] |
17 filename = file.filename |
19 filename = file.filename |
18 config = load_config('ldcheck.cfg') |
20 config_object = config.load_config('ldcheck.cfg') |
19 for ldconfig_ldr_path in find_ldconfig_ldr_paths(config): |
21 for ldconfig_ldr_path in config.find_ldconfig_ldr_paths(config_object): |
20 with ldconfig_ldr_path.open() as ldconfig_ldr: |
22 with ldconfig_ldr_path.open() as ldconfig_ldr: |
21 load_colours(ldconfig_ldr) |
23 colours.load_colours(ldconfig_ldr) |
22 model = read_ldraw( |
24 model = parse.read_ldraw( |
23 file.stream, |
25 file.stream, |
24 name = filename, |
26 name = filename, |
25 ldraw_directories = config['libraries'], |
27 ldraw_directories = config_object['libraries'], |
26 ) |
28 ) |
27 report = check_model(model, test_suite) |
29 report = testsuite.check_model(model, test_suite) |
28 |
30 |
29 # Amend human-readable messages into the report |
31 # Amend human-readable messages into the report |
30 for problem in report['problems']: |
32 for problem in report['problems']: |
31 object = model.body[problem.body_index] |
33 object = model.body[problem.body_index] |
32 problem.message_str = problem_text(problem, test_suite) |
34 problem.message_str = str(problem) |
33 problem.ldraw_code = object.textual_representation() |
35 problem.ldraw_code = object.textual_representation() |
34 return render_template('webfront.html', |
36 return flask.render_template('webfront.html', |
35 report = report, |
37 report = report, |
36 name = filename, |
38 name = filename, |
37 problem_types = all_problem_types(test_suite) |
39 problem_types = testsuite.all_problem_types(test_suite) |
38 ) |
40 ) |
39 else: |
41 else: |
40 test_suite = load_tests() |
42 return flask.render_template('webfront.html', |
41 return render_template('webfront.html', |
|
42 report = None, |
43 report = None, |
43 name = None, |
44 name = None, |
44 problem_types = all_problem_types(test_suite) |
45 problem_types = testsuite.all_problem_types(test_suite) |
45 ) |
46 ) |
46 |
47 |
47 @app.route('/static/<path:path>') |
48 @app.route('/static/<path:path>') |
48 def static_file(path): |
49 def static_file(path): |
49 from flask import send_from_directory |
50 from flask import send_from_directory |