added a simple web frontend

Tue, 23 Jan 2018 14:09:52 +0200

author
Santeri Piippo
date
Tue, 23 Jan 2018 14:09:52 +0200
changeset 29
db6ca177c6c4
parent 28
5933250813a3
child 30
0d9ca37901ed

added a simple web frontend

parse.py file | annotate | diff | comparison | revisions
testsuite.py file | annotate | diff | comparison | revisions
webfront.py file | annotate | diff | comparison | revisions
--- a/parse.py	Tue Jan 23 14:09:44 2018 +0200
+++ b/parse.py	Tue Jan 23 14:09:52 2018 +0200
@@ -7,6 +7,8 @@
     pass
 
 def parse_ldraw_code(line):
+    if isinstance(line, bytes):
+        line = line.decode()
     line = line.strip()
     if not line:
         return linetypes.EmptyLine()
--- a/testsuite.py	Tue Jan 23 14:09:44 2018 +0200
+++ b/testsuite.py	Tue Jan 23 14:09:52 2018 +0200
@@ -84,14 +84,36 @@
             report.append(problem)
     return report
 
+def problem_text(problem, test_suite):
+    message = test_suite['messages'][problem['name']]
+    if callable(message):
+        message = message(**problem['args'])
+    return message
+
+def format_report_html(report, model, test_suite):
+    result = []
+    for problem in report:
+        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['type'],
+            message = problem_text(problem, test_suite),
+            ldraw_code = ldraw_code,
+        )
+        result.append((problem['line-number'], message))
+    return '\n'.join(
+        problem[1]
+        for problem in sorted(result)
+    )
+
 def format_report(report, model, test_suite):
     import colorama
     colorama.init()
     result = []
     for problem in report:
-        problem_text = test_suite['messages'][problem['name']]
-        if callable(problem_text):
-            problem_text = problem_text(**problem['args'])
         if problem['type'] == 'error':
             text_colour = colorama.Fore.LIGHTRED_EX
         elif problem['type'] == 'warning':
@@ -106,7 +128,7 @@
             model_name = model.name,
             line_number = problem['line-number'],
             problem_type = problem['type'],
-            message = problem_text,
+            message = problem_text(problem, test_suite),
             colour_reset = colorama.Fore.RESET,
             ldraw_code = ldraw_code,
         )
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/webfront.py	Tue Jan 23 14:09:52 2018 +0200
@@ -0,0 +1,41 @@
+from flask import Flask, render_template, redirect, request
+from ldcheck import load_config, load_colours, find_ldconfig_ldr_paths
+from ldcheck import read_ldraw
+from testsuite import load_tests, check_model, format_report_html
+
+app = Flask('LDCheck')
+
+@app.route('/', methods = ['GET', 'POST'])
+def web_front():
+    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']
+        print(type(file))
+        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 = file.filename,
+            config = config,
+        )
+        test_suite = load_tests()
+        report = check_model(model, test_suite)
+        return str.format(
+            '<!doctype html><html><body><ul>{report}</ul></body></html>',
+            report = format_report_html(report, model, test_suite)
+        )
+    return '''
+    <!doctype html>
+    <title>Upload new File</title>
+    <h1>Upload new File</h1>
+    <form method=post enctype=multipart/form-data>
+      <p><input type=file name=file>
+         <input type=submit value=Upload>
+    </form>
+    '''
+
+app.run()

mercurial