From e13d63bf6bd716e0d78ce62e8c755e8e0b7a1588 Mon Sep 17 00:00:00 2001 From: Alan Orth Date: Fri, 12 Mar 2021 19:14:49 +0200 Subject: [PATCH] Major refactor Refactor the templates to include a header, use csv-metadata-quality in a subshell instead of trying to import and pass args to it (which I actually managed to do, but then trying to capture the output was an issue), and use ansi2html to prepare the CLI output as the HTML. --- main.py | 29 ++++++++++++++++++++++++++++- pyproject.toml | 1 + source/scss/bootstrap.scss | 2 +- templates/header.html | 7 +++++++ templates/index.html | 4 ++-- templates/process.html | 13 +++++++++++-- 6 files changed, 50 insertions(+), 6 deletions(-) create mode 100644 templates/header.html diff --git a/main.py b/main.py index 93d803a..423cc38 100644 --- a/main.py +++ b/main.py @@ -1,5 +1,8 @@ import os +import subprocess +import sys +from ansi2html import Ansi2HTMLConverter from csv_metadata_quality.version import VERSION as cli_version from flask import Flask, abort, redirect, render_template, request, url_for from werkzeug.utils import secure_filename @@ -32,9 +35,33 @@ def upload_file(): return "No file selected" +# TODO: probably use a base64- and URL-encoded version of the filename here so +# we can allow results to be saved and shared? @app.route("/process/") def process_file(filename): - return render_template("process.html", cli_version=cli_version, filename=filename) + # do we need to use secure_filename again here? + input_file = os.path.join(app.config["UPLOAD_PATH"], filename) + # TODO: write an output file based on the input file name + output_file = os.path.join(app.config["UPLOAD_PATH"], "test.csv") + + sys.argv = ["", "-i", input_file, "-o", output_file] + + # run subprocess and capture output as UTF-8 so we get a string instead of + # bytes for ansi2html + results = subprocess.run( + ["csv-metadata-quality", "-i", input_file, "-o", output_file], + capture_output=True, + encoding="UTF-8", + ) + # convert the output to HTML using ansi2html + conv = Ansi2HTMLConverter() + html = conv.convert(results.stdout) + return render_template( + "process.html", cli_version=cli_version, filename=filename, stdout=html + ) + + # I should remember this Flask-specific way to send files to the client + # return send_from_directory(app.config["UPLOAD_FOLDER"], filename) if __name__ == "__main__": diff --git a/pyproject.toml b/pyproject.toml index be62b5e..8e957e3 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -9,6 +9,7 @@ license = "AGPL-3.0-only" python = "^3.7.1" csv-metadata-quality = {git = "https://github.com/ilri/csv-metadata-quality.git", rev = "web"} Flask = "^1.1.2" +ansi2html = "^1.6.0" [tool.poetry.dev-dependencies] black = "^20.8b1" diff --git a/source/scss/bootstrap.scss b/source/scss/bootstrap.scss index 9fed3ee..4d727ec 100644 --- a/source/scss/bootstrap.scss +++ b/source/scss/bootstrap.scss @@ -26,7 +26,7 @@ //@import "../../node_modules/bootstrap/scss/dropdown"; //@import "../../node_modules/bootstrap/scss/button-group"; //@import "../../node_modules/bootstrap/scss/nav"; -//@import "../../node_modules/bootstrap/scss/navbar"; +@import "../../node_modules/bootstrap/scss/navbar"; //@import "../../node_modules/bootstrap/scss/card"; //@import "../../node_modules/bootstrap/scss/accordion"; //@import "../../node_modules/bootstrap/scss/breadcrumb"; diff --git a/templates/header.html b/templates/header.html new file mode 100644 index 0000000..30b0447 --- /dev/null +++ b/templates/header.html @@ -0,0 +1,7 @@ +
+ +
diff --git a/templates/index.html b/templates/index.html index a34d8fd..917db6c 100644 --- a/templates/index.html +++ b/templates/index.html @@ -2,9 +2,9 @@ {% include 'head.html' %} + {% include 'header.html' %}
-
-

DSpace CSV Metadata Quality Checker

+

The DSpace CSV Metadata Quality Checker is a pipeline of sanity checks and automated fixes for a number of common issues in metadata files.

Select a CSV file to process.

diff --git a/templates/process.html b/templates/process.html index 215198e..551f9e1 100644 --- a/templates/process.html +++ b/templates/process.html @@ -2,13 +2,22 @@ {% include 'head.html' %} + {% include 'header.html' %}
-

DSpace CSV Metadata Quality Checker

-

Processing {{ filename }}

+

Results

+

Processing {{ filename }}.

+
+
+
+ {{ stdout | safe }} +
+
+
+ {% include 'footer.html' %}