Fri, 18 Sep 2020 20:22:22 +0300
added unit testing
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 |
29 | 4 | from ldcheck import load_config, load_colours, 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 |
29 | 40 | config = load_config('ldcheck.cfg') |
41 | for ldconfig_ldr_path in find_ldconfig_ldr_paths(config): | |
42 | with ldconfig_ldr_path.open() as ldconfig_ldr: | |
43 | load_colours(ldconfig_ldr) | |
44 | model = read_ldraw( | |
45 | file.stream, | |
61
15c95d3fcfd8
show the filename of the processed file in the report
Teemu Piippo <teemu@hecknology.net>
parents:
57
diff
changeset
|
46 | name = filename, |
57 | 47 | ldraw_directories = config['libraries'], |
29 | 48 | ) |
49 | 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
|
50 | |
75f44d3063da
Reworked web front, problems are now sorted by category as well as line number
Santeri Piippo
parents:
29
diff
changeset
|
51 | # 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
|
52 | 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
|
53 | 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
|
54 | 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
|
55 | 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
|
56 | return render_template('webfront.html', |
8949af6a4279
added the list of issues onto the web frontend
Teemu Piippo <teemu@hecknology.net>
parents:
62
diff
changeset
|
57 | report = report, |
8949af6a4279
added the list of issues onto the web frontend
Teemu Piippo <teemu@hecknology.net>
parents:
62
diff
changeset
|
58 | name = filename, |
100
62759e5c4554
add some basic versioning
Teemu Piippo <teemu@hecknology.net>
parents:
99
diff
changeset
|
59 | problem_types = all_problem_types(test_suite), |
62759e5c4554
add some basic versioning
Teemu Piippo <teemu@hecknology.net>
parents:
99
diff
changeset
|
60 | **common_args, |
63
8949af6a4279
added the list of issues onto the web frontend
Teemu Piippo <teemu@hecknology.net>
parents:
62
diff
changeset
|
61 | ) |
32
75f44d3063da
Reworked web front, problems are now sorted by category as well as line number
Santeri Piippo
parents:
29
diff
changeset
|
62 | else: |
63
8949af6a4279
added the list of issues onto the web frontend
Teemu Piippo <teemu@hecknology.net>
parents:
62
diff
changeset
|
63 | test_suite = load_tests() |
8949af6a4279
added the list of issues onto the web frontend
Teemu Piippo <teemu@hecknology.net>
parents:
62
diff
changeset
|
64 | return render_template('webfront.html', |
8949af6a4279
added the list of issues onto the web frontend
Teemu Piippo <teemu@hecknology.net>
parents:
62
diff
changeset
|
65 | report = None, |
8949af6a4279
added the list of issues onto the web frontend
Teemu Piippo <teemu@hecknology.net>
parents:
62
diff
changeset
|
66 | name = None, |
100
62759e5c4554
add some basic versioning
Teemu Piippo <teemu@hecknology.net>
parents:
99
diff
changeset
|
67 | problem_types = all_problem_types(test_suite), |
62759e5c4554
add some basic versioning
Teemu Piippo <teemu@hecknology.net>
parents:
99
diff
changeset
|
68 | **common_args, |
63
8949af6a4279
added the list of issues onto the web frontend
Teemu Piippo <teemu@hecknology.net>
parents:
62
diff
changeset
|
69 | ) |
29 | 70 | |
32
75f44d3063da
Reworked web front, problems are now sorted by category as well as line number
Santeri Piippo
parents:
29
diff
changeset
|
71 | @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
|
72 | 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
|
73 | 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
|
74 | 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
|
75 | 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
|
76 | |
75f44d3063da
Reworked web front, problems are now sorted by category as well as line number
Santeri Piippo
parents:
29
diff
changeset
|
77 | if __name__ == '__main__': |
75f44d3063da
Reworked web front, problems are now sorted by category as well as line number
Santeri Piippo
parents:
29
diff
changeset
|
78 | 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
|
79 | parser = ArgumentParser() |
75f44d3063da
Reworked web front, problems are now sorted by category as well as line number
Santeri Piippo
parents:
29
diff
changeset
|
80 | 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
|
81 | 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
|
82 | 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
|
83 | app.run(port = args.port, debug = args.debug) |