webfront.py

Tue, 25 Aug 2020 23:37:53 +0300

author
Teemu Piippo <teemu@hecknology.net>
date
Tue, 25 Aug 2020 23:37:53 +0300
changeset 109
b627f8963a84
parent 100
62759e5c4554
child 112
d997456985b2
permissions
-rwxr-xr-x

handle file not found more cleanly

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
db6ca177c6c4 added a simple web frontend
Santeri Piippo
parents:
diff changeset
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
db6ca177c6c4 added a simple web frontend
Santeri Piippo
parents:
diff changeset
4 from ldcheck import load_config, load_colours, find_ldconfig_ldr_paths
57
c147116768f4 fixed the web front
Teemu Piippo <teemu@hecknology.net>
parents: 32
diff changeset
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
db6ca177c6c4 added a simple web frontend
Santeri Piippo
parents:
diff changeset
7
100
62759e5c4554 add some basic versioning
Teemu Piippo <teemu@hecknology.net>
parents: 99
diff changeset
8 app = Flask(appname)
29
db6ca177c6c4 added a simple web frontend
Santeri Piippo
parents:
diff changeset
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
db6ca177c6c4 added a simple web frontend
Santeri Piippo
parents:
diff changeset
26 @app.route('/', methods = ['GET', 'POST'])
db6ca177c6c4 added a simple web frontend
Santeri Piippo
parents:
diff changeset
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
db6ca177c6c4 added a simple web frontend
Santeri Piippo
parents:
diff changeset
34 if request.method == 'POST':
db6ca177c6c4 added a simple web frontend
Santeri Piippo
parents:
diff changeset
35 # check if the post request has the file part
db6ca177c6c4 added a simple web frontend
Santeri Piippo
parents:
diff changeset
36 if 'file' not in request.files or not request.files['file'].filename:
db6ca177c6c4 added a simple web frontend
Santeri Piippo
parents:
diff changeset
37 return redirect(request.url)
db6ca177c6c4 added a simple web frontend
Santeri Piippo
parents:
diff changeset
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
db6ca177c6c4 added a simple web frontend
Santeri Piippo
parents:
diff changeset
40 config = load_config('ldcheck.cfg')
db6ca177c6c4 added a simple web frontend
Santeri Piippo
parents:
diff changeset
41 for ldconfig_ldr_path in find_ldconfig_ldr_paths(config):
db6ca177c6c4 added a simple web frontend
Santeri Piippo
parents:
diff changeset
42 with ldconfig_ldr_path.open() as ldconfig_ldr:
db6ca177c6c4 added a simple web frontend
Santeri Piippo
parents:
diff changeset
43 load_colours(ldconfig_ldr)
db6ca177c6c4 added a simple web frontend
Santeri Piippo
parents:
diff changeset
44 model = read_ldraw(
db6ca177c6c4 added a simple web frontend
Santeri Piippo
parents:
diff changeset
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
c147116768f4 fixed the web front
Teemu Piippo <teemu@hecknology.net>
parents: 32
diff changeset
47 ldraw_directories = config['libraries'],
100
62759e5c4554 add some basic versioning
Teemu Piippo <teemu@hecknology.net>
parents: 99
diff changeset
48 **common_args,
29
db6ca177c6c4 added a simple web frontend
Santeri Piippo
parents:
diff changeset
49 )
db6ca177c6c4 added a simple web frontend
Santeri Piippo
parents:
diff changeset
50 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
51
75f44d3063da Reworked web front, problems are now sorted by category as well as line number
Santeri Piippo
parents: 29
diff changeset
52 # 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
53 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
54 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
55 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
56 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
57 return render_template('webfront.html',
8949af6a4279 added the list of issues onto the web frontend
Teemu Piippo <teemu@hecknology.net>
parents: 62
diff changeset
58 report = report,
8949af6a4279 added the list of issues onto the web frontend
Teemu Piippo <teemu@hecknology.net>
parents: 62
diff changeset
59 name = filename,
100
62759e5c4554 add some basic versioning
Teemu Piippo <teemu@hecknology.net>
parents: 99
diff changeset
60 problem_types = all_problem_types(test_suite),
62759e5c4554 add some basic versioning
Teemu Piippo <teemu@hecknology.net>
parents: 99
diff changeset
61 **common_args,
63
8949af6a4279 added the list of issues onto the web frontend
Teemu Piippo <teemu@hecknology.net>
parents: 62
diff changeset
62 )
32
75f44d3063da Reworked web front, problems are now sorted by category as well as line number
Santeri Piippo
parents: 29
diff changeset
63 else:
63
8949af6a4279 added the list of issues onto the web frontend
Teemu Piippo <teemu@hecknology.net>
parents: 62
diff changeset
64 test_suite = load_tests()
8949af6a4279 added the list of issues onto the web frontend
Teemu Piippo <teemu@hecknology.net>
parents: 62
diff changeset
65 return render_template('webfront.html',
8949af6a4279 added the list of issues onto the web frontend
Teemu Piippo <teemu@hecknology.net>
parents: 62
diff changeset
66 report = None,
8949af6a4279 added the list of issues onto the web frontend
Teemu Piippo <teemu@hecknology.net>
parents: 62
diff changeset
67 name = None,
100
62759e5c4554 add some basic versioning
Teemu Piippo <teemu@hecknology.net>
parents: 99
diff changeset
68 problem_types = all_problem_types(test_suite),
62759e5c4554 add some basic versioning
Teemu Piippo <teemu@hecknology.net>
parents: 99
diff changeset
69 **common_args,
63
8949af6a4279 added the list of issues onto the web frontend
Teemu Piippo <teemu@hecknology.net>
parents: 62
diff changeset
70 )
29
db6ca177c6c4 added a simple web frontend
Santeri Piippo
parents:
diff changeset
71
32
75f44d3063da Reworked web front, problems are now sorted by category as well as line number
Santeri Piippo
parents: 29
diff changeset
72 @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
73 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
74 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
75 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
76 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
77
75f44d3063da Reworked web front, problems are now sorted by category as well as line number
Santeri Piippo
parents: 29
diff changeset
78 if __name__ == '__main__':
75f44d3063da Reworked web front, problems are now sorted by category as well as line number
Santeri Piippo
parents: 29
diff changeset
79 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
80 parser = ArgumentParser()
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('-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
82 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
83 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
84 app.run(port = args.port, debug = args.debug)

mercurial