Wed, 31 Jan 2018 14:54:58 +0200
fixed python3.6ism
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 |
41
4d87bc126368
don't crash and burn if someone sends something that's not LDraw
Santeri Piippo
parents:
32
diff
changeset
|
2 | from flask import Flask, render_template, redirect, request, abort |
29 | 3 | from ldcheck import load_config, load_colours, find_ldconfig_ldr_paths |
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 | 6 | |
7 | app = Flask('LDCheck') | |
8 | ||
9 | @app.route('/', methods = ['GET', 'POST']) | |
10 | def web_front(): | |
11 | if request.method == 'POST': | |
12 | # check if the post request has the file part | |
13 | if 'file' not in request.files or not request.files['file'].filename: | |
14 | return redirect(request.url) | |
15 | file = request.files['file'] | |
16 | config = load_config('ldcheck.cfg') | |
17 | for ldconfig_ldr_path in find_ldconfig_ldr_paths(config): | |
18 | with ldconfig_ldr_path.open() as ldconfig_ldr: | |
19 | load_colours(ldconfig_ldr) | |
41
4d87bc126368
don't crash and burn if someone sends something that's not LDraw
Santeri Piippo
parents:
32
diff
changeset
|
20 | try: |
4d87bc126368
don't crash and burn if someone sends something that's not LDraw
Santeri Piippo
parents:
32
diff
changeset
|
21 | model = read_ldraw( |
4d87bc126368
don't crash and burn if someone sends something that's not LDraw
Santeri Piippo
parents:
32
diff
changeset
|
22 | file.stream, |
4d87bc126368
don't crash and burn if someone sends something that's not LDraw
Santeri Piippo
parents:
32
diff
changeset
|
23 | name = file.filename, |
4d87bc126368
don't crash and burn if someone sends something that's not LDraw
Santeri Piippo
parents:
32
diff
changeset
|
24 | config = config, |
4d87bc126368
don't crash and burn if someone sends something that's not LDraw
Santeri Piippo
parents:
32
diff
changeset
|
25 | ) |
4d87bc126368
don't crash and burn if someone sends something that's not LDraw
Santeri Piippo
parents:
32
diff
changeset
|
26 | except: |
4d87bc126368
don't crash and burn if someone sends something that's not LDraw
Santeri Piippo
parents:
32
diff
changeset
|
27 | abort(400) |
29 | 28 | test_suite = load_tests() |
29 | 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
|
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']: |
75f44d3063da
Reworked web front, problems are now sorted by category as well as line number
Santeri Piippo
parents:
29
diff
changeset
|
33 | 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
|
34 | 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
|
35 | 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
|
36 | else: |
75f44d3063da
Reworked web front, problems are now sorted by category as well as line number
Santeri Piippo
parents:
29
diff
changeset
|
37 | report = None |
75f44d3063da
Reworked web front, problems are now sorted by category as well as line number
Santeri Piippo
parents:
29
diff
changeset
|
38 | 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
|
39 | report = report, |
75f44d3063da
Reworked web front, problems are now sorted by category as well as line number
Santeri Piippo
parents:
29
diff
changeset
|
40 | ) |
29 | 41 | |
32
75f44d3063da
Reworked web front, problems are now sorted by category as well as line number
Santeri Piippo
parents:
29
diff
changeset
|
42 | @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
|
43 | 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
|
44 | 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
|
45 | 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
|
46 | 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
|
47 | |
75f44d3063da
Reworked web front, problems are now sorted by category as well as line number
Santeri Piippo
parents:
29
diff
changeset
|
48 | if __name__ == '__main__': |
75f44d3063da
Reworked web front, problems are now sorted by category as well as line number
Santeri Piippo
parents:
29
diff
changeset
|
49 | 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
|
50 | parser = ArgumentParser() |
75f44d3063da
Reworked web front, problems are now sorted by category as well as line number
Santeri Piippo
parents:
29
diff
changeset
|
51 | 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
|
52 | 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
|
53 | 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
|
54 | app.run(port = args.port, debug = args.debug) |