webfront.py

Thu, 26 Aug 2021 19:37:00 +0300

author
Teemu Piippo <teemu@hecknology.net>
date
Thu, 26 Aug 2021 19:37:00 +0300
changeset 148
8f621aa4cfd7
parent 145
fde18c4d6784
child 151
e4401bf4a387
permissions
-rwxr-xr-x

Update license year

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
57
c147116768f4 fixed the web front
Teemu Piippo <teemu@hecknology.net>
parents: 32
diff changeset
4 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
5 from testsuite import load_tests, check_model, problem_text, all_problem_types
29
db6ca177c6c4 added a simple web frontend
Santeri Piippo
parents:
diff changeset
6
100
62759e5c4554 add some basic versioning
Teemu Piippo <teemu@hecknology.net>
parents: 99
diff changeset
7 app = Flask(appname)
29
db6ca177c6c4 added a simple web frontend
Santeri Piippo
parents:
diff changeset
8
145
fde18c4d6784 refactoring: moved context-dependant data to new class LDrawContext. ldcheck no longer writes the config file, and looks for it in sensible locations like ~/.config and /etc. LDraw libraries can now be specified on the command line.
Teemu Piippo <teemu@hecknology.net>
parents: 122
diff changeset
9 from ldcheck import LDrawContext
fde18c4d6784 refactoring: moved context-dependant data to new class LDrawContext. ldcheck no longer writes the config file, and looks for it in sensible locations like ~/.config and /etc. LDraw libraries can now be specified on the command line.
Teemu Piippo <teemu@hecknology.net>
parents: 122
diff changeset
10 try:
fde18c4d6784 refactoring: moved context-dependant data to new class LDrawContext. ldcheck no longer writes the config file, and looks for it in sensible locations like ~/.config and /etc. LDraw libraries can now be specified on the command line.
Teemu Piippo <teemu@hecknology.net>
parents: 122
diff changeset
11 context = LDrawContext()
fde18c4d6784 refactoring: moved context-dependant data to new class LDrawContext. ldcheck no longer writes the config file, and looks for it in sensible locations like ~/.config and /etc. LDraw libraries can now be specified on the command line.
Teemu Piippo <teemu@hecknology.net>
parents: 122
diff changeset
12 except RuntimeError as error:
fde18c4d6784 refactoring: moved context-dependant data to new class LDrawContext. ldcheck no longer writes the config file, and looks for it in sensible locations like ~/.config and /etc. LDraw libraries can now be specified on the command line.
Teemu Piippo <teemu@hecknology.net>
parents: 122
diff changeset
13 from sys import stderr, exit
fde18c4d6784 refactoring: moved context-dependant data to new class LDrawContext. ldcheck no longer writes the config file, and looks for it in sensible locations like ~/.config and /etc. LDraw libraries can now be specified on the command line.
Teemu Piippo <teemu@hecknology.net>
parents: 122
diff changeset
14 print('error:', str(error), file = stderr)
fde18c4d6784 refactoring: moved context-dependant data to new class LDrawContext. ldcheck no longer writes the config file, and looks for it in sensible locations like ~/.config and /etc. LDraw libraries can now be specified on the command line.
Teemu Piippo <teemu@hecknology.net>
parents: 122
diff changeset
15 exit(1)
fde18c4d6784 refactoring: moved context-dependant data to new class LDrawContext. ldcheck no longer writes the config file, and looks for it in sensible locations like ~/.config and /etc. LDraw libraries can now be specified on the command line.
Teemu Piippo <teemu@hecknology.net>
parents: 122
diff changeset
16
99
01941a811b5a move format_report_html to webfront.py
Teemu Piippo <teemu@hecknology.net>
parents: 63
diff changeset
17 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
18 messages = []
01941a811b5a move format_report_html to webfront.py
Teemu Piippo <teemu@hecknology.net>
parents: 63
diff changeset
19 for problem in report['problems']:
01941a811b5a move format_report_html to webfront.py
Teemu Piippo <teemu@hecknology.net>
parents: 63
diff changeset
20 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
21 message = str.format(
01941a811b5a move format_report_html to webfront.py
Teemu Piippo <teemu@hecknology.net>
parents: 63
diff changeset
22 '<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
23 '{problem_type}: {message}<br />{ldraw_code}</li>',
01941a811b5a move format_report_html to webfront.py
Teemu Piippo <teemu@hecknology.net>
parents: 63
diff changeset
24 model_name = model.name,
01941a811b5a move format_report_html to webfront.py
Teemu Piippo <teemu@hecknology.net>
parents: 63
diff changeset
25 line_number = problem.line_number,
01941a811b5a move format_report_html to webfront.py
Teemu Piippo <teemu@hecknology.net>
parents: 63
diff changeset
26 problem_type = problem.severity,
01941a811b5a move format_report_html to webfront.py
Teemu Piippo <teemu@hecknology.net>
parents: 63
diff changeset
27 message = problem_text(problem, test_suite),
01941a811b5a move format_report_html to webfront.py
Teemu Piippo <teemu@hecknology.net>
parents: 63
diff changeset
28 ldraw_code = ldraw_code,
01941a811b5a move format_report_html to webfront.py
Teemu Piippo <teemu@hecknology.net>
parents: 63
diff changeset
29 )
01941a811b5a move format_report_html to webfront.py
Teemu Piippo <teemu@hecknology.net>
parents: 63
diff changeset
30 messages.append(message)
01941a811b5a move format_report_html to webfront.py
Teemu Piippo <teemu@hecknology.net>
parents: 63
diff changeset
31 return '\n'.join(messages)
01941a811b5a move format_report_html to webfront.py
Teemu Piippo <teemu@hecknology.net>
parents: 63
diff changeset
32
29
db6ca177c6c4 added a simple web frontend
Santeri Piippo
parents:
diff changeset
33 @app.route('/', methods = ['GET', 'POST'])
db6ca177c6c4 added a simple web frontend
Santeri Piippo
parents:
diff changeset
34 def web_front():
63
8949af6a4279 added the list of issues onto the web frontend
Teemu Piippo <teemu@hecknology.net>
parents: 62
diff changeset
35 test_suite = load_tests()
100
62759e5c4554 add some basic versioning
Teemu Piippo <teemu@hecknology.net>
parents: 99
diff changeset
36 common_args = {
62759e5c4554 add some basic versioning
Teemu Piippo <teemu@hecknology.net>
parents: 99
diff changeset
37 'appname': appname,
62759e5c4554 add some basic versioning
Teemu Piippo <teemu@hecknology.net>
parents: 99
diff changeset
38 'version': version_string,
62759e5c4554 add some basic versioning
Teemu Piippo <teemu@hecknology.net>
parents: 99
diff changeset
39 'is_debug': max(version) == 9999,
62759e5c4554 add some basic versioning
Teemu Piippo <teemu@hecknology.net>
parents: 99
diff changeset
40 }
29
db6ca177c6c4 added a simple web frontend
Santeri Piippo
parents:
diff changeset
41 if request.method == 'POST':
db6ca177c6c4 added a simple web frontend
Santeri Piippo
parents:
diff changeset
42 # check if the post request has the file part
db6ca177c6c4 added a simple web frontend
Santeri Piippo
parents:
diff changeset
43 if 'file' not in request.files or not request.files['file'].filename:
db6ca177c6c4 added a simple web frontend
Santeri Piippo
parents:
diff changeset
44 return redirect(request.url)
db6ca177c6c4 added a simple web frontend
Santeri Piippo
parents:
diff changeset
45 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
46 filename = file.filename
145
fde18c4d6784 refactoring: moved context-dependant data to new class LDrawContext. ldcheck no longer writes the config file, and looks for it in sensible locations like ~/.config and /etc. LDraw libraries can now be specified on the command line.
Teemu Piippo <teemu@hecknology.net>
parents: 122
diff changeset
47 model = read_ldraw(file.stream, name = filename, context = context)
29
db6ca177c6c4 added a simple web frontend
Santeri Piippo
parents:
diff changeset
48 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
49
75f44d3063da Reworked web front, problems are now sorted by category as well as line number
Santeri Piippo
parents: 29
diff changeset
50 # 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
51 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
52 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
53 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
54 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
55 return render_template('webfront.html',
8949af6a4279 added the list of issues onto the web frontend
Teemu Piippo <teemu@hecknology.net>
parents: 62
diff changeset
56 report = report,
8949af6a4279 added the list of issues onto the web frontend
Teemu Piippo <teemu@hecknology.net>
parents: 62
diff changeset
57 name = filename,
100
62759e5c4554 add some basic versioning
Teemu Piippo <teemu@hecknology.net>
parents: 99
diff changeset
58 problem_types = all_problem_types(test_suite),
62759e5c4554 add some basic versioning
Teemu Piippo <teemu@hecknology.net>
parents: 99
diff changeset
59 **common_args,
63
8949af6a4279 added the list of issues onto the web frontend
Teemu Piippo <teemu@hecknology.net>
parents: 62
diff changeset
60 )
32
75f44d3063da Reworked web front, problems are now sorted by category as well as line number
Santeri Piippo
parents: 29
diff changeset
61 else:
63
8949af6a4279 added the list of issues onto the web frontend
Teemu Piippo <teemu@hecknology.net>
parents: 62
diff changeset
62 test_suite = load_tests()
8949af6a4279 added the list of issues onto the web frontend
Teemu Piippo <teemu@hecknology.net>
parents: 62
diff changeset
63 return render_template('webfront.html',
8949af6a4279 added the list of issues onto the web frontend
Teemu Piippo <teemu@hecknology.net>
parents: 62
diff changeset
64 report = None,
8949af6a4279 added the list of issues onto the web frontend
Teemu Piippo <teemu@hecknology.net>
parents: 62
diff changeset
65 name = None,
100
62759e5c4554 add some basic versioning
Teemu Piippo <teemu@hecknology.net>
parents: 99
diff changeset
66 problem_types = all_problem_types(test_suite),
62759e5c4554 add some basic versioning
Teemu Piippo <teemu@hecknology.net>
parents: 99
diff changeset
67 **common_args,
63
8949af6a4279 added the list of issues onto the web frontend
Teemu Piippo <teemu@hecknology.net>
parents: 62
diff changeset
68 )
29
db6ca177c6c4 added a simple web frontend
Santeri Piippo
parents:
diff changeset
69
32
75f44d3063da Reworked web front, problems are now sorted by category as well as line number
Santeri Piippo
parents: 29
diff changeset
70 @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
71 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
72 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
73 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
74 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
75
75f44d3063da Reworked web front, problems are now sorted by category as well as line number
Santeri Piippo
parents: 29
diff changeset
76 if __name__ == '__main__':
75f44d3063da Reworked web front, problems are now sorted by category as well as line number
Santeri Piippo
parents: 29
diff changeset
77 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
78 parser = ArgumentParser()
75f44d3063da Reworked web front, problems are now sorted by category as well as line number
Santeri Piippo
parents: 29
diff changeset
79 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
80 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
81 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
82 app.run(port = args.port, debug = args.debug)

mercurial