webfront.py

Sat, 01 Jun 2019 10:47:58 +0300

author
Teemu Piippo <teemu@hecknology.net>
date
Sat, 01 Jun 2019 10:47:58 +0300
changeset 56
ed6d39c59e56
parent 32
75f44d3063da
child 41
4d87bc126368
child 57
c147116768f4
permissions
-rwxr-xr-x

fixed BFC INVERTNEXT being interpreted as a header command

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
db6ca177c6c4 added a simple web frontend
Santeri Piippo
parents:
diff changeset
3 from ldcheck import load_config, load_colours, find_ldconfig_ldr_paths
db6ca177c6c4 added a simple web frontend
Santeri Piippo
parents:
diff changeset
4 from ldcheck import read_ldraw
32
75f44d3063da Reworked web front, problems are now sorted by category as well as line number
Santeri Piippo
parents: 29
diff changeset
5 from testsuite import load_tests, check_model, problem_text
29
db6ca177c6c4 added a simple web frontend
Santeri Piippo
parents:
diff changeset
6
db6ca177c6c4 added a simple web frontend
Santeri Piippo
parents:
diff changeset
7 app = Flask('LDCheck')
db6ca177c6c4 added a simple web frontend
Santeri Piippo
parents:
diff changeset
8
db6ca177c6c4 added a simple web frontend
Santeri Piippo
parents:
diff changeset
9 @app.route('/', methods = ['GET', 'POST'])
db6ca177c6c4 added a simple web frontend
Santeri Piippo
parents:
diff changeset
10 def web_front():
db6ca177c6c4 added a simple web frontend
Santeri Piippo
parents:
diff changeset
11 if request.method == 'POST':
db6ca177c6c4 added a simple web frontend
Santeri Piippo
parents:
diff changeset
12 # check if the post request has the file part
db6ca177c6c4 added a simple web frontend
Santeri Piippo
parents:
diff changeset
13 if 'file' not in request.files or not request.files['file'].filename:
db6ca177c6c4 added a simple web frontend
Santeri Piippo
parents:
diff changeset
14 return redirect(request.url)
db6ca177c6c4 added a simple web frontend
Santeri Piippo
parents:
diff changeset
15 file = request.files['file']
db6ca177c6c4 added a simple web frontend
Santeri Piippo
parents:
diff changeset
16 config = load_config('ldcheck.cfg')
db6ca177c6c4 added a simple web frontend
Santeri Piippo
parents:
diff changeset
17 for ldconfig_ldr_path in find_ldconfig_ldr_paths(config):
db6ca177c6c4 added a simple web frontend
Santeri Piippo
parents:
diff changeset
18 with ldconfig_ldr_path.open() as ldconfig_ldr:
db6ca177c6c4 added a simple web frontend
Santeri Piippo
parents:
diff changeset
19 load_colours(ldconfig_ldr)
db6ca177c6c4 added a simple web frontend
Santeri Piippo
parents:
diff changeset
20 model = read_ldraw(
db6ca177c6c4 added a simple web frontend
Santeri Piippo
parents:
diff changeset
21 file.stream,
db6ca177c6c4 added a simple web frontend
Santeri Piippo
parents:
diff changeset
22 name = file.filename,
db6ca177c6c4 added a simple web frontend
Santeri Piippo
parents:
diff changeset
23 config = config,
db6ca177c6c4 added a simple web frontend
Santeri Piippo
parents:
diff changeset
24 )
db6ca177c6c4 added a simple web frontend
Santeri Piippo
parents:
diff changeset
25 test_suite = load_tests()
db6ca177c6c4 added a simple web frontend
Santeri Piippo
parents:
diff changeset
26 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
27
75f44d3063da Reworked web front, problems are now sorted by category as well as line number
Santeri Piippo
parents: 29
diff changeset
28 # 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
29 for problem in report['problems']:
75f44d3063da Reworked web front, problems are now sorted by category as well as line number
Santeri Piippo
parents: 29
diff changeset
30 object = model.body[problem['body-index']]
75f44d3063da Reworked web front, problems are now sorted by category as well as line number
Santeri Piippo
parents: 29
diff changeset
31 problem['message'] = problem_text(problem, test_suite)
75f44d3063da Reworked web front, problems are now sorted by category as well as line number
Santeri Piippo
parents: 29
diff changeset
32 problem['ldraw-code'] = object.textual_representation()
75f44d3063da Reworked web front, problems are now sorted by category as well as line number
Santeri Piippo
parents: 29
diff changeset
33 else:
75f44d3063da Reworked web front, problems are now sorted by category as well as line number
Santeri Piippo
parents: 29
diff changeset
34 report = None
75f44d3063da Reworked web front, problems are now sorted by category as well as line number
Santeri Piippo
parents: 29
diff changeset
35 return render_template('webfront.html',
75f44d3063da Reworked web front, problems are now sorted by category as well as line number
Santeri Piippo
parents: 29
diff changeset
36 report = report,
75f44d3063da Reworked web front, problems are now sorted by category as well as line number
Santeri Piippo
parents: 29
diff changeset
37 )
29
db6ca177c6c4 added a simple web frontend
Santeri Piippo
parents:
diff changeset
38
32
75f44d3063da Reworked web front, problems are now sorted by category as well as line number
Santeri Piippo
parents: 29
diff changeset
39 @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
40 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
41 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
42 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
43 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
44
75f44d3063da Reworked web front, problems are now sorted by category as well as line number
Santeri Piippo
parents: 29
diff changeset
45 if __name__ == '__main__':
75f44d3063da Reworked web front, problems are now sorted by category as well as line number
Santeri Piippo
parents: 29
diff changeset
46 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
47 parser = ArgumentParser()
75f44d3063da Reworked web front, problems are now sorted by category as well as line number
Santeri Piippo
parents: 29
diff changeset
48 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
49 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
50 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
51 app.run(port = args.port, debug = args.debug)

mercurial