Fri, 18 Sep 2020 21:01:49 +0300
added license tests
32
75f44d3063da
Reworked web front, problems are now sorted by category as well as line number
Santeri Piippo
parents:
29
diff
changeset
|
1 | #!/usr/bin/env python3 |
29 | 2 | from flask import Flask, render_template, redirect, request |
100
62759e5c4554
add some basic versioning
Teemu Piippo <teemu@hecknology.net>
parents:
99
diff
changeset
|
3 | from ldcheck import appname, version, version_string |
122 | 4 | from ldcheck import load_config, find_ldconfig_ldr_paths |
57 | 5 | from parse import read_ldraw |
63
8949af6a4279
added the list of issues onto the web frontend
Teemu Piippo <teemu@hecknology.net>
parents:
62
diff
changeset
|
6 | from testsuite import load_tests, check_model, problem_text, all_problem_types |
29 | 7 | |
100
62759e5c4554
add some basic versioning
Teemu Piippo <teemu@hecknology.net>
parents:
99
diff
changeset
|
8 | app = Flask(appname) |
29 | 9 | |
99
01941a811b5a
move format_report_html to webfront.py
Teemu Piippo <teemu@hecknology.net>
parents:
63
diff
changeset
|
10 | def format_report_html(report, model, test_suite): |
01941a811b5a
move format_report_html to webfront.py
Teemu Piippo <teemu@hecknology.net>
parents:
63
diff
changeset
|
11 | messages = [] |
01941a811b5a
move format_report_html to webfront.py
Teemu Piippo <teemu@hecknology.net>
parents:
63
diff
changeset
|
12 | for problem in report['problems']: |
01941a811b5a
move format_report_html to webfront.py
Teemu Piippo <teemu@hecknology.net>
parents:
63
diff
changeset
|
13 | ldraw_code = model.body[problem.body_index].textual_representation() |
01941a811b5a
move format_report_html to webfront.py
Teemu Piippo <teemu@hecknology.net>
parents:
63
diff
changeset
|
14 | message = str.format( |
01941a811b5a
move format_report_html to webfront.py
Teemu Piippo <teemu@hecknology.net>
parents:
63
diff
changeset
|
15 | '<li class="{problem_type}">{model_name}:{line_number}:' |
01941a811b5a
move format_report_html to webfront.py
Teemu Piippo <teemu@hecknology.net>
parents:
63
diff
changeset
|
16 | '{problem_type}: {message}<br />{ldraw_code}</li>', |
01941a811b5a
move format_report_html to webfront.py
Teemu Piippo <teemu@hecknology.net>
parents:
63
diff
changeset
|
17 | model_name = model.name, |
01941a811b5a
move format_report_html to webfront.py
Teemu Piippo <teemu@hecknology.net>
parents:
63
diff
changeset
|
18 | line_number = problem.line_number, |
01941a811b5a
move format_report_html to webfront.py
Teemu Piippo <teemu@hecknology.net>
parents:
63
diff
changeset
|
19 | problem_type = problem.severity, |
01941a811b5a
move format_report_html to webfront.py
Teemu Piippo <teemu@hecknology.net>
parents:
63
diff
changeset
|
20 | message = problem_text(problem, test_suite), |
01941a811b5a
move format_report_html to webfront.py
Teemu Piippo <teemu@hecknology.net>
parents:
63
diff
changeset
|
21 | ldraw_code = ldraw_code, |
01941a811b5a
move format_report_html to webfront.py
Teemu Piippo <teemu@hecknology.net>
parents:
63
diff
changeset
|
22 | ) |
01941a811b5a
move format_report_html to webfront.py
Teemu Piippo <teemu@hecknology.net>
parents:
63
diff
changeset
|
23 | messages.append(message) |
01941a811b5a
move format_report_html to webfront.py
Teemu Piippo <teemu@hecknology.net>
parents:
63
diff
changeset
|
24 | return '\n'.join(messages) |
01941a811b5a
move format_report_html to webfront.py
Teemu Piippo <teemu@hecknology.net>
parents:
63
diff
changeset
|
25 | |
29 | 26 | @app.route('/', methods = ['GET', 'POST']) |
27 | def web_front(): | |
63
8949af6a4279
added the list of issues onto the web frontend
Teemu Piippo <teemu@hecknology.net>
parents:
62
diff
changeset
|
28 | test_suite = load_tests() |
100
62759e5c4554
add some basic versioning
Teemu Piippo <teemu@hecknology.net>
parents:
99
diff
changeset
|
29 | common_args = { |
62759e5c4554
add some basic versioning
Teemu Piippo <teemu@hecknology.net>
parents:
99
diff
changeset
|
30 | 'appname': appname, |
62759e5c4554
add some basic versioning
Teemu Piippo <teemu@hecknology.net>
parents:
99
diff
changeset
|
31 | 'version': version_string, |
62759e5c4554
add some basic versioning
Teemu Piippo <teemu@hecknology.net>
parents:
99
diff
changeset
|
32 | 'is_debug': max(version) == 9999, |
62759e5c4554
add some basic versioning
Teemu Piippo <teemu@hecknology.net>
parents:
99
diff
changeset
|
33 | } |
29 | 34 | if request.method == 'POST': |
35 | # check if the post request has the file part | |
36 | if 'file' not in request.files or not request.files['file'].filename: | |
37 | return redirect(request.url) | |
38 | file = request.files['file'] | |
61
15c95d3fcfd8
show the filename of the processed file in the report
Teemu Piippo <teemu@hecknology.net>
parents:
57
diff
changeset
|
39 | filename = file.filename |
122 | 40 | config = load_config() |
29 | 41 | model = read_ldraw( |
42 | file.stream, | |
61
15c95d3fcfd8
show the filename of the processed file in the report
Teemu Piippo <teemu@hecknology.net>
parents:
57
diff
changeset
|
43 | name = filename, |
57 | 44 | ldraw_directories = config['libraries'], |
29 | 45 | ) |
46 | report = check_model(model, test_suite) | |
32
75f44d3063da
Reworked web front, problems are now sorted by category as well as line number
Santeri Piippo
parents:
29
diff
changeset
|
47 | |
75f44d3063da
Reworked web front, problems are now sorted by category as well as line number
Santeri Piippo
parents:
29
diff
changeset
|
48 | # Amend human-readable messages into the report |
75f44d3063da
Reworked web front, problems are now sorted by category as well as line number
Santeri Piippo
parents:
29
diff
changeset
|
49 | for problem in report['problems']: |
62
f0a6bf48b05e
Problem reporting revamp, program is now aware of its problem types
Teemu Piippo <teemu@hecknology.net>
parents:
61
diff
changeset
|
50 | object = model.body[problem.body_index] |
f0a6bf48b05e
Problem reporting revamp, program is now aware of its problem types
Teemu Piippo <teemu@hecknology.net>
parents:
61
diff
changeset
|
51 | problem.message_str = problem_text(problem, test_suite) |
f0a6bf48b05e
Problem reporting revamp, program is now aware of its problem types
Teemu Piippo <teemu@hecknology.net>
parents:
61
diff
changeset
|
52 | problem.ldraw_code = object.textual_representation() |
63
8949af6a4279
added the list of issues onto the web frontend
Teemu Piippo <teemu@hecknology.net>
parents:
62
diff
changeset
|
53 | return render_template('webfront.html', |
8949af6a4279
added the list of issues onto the web frontend
Teemu Piippo <teemu@hecknology.net>
parents:
62
diff
changeset
|
54 | report = report, |
8949af6a4279
added the list of issues onto the web frontend
Teemu Piippo <teemu@hecknology.net>
parents:
62
diff
changeset
|
55 | name = filename, |
100
62759e5c4554
add some basic versioning
Teemu Piippo <teemu@hecknology.net>
parents:
99
diff
changeset
|
56 | problem_types = all_problem_types(test_suite), |
62759e5c4554
add some basic versioning
Teemu Piippo <teemu@hecknology.net>
parents:
99
diff
changeset
|
57 | **common_args, |
63
8949af6a4279
added the list of issues onto the web frontend
Teemu Piippo <teemu@hecknology.net>
parents:
62
diff
changeset
|
58 | ) |
32
75f44d3063da
Reworked web front, problems are now sorted by category as well as line number
Santeri Piippo
parents:
29
diff
changeset
|
59 | else: |
63
8949af6a4279
added the list of issues onto the web frontend
Teemu Piippo <teemu@hecknology.net>
parents:
62
diff
changeset
|
60 | test_suite = load_tests() |
8949af6a4279
added the list of issues onto the web frontend
Teemu Piippo <teemu@hecknology.net>
parents:
62
diff
changeset
|
61 | return render_template('webfront.html', |
8949af6a4279
added the list of issues onto the web frontend
Teemu Piippo <teemu@hecknology.net>
parents:
62
diff
changeset
|
62 | report = None, |
8949af6a4279
added the list of issues onto the web frontend
Teemu Piippo <teemu@hecknology.net>
parents:
62
diff
changeset
|
63 | name = None, |
100
62759e5c4554
add some basic versioning
Teemu Piippo <teemu@hecknology.net>
parents:
99
diff
changeset
|
64 | problem_types = all_problem_types(test_suite), |
62759e5c4554
add some basic versioning
Teemu Piippo <teemu@hecknology.net>
parents:
99
diff
changeset
|
65 | **common_args, |
63
8949af6a4279
added the list of issues onto the web frontend
Teemu Piippo <teemu@hecknology.net>
parents:
62
diff
changeset
|
66 | ) |
29 | 67 | |
32
75f44d3063da
Reworked web front, problems are now sorted by category as well as line number
Santeri Piippo
parents:
29
diff
changeset
|
68 | @app.route('/static/<path:path>') |
75f44d3063da
Reworked web front, problems are now sorted by category as well as line number
Santeri Piippo
parents:
29
diff
changeset
|
69 | def static_file(path): |
75f44d3063da
Reworked web front, problems are now sorted by category as well as line number
Santeri Piippo
parents:
29
diff
changeset
|
70 | from flask import send_from_directory |
75f44d3063da
Reworked web front, problems are now sorted by category as well as line number
Santeri Piippo
parents:
29
diff
changeset
|
71 | from os import path |
75f44d3063da
Reworked web front, problems are now sorted by category as well as line number
Santeri Piippo
parents:
29
diff
changeset
|
72 | return send_from_directory(path.join('static', path)) |
75f44d3063da
Reworked web front, problems are now sorted by category as well as line number
Santeri Piippo
parents:
29
diff
changeset
|
73 | |
75f44d3063da
Reworked web front, problems are now sorted by category as well as line number
Santeri Piippo
parents:
29
diff
changeset
|
74 | if __name__ == '__main__': |
75f44d3063da
Reworked web front, problems are now sorted by category as well as line number
Santeri Piippo
parents:
29
diff
changeset
|
75 | from argparse import ArgumentParser |
75f44d3063da
Reworked web front, problems are now sorted by category as well as line number
Santeri Piippo
parents:
29
diff
changeset
|
76 | parser = ArgumentParser() |
75f44d3063da
Reworked web front, problems are now sorted by category as well as line number
Santeri Piippo
parents:
29
diff
changeset
|
77 | parser.add_argument('-p', '--port', type = int, default = 5000) |
75f44d3063da
Reworked web front, problems are now sorted by category as well as line number
Santeri Piippo
parents:
29
diff
changeset
|
78 | parser.add_argument('-d', '--debug', action = 'store_true') |
75f44d3063da
Reworked web front, problems are now sorted by category as well as line number
Santeri Piippo
parents:
29
diff
changeset
|
79 | args = parser.parse_args() |
75f44d3063da
Reworked web front, problems are now sorted by category as well as line number
Santeri Piippo
parents:
29
diff
changeset
|
80 | app.run(port = args.port, debug = args.debug) |