webfront.py

Thu, 26 Aug 2021 19:54:15 +0300

author
Teemu Piippo <teemu@hecknology.net>
date
Thu, 26 Aug 2021 19:54:15 +0300
changeset 151
e4401bf4a387
parent 145
fde18c4d6784
permissions
-rwxr-xr-x

Fix merge issues regarding web front

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
151
e4401bf4a387 Fix merge issues regarding web front
Teemu Piippo <teemu@hecknology.net>
parents: 145
diff changeset
2 import argparse, sys
29
db6ca177c6c4 added a simple web frontend
Santeri Piippo
parents:
diff changeset
3 from flask import Flask, render_template, redirect, request
100
62759e5c4554 add some basic versioning
Teemu Piippo <teemu@hecknology.net>
parents: 99
diff changeset
4 from ldcheck import appname, version, version_string
151
e4401bf4a387 Fix merge issues regarding web front
Teemu Piippo <teemu@hecknology.net>
parents: 145
diff changeset
5 from ldcheck import load_rcfile, postprocess_library_paths
57
c147116768f4 fixed the web front
Teemu Piippo <teemu@hecknology.net>
parents: 32
diff changeset
6 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
7 from testsuite import load_tests, check_model, problem_text, all_problem_types
29
db6ca177c6c4 added a simple web frontend
Santeri Piippo
parents:
diff changeset
8
151
e4401bf4a387 Fix merge issues regarding web front
Teemu Piippo <teemu@hecknology.net>
parents: 145
diff changeset
9 parser = argparse.ArgumentParser()
e4401bf4a387 Fix merge issues regarding web front
Teemu Piippo <teemu@hecknology.net>
parents: 145
diff changeset
10 parser.add_argument('-l', '--library', action = 'append')
e4401bf4a387 Fix merge issues regarding web front
Teemu Piippo <teemu@hecknology.net>
parents: 145
diff changeset
11 parser.add_argument('--color', action = 'store_true')
e4401bf4a387 Fix merge issues regarding web front
Teemu Piippo <teemu@hecknology.net>
parents: 145
diff changeset
12 args = parser.parse_args(load_rcfile() + sys.argv[1:])
e4401bf4a387 Fix merge issues regarding web front
Teemu Piippo <teemu@hecknology.net>
parents: 145
diff changeset
13 libraries = postprocess_library_paths(args.library)
e4401bf4a387 Fix merge issues regarding web front
Teemu Piippo <teemu@hecknology.net>
parents: 145
diff changeset
14
100
62759e5c4554 add some basic versioning
Teemu Piippo <teemu@hecknology.net>
parents: 99
diff changeset
15 app = Flask(appname)
29
db6ca177c6c4 added a simple web frontend
Santeri Piippo
parents:
diff changeset
16
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
17 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
18 try:
151
e4401bf4a387 Fix merge issues regarding web front
Teemu Piippo <teemu@hecknology.net>
parents: 145
diff changeset
19 context = LDrawContext(libraries = libraries)
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
20 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
21 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
22 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
23 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
24
99
01941a811b5a move format_report_html to webfront.py
Teemu Piippo <teemu@hecknology.net>
parents: 63
diff changeset
25 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
26 messages = []
01941a811b5a move format_report_html to webfront.py
Teemu Piippo <teemu@hecknology.net>
parents: 63
diff changeset
27 for problem in report['problems']:
01941a811b5a move format_report_html to webfront.py
Teemu Piippo <teemu@hecknology.net>
parents: 63
diff changeset
28 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
29 message = str.format(
01941a811b5a move format_report_html to webfront.py
Teemu Piippo <teemu@hecknology.net>
parents: 63
diff changeset
30 '<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
31 '{problem_type}: {message}<br />{ldraw_code}</li>',
01941a811b5a move format_report_html to webfront.py
Teemu Piippo <teemu@hecknology.net>
parents: 63
diff changeset
32 model_name = model.name,
01941a811b5a move format_report_html to webfront.py
Teemu Piippo <teemu@hecknology.net>
parents: 63
diff changeset
33 line_number = problem.line_number,
01941a811b5a move format_report_html to webfront.py
Teemu Piippo <teemu@hecknology.net>
parents: 63
diff changeset
34 problem_type = problem.severity,
01941a811b5a move format_report_html to webfront.py
Teemu Piippo <teemu@hecknology.net>
parents: 63
diff changeset
35 message = problem_text(problem, test_suite),
01941a811b5a move format_report_html to webfront.py
Teemu Piippo <teemu@hecknology.net>
parents: 63
diff changeset
36 ldraw_code = ldraw_code,
01941a811b5a move format_report_html to webfront.py
Teemu Piippo <teemu@hecknology.net>
parents: 63
diff changeset
37 )
01941a811b5a move format_report_html to webfront.py
Teemu Piippo <teemu@hecknology.net>
parents: 63
diff changeset
38 messages.append(message)
01941a811b5a move format_report_html to webfront.py
Teemu Piippo <teemu@hecknology.net>
parents: 63
diff changeset
39 return '\n'.join(messages)
01941a811b5a move format_report_html to webfront.py
Teemu Piippo <teemu@hecknology.net>
parents: 63
diff changeset
40
29
db6ca177c6c4 added a simple web frontend
Santeri Piippo
parents:
diff changeset
41 @app.route('/', methods = ['GET', 'POST'])
db6ca177c6c4 added a simple web frontend
Santeri Piippo
parents:
diff changeset
42 def web_front():
63
8949af6a4279 added the list of issues onto the web frontend
Teemu Piippo <teemu@hecknology.net>
parents: 62
diff changeset
43 test_suite = load_tests()
100
62759e5c4554 add some basic versioning
Teemu Piippo <teemu@hecknology.net>
parents: 99
diff changeset
44 common_args = {
62759e5c4554 add some basic versioning
Teemu Piippo <teemu@hecknology.net>
parents: 99
diff changeset
45 'appname': appname,
62759e5c4554 add some basic versioning
Teemu Piippo <teemu@hecknology.net>
parents: 99
diff changeset
46 'version': version_string,
62759e5c4554 add some basic versioning
Teemu Piippo <teemu@hecknology.net>
parents: 99
diff changeset
47 'is_debug': max(version) == 9999,
62759e5c4554 add some basic versioning
Teemu Piippo <teemu@hecknology.net>
parents: 99
diff changeset
48 }
29
db6ca177c6c4 added a simple web frontend
Santeri Piippo
parents:
diff changeset
49 if request.method == 'POST':
db6ca177c6c4 added a simple web frontend
Santeri Piippo
parents:
diff changeset
50 # check if the post request has the file part
db6ca177c6c4 added a simple web frontend
Santeri Piippo
parents:
diff changeset
51 if 'file' not in request.files or not request.files['file'].filename:
db6ca177c6c4 added a simple web frontend
Santeri Piippo
parents:
diff changeset
52 return redirect(request.url)
db6ca177c6c4 added a simple web frontend
Santeri Piippo
parents:
diff changeset
53 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
54 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
55 model = read_ldraw(file.stream, name = filename, context = context)
29
db6ca177c6c4 added a simple web frontend
Santeri Piippo
parents:
diff changeset
56 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
57
75f44d3063da Reworked web front, problems are now sorted by category as well as line number
Santeri Piippo
parents: 29
diff changeset
58 # 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
59 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
60 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
61 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
62 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
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 = report,
8949af6a4279 added the list of issues onto the web frontend
Teemu Piippo <teemu@hecknology.net>
parents: 62
diff changeset
65 name = filename,
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 )
32
75f44d3063da Reworked web front, problems are now sorted by category as well as line number
Santeri Piippo
parents: 29
diff changeset
69 else:
63
8949af6a4279 added the list of issues onto the web frontend
Teemu Piippo <teemu@hecknology.net>
parents: 62
diff changeset
70 test_suite = load_tests()
8949af6a4279 added the list of issues onto the web frontend
Teemu Piippo <teemu@hecknology.net>
parents: 62
diff changeset
71 return render_template('webfront.html',
8949af6a4279 added the list of issues onto the web frontend
Teemu Piippo <teemu@hecknology.net>
parents: 62
diff changeset
72 report = None,
8949af6a4279 added the list of issues onto the web frontend
Teemu Piippo <teemu@hecknology.net>
parents: 62
diff changeset
73 name = None,
100
62759e5c4554 add some basic versioning
Teemu Piippo <teemu@hecknology.net>
parents: 99
diff changeset
74 problem_types = all_problem_types(test_suite),
62759e5c4554 add some basic versioning
Teemu Piippo <teemu@hecknology.net>
parents: 99
diff changeset
75 **common_args,
63
8949af6a4279 added the list of issues onto the web frontend
Teemu Piippo <teemu@hecknology.net>
parents: 62
diff changeset
76 )
29
db6ca177c6c4 added a simple web frontend
Santeri Piippo
parents:
diff changeset
77
32
75f44d3063da Reworked web front, problems are now sorted by category as well as line number
Santeri Piippo
parents: 29
diff changeset
78 @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
79 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
80 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
81 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
82 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
83
75f44d3063da Reworked web front, problems are now sorted by category as well as line number
Santeri Piippo
parents: 29
diff changeset
84 if __name__ == '__main__':
75f44d3063da Reworked web front, problems are now sorted by category as well as line number
Santeri Piippo
parents: 29
diff changeset
85 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
86 parser = ArgumentParser()
75f44d3063da Reworked web front, problems are now sorted by category as well as line number
Santeri Piippo
parents: 29
diff changeset
87 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
88 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
89 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
90 app.run(port = args.port, debug = args.debug)

mercurial