webfront.py

changeset 147
bec55b021ae7
parent 145
fde18c4d6784
child 151
e4401bf4a387
--- a/webfront.py	Thu Aug 26 19:16:25 2021 +0300
+++ b/webfront.py	Thu Aug 26 19:36:44 2021 +0300
@@ -1,29 +1,50 @@
 #!/usr/bin/env python3
 from flask import Flask, render_template, redirect, request
-from ldcheck import load_config, load_colours, find_ldconfig_ldr_paths
+from ldcheck import appname, version, version_string
 from parse import read_ldraw
 from testsuite import load_tests, check_model, problem_text, all_problem_types
 
-app = Flask('LDCheck')
+app = Flask(appname)
+
+from ldcheck import LDrawContext
+try:
+    context = LDrawContext()
+except RuntimeError as error:
+    from sys import stderr, exit
+    print('error:', str(error), file = stderr)
+    exit(1)
+
+def format_report_html(report, model, test_suite):
+    messages = []
+    for problem in report['problems']:
+        ldraw_code = model.body[problem.body_index].textual_representation()
+        message = str.format(
+            '<li class="{problem_type}">{model_name}:{line_number}:'
+            '{problem_type}: {message}<br />{ldraw_code}</li>',
+            model_name = model.name,
+            line_number = problem.line_number,
+            problem_type = problem.severity,
+            message = problem_text(problem, test_suite),
+            ldraw_code = ldraw_code,
+        )
+        messages.append(message)
+    return '\n'.join(messages)
 
 @app.route('/', methods = ['GET', 'POST'])
 def web_front():
     test_suite = load_tests()
+    common_args = {
+        'appname': appname,
+        'version': version_string,
+        'is_debug': max(version) == 9999,
+    }
     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'],
-        )
+        model = read_ldraw(file.stream, name = filename, context = context)
         report = check_model(model, test_suite)
 
         # Amend human-readable messages into the report
@@ -34,14 +55,16 @@
         return render_template('webfront.html',
             report = report,
             name = filename,
-            problem_types = all_problem_types(test_suite)
+            problem_types = all_problem_types(test_suite),
+            **common_args,
         )
     else:
         test_suite = load_tests()
         return render_template('webfront.html',
             report = None,
             name = None,
-            problem_types = all_problem_types(test_suite)
+            problem_types = all_problem_types(test_suite),
+            **common_args,
         )
 
 @app.route('/static/<path:path>')

mercurial