1 #!/usr/bin/env python3 |
1 #!/usr/bin/env python3 |
2 from flask import Flask, render_template, redirect, request |
2 from flask import Flask, render_template, redirect, request |
3 from ldcheck import load_config, load_colours, find_ldconfig_ldr_paths |
3 from ldcheck import appname, version, version_string |
4 from parse import read_ldraw |
4 from parse import read_ldraw |
5 from testsuite import load_tests, check_model, problem_text, all_problem_types |
5 from testsuite import load_tests, check_model, problem_text, all_problem_types |
6 |
6 |
7 app = Flask('LDCheck') |
7 app = Flask(appname) |
|
8 |
|
9 from ldcheck import LDrawContext |
|
10 try: |
|
11 context = LDrawContext() |
|
12 except RuntimeError as error: |
|
13 from sys import stderr, exit |
|
14 print('error:', str(error), file = stderr) |
|
15 exit(1) |
|
16 |
|
17 def format_report_html(report, model, test_suite): |
|
18 messages = [] |
|
19 for problem in report['problems']: |
|
20 ldraw_code = model.body[problem.body_index].textual_representation() |
|
21 message = str.format( |
|
22 '<li class="{problem_type}">{model_name}:{line_number}:' |
|
23 '{problem_type}: {message}<br />{ldraw_code}</li>', |
|
24 model_name = model.name, |
|
25 line_number = problem.line_number, |
|
26 problem_type = problem.severity, |
|
27 message = problem_text(problem, test_suite), |
|
28 ldraw_code = ldraw_code, |
|
29 ) |
|
30 messages.append(message) |
|
31 return '\n'.join(messages) |
8 |
32 |
9 @app.route('/', methods = ['GET', 'POST']) |
33 @app.route('/', methods = ['GET', 'POST']) |
10 def web_front(): |
34 def web_front(): |
11 test_suite = load_tests() |
35 test_suite = load_tests() |
|
36 common_args = { |
|
37 'appname': appname, |
|
38 'version': version_string, |
|
39 'is_debug': max(version) == 9999, |
|
40 } |
12 if request.method == 'POST': |
41 if request.method == 'POST': |
13 # check if the post request has the file part |
42 # check if the post request has the file part |
14 if 'file' not in request.files or not request.files['file'].filename: |
43 if 'file' not in request.files or not request.files['file'].filename: |
15 return redirect(request.url) |
44 return redirect(request.url) |
16 file = request.files['file'] |
45 file = request.files['file'] |
17 filename = file.filename |
46 filename = file.filename |
18 config = load_config('ldcheck.cfg') |
47 model = read_ldraw(file.stream, name = filename, context = context) |
19 for ldconfig_ldr_path in find_ldconfig_ldr_paths(config): |
|
20 with ldconfig_ldr_path.open() as ldconfig_ldr: |
|
21 load_colours(ldconfig_ldr) |
|
22 model = read_ldraw( |
|
23 file.stream, |
|
24 name = filename, |
|
25 ldraw_directories = config['libraries'], |
|
26 ) |
|
27 report = check_model(model, test_suite) |
48 report = check_model(model, test_suite) |
28 |
49 |
29 # Amend human-readable messages into the report |
50 # Amend human-readable messages into the report |
30 for problem in report['problems']: |
51 for problem in report['problems']: |
31 object = model.body[problem.body_index] |
52 object = model.body[problem.body_index] |
32 problem.message_str = problem_text(problem, test_suite) |
53 problem.message_str = problem_text(problem, test_suite) |
33 problem.ldraw_code = object.textual_representation() |
54 problem.ldraw_code = object.textual_representation() |
34 return render_template('webfront.html', |
55 return render_template('webfront.html', |
35 report = report, |
56 report = report, |
36 name = filename, |
57 name = filename, |
37 problem_types = all_problem_types(test_suite) |
58 problem_types = all_problem_types(test_suite), |
|
59 **common_args, |
38 ) |
60 ) |
39 else: |
61 else: |
40 test_suite = load_tests() |
62 test_suite = load_tests() |
41 return render_template('webfront.html', |
63 return render_template('webfront.html', |
42 report = None, |
64 report = None, |
43 name = None, |
65 name = None, |
44 problem_types = all_problem_types(test_suite) |
66 problem_types = all_problem_types(test_suite), |
|
67 **common_args, |
45 ) |
68 ) |
46 |
69 |
47 @app.route('/static/<path:path>') |
70 @app.route('/static/<path:path>') |
48 def static_file(path): |
71 def static_file(path): |
49 from flask import send_from_directory |
72 from flask import send_from_directory |