webfront.py

Sat, 08 Jun 2019 11:17:17 +0300

author
Teemu Piippo <teemu@hecknology.net>
date
Sat, 08 Jun 2019 11:17:17 +0300
changeset 65
f2dc17b830e0
parent 63
8949af6a4279
permissions
-rwxr-xr-x

cleanup

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
65
f2dc17b830e0 cleanup
Teemu Piippo <teemu@hecknology.net>
parents: 63
diff changeset
2 import flask
f2dc17b830e0 cleanup
Teemu Piippo <teemu@hecknology.net>
parents: 63
diff changeset
3 import testsuite
f2dc17b830e0 cleanup
Teemu Piippo <teemu@hecknology.net>
parents: 63
diff changeset
4 import config
f2dc17b830e0 cleanup
Teemu Piippo <teemu@hecknology.net>
parents: 63
diff changeset
5 import parse
f2dc17b830e0 cleanup
Teemu Piippo <teemu@hecknology.net>
parents: 63
diff changeset
6 import colours
29
db6ca177c6c4 added a simple web frontend
Santeri Piippo
parents:
diff changeset
7
65
f2dc17b830e0 cleanup
Teemu Piippo <teemu@hecknology.net>
parents: 63
diff changeset
8 app = flask.Flask('LDCheck')
29
db6ca177c6c4 added a simple web frontend
Santeri Piippo
parents:
diff changeset
9
db6ca177c6c4 added a simple web frontend
Santeri Piippo
parents:
diff changeset
10 @app.route('/', methods = ['GET', 'POST'])
db6ca177c6c4 added a simple web frontend
Santeri Piippo
parents:
diff changeset
11 def web_front():
65
f2dc17b830e0 cleanup
Teemu Piippo <teemu@hecknology.net>
parents: 63
diff changeset
12 test_suite = testsuite.load_tests()
f2dc17b830e0 cleanup
Teemu Piippo <teemu@hecknology.net>
parents: 63
diff changeset
13 request = flask.request
29
db6ca177c6c4 added a simple web frontend
Santeri Piippo
parents:
diff changeset
14 if request.method == 'POST':
db6ca177c6c4 added a simple web frontend
Santeri Piippo
parents:
diff changeset
15 # check if the post request has the file part
db6ca177c6c4 added a simple web frontend
Santeri Piippo
parents:
diff changeset
16 if 'file' not in request.files or not request.files['file'].filename:
65
f2dc17b830e0 cleanup
Teemu Piippo <teemu@hecknology.net>
parents: 63
diff changeset
17 return flask.redirect(request.url)
29
db6ca177c6c4 added a simple web frontend
Santeri Piippo
parents:
diff changeset
18 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
19 filename = file.filename
65
f2dc17b830e0 cleanup
Teemu Piippo <teemu@hecknology.net>
parents: 63
diff changeset
20 config_object = config.load_config('ldcheck.cfg')
f2dc17b830e0 cleanup
Teemu Piippo <teemu@hecknology.net>
parents: 63
diff changeset
21 for ldconfig_ldr_path in config.find_ldconfig_ldr_paths(config_object):
29
db6ca177c6c4 added a simple web frontend
Santeri Piippo
parents:
diff changeset
22 with ldconfig_ldr_path.open() as ldconfig_ldr:
65
f2dc17b830e0 cleanup
Teemu Piippo <teemu@hecknology.net>
parents: 63
diff changeset
23 colours.load_colours(ldconfig_ldr)
f2dc17b830e0 cleanup
Teemu Piippo <teemu@hecknology.net>
parents: 63
diff changeset
24 model = parse.read_ldraw(
29
db6ca177c6c4 added a simple web frontend
Santeri Piippo
parents:
diff changeset
25 file.stream,
61
15c95d3fcfd8 show the filename of the processed file in the report
Teemu Piippo <teemu@hecknology.net>
parents: 57
diff changeset
26 name = filename,
65
f2dc17b830e0 cleanup
Teemu Piippo <teemu@hecknology.net>
parents: 63
diff changeset
27 ldraw_directories = config_object['libraries'],
29
db6ca177c6c4 added a simple web frontend
Santeri Piippo
parents:
diff changeset
28 )
65
f2dc17b830e0 cleanup
Teemu Piippo <teemu@hecknology.net>
parents: 63
diff changeset
29 report = testsuite.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
30
75f44d3063da Reworked web front, problems are now sorted by category as well as line number
Santeri Piippo
parents: 29
diff changeset
31 # 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
32 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
33 object = model.body[problem.body_index]
65
f2dc17b830e0 cleanup
Teemu Piippo <teemu@hecknology.net>
parents: 63
diff changeset
34 problem.message_str = str(problem)
62
f0a6bf48b05e Problem reporting revamp, program is now aware of its problem types
Teemu Piippo <teemu@hecknology.net>
parents: 61
diff changeset
35 problem.ldraw_code = object.textual_representation()
65
f2dc17b830e0 cleanup
Teemu Piippo <teemu@hecknology.net>
parents: 63
diff changeset
36 return flask.render_template('webfront.html',
63
8949af6a4279 added the list of issues onto the web frontend
Teemu Piippo <teemu@hecknology.net>
parents: 62
diff changeset
37 report = report,
8949af6a4279 added the list of issues onto the web frontend
Teemu Piippo <teemu@hecknology.net>
parents: 62
diff changeset
38 name = filename,
65
f2dc17b830e0 cleanup
Teemu Piippo <teemu@hecknology.net>
parents: 63
diff changeset
39 problem_types = testsuite.all_problem_types(test_suite)
63
8949af6a4279 added the list of issues onto the web frontend
Teemu Piippo <teemu@hecknology.net>
parents: 62
diff changeset
40 )
32
75f44d3063da Reworked web front, problems are now sorted by category as well as line number
Santeri Piippo
parents: 29
diff changeset
41 else:
65
f2dc17b830e0 cleanup
Teemu Piippo <teemu@hecknology.net>
parents: 63
diff changeset
42 return flask.render_template('webfront.html',
63
8949af6a4279 added the list of issues onto the web frontend
Teemu Piippo <teemu@hecknology.net>
parents: 62
diff changeset
43 report = None,
8949af6a4279 added the list of issues onto the web frontend
Teemu Piippo <teemu@hecknology.net>
parents: 62
diff changeset
44 name = None,
65
f2dc17b830e0 cleanup
Teemu Piippo <teemu@hecknology.net>
parents: 63
diff changeset
45 problem_types = testsuite.all_problem_types(test_suite)
63
8949af6a4279 added the list of issues onto the web frontend
Teemu Piippo <teemu@hecknology.net>
parents: 62
diff changeset
46 )
29
db6ca177c6c4 added a simple web frontend
Santeri Piippo
parents:
diff changeset
47
32
75f44d3063da Reworked web front, problems are now sorted by category as well as line number
Santeri Piippo
parents: 29
diff changeset
48 @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
49 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
50 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
51 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
52 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
53
75f44d3063da Reworked web front, problems are now sorted by category as well as line number
Santeri Piippo
parents: 29
diff changeset
54 if __name__ == '__main__':
75f44d3063da Reworked web front, problems are now sorted by category as well as line number
Santeri Piippo
parents: 29
diff changeset
55 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
56 parser = ArgumentParser()
75f44d3063da Reworked web front, problems are now sorted by category as well as line number
Santeri Piippo
parents: 29
diff changeset
57 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
58 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
59 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
60 app.run(port = args.port, debug = args.debug)

mercurial