webfront.py

Thu, 26 Aug 2021 19:16:25 +0300

author
Teemu Piippo <teemu@hecknology.net>
date
Thu, 26 Aug 2021 19:16:25 +0300
changeset 146
3555679d276b
parent 63
8949af6a4279
child 65
f2dc17b830e0
child 99
01941a811b5a
permissions
-rwxr-xr-x

Cleanup ldcheck.py
Remove dependency on configobj. Configuration file replaced with command line arguments and rcfile

#!/usr/bin/env python3
from flask import Flask, render_template, redirect, request
from ldcheck import load_config, load_colours, find_ldconfig_ldr_paths
from parse import read_ldraw
from testsuite import load_tests, check_model, problem_text, all_problem_types

app = Flask('LDCheck')

@app.route('/', methods = ['GET', 'POST'])
def web_front():
    test_suite = load_tests()
    if request.method == 'POST':
        # check if the post request has the file part
        if 'file' not in request.files or not request.files['file'].filename:
            return redirect(request.url)
        file = request.files['file']
        filename = file.filename
        config = load_config('ldcheck.cfg')
        for ldconfig_ldr_path in find_ldconfig_ldr_paths(config):
            with ldconfig_ldr_path.open() as ldconfig_ldr:
                load_colours(ldconfig_ldr)
        model = read_ldraw(
            file.stream,
            name = filename,
            ldraw_directories = config['libraries'],
        )
        report = check_model(model, test_suite)

        # Amend human-readable messages into the report
        for problem in report['problems']:
            object = model.body[problem.body_index]
            problem.message_str = problem_text(problem, test_suite)
            problem.ldraw_code = object.textual_representation()
        return render_template('webfront.html',
            report = report,
            name = filename,
            problem_types = all_problem_types(test_suite)
        )
    else:
        test_suite = load_tests()
        return render_template('webfront.html',
            report = None,
            name = None,
            problem_types = all_problem_types(test_suite)
        )

@app.route('/static/<path:path>')
def static_file(path):
    from flask import send_from_directory
    from os import path
    return send_from_directory(path.join('static', path))

if __name__ == '__main__':
    from argparse import ArgumentParser
    parser = ArgumentParser()
    parser.add_argument('-p', '--port', type = int, default = 5000)
    parser.add_argument('-d', '--debug', action = 'store_true')
    args = parser.parse_args()
    app.run(port = args.port, debug = args.debug)

mercurial