diff options
Diffstat (limited to 'qc_app')
-rw-r--r-- | qc_app/entry.py | 9 | ||||
-rw-r--r-- | qc_app/parse.py | 58 |
2 files changed, 57 insertions, 10 deletions
diff --git a/qc_app/entry.py b/qc_app/entry.py index e7ed294..31d64ca 100644 --- a/qc_app/entry.py +++ b/qc_app/entry.py @@ -11,8 +11,6 @@ from flask import ( render_template, current_app as app) -from quality_control.parsing import FileType - entrybp = Blueprint("entry", __name__) @entrybp.route("/", methods=["GET", "POST"]) @@ -40,12 +38,11 @@ def upload_file(): if errors: return render_template("index.html") - filetype = ( - FileType.AVERAGE if request.form["filetype"] == "average" - else FileType.STANDARD_ERROR) filename = secure_filename(text_file.filename) if not os.path.exists(app.config["UPLOAD_FOLDER"]): os.mkdir(app.config["UPLOAD_FOLDER"]) text_file.save(os.path.join(app.config["UPLOAD_FOLDER"], filename)) - return redirect(url_for("parse.parse_file", filename=filename)) + return redirect(url_for( + "parse.parse", filename=filename, + filetype=request.form["filetype"])) diff --git a/qc_app/parse.py b/qc_app/parse.py index 1d9e3c8..aa88260 100644 --- a/qc_app/parse.py +++ b/qc_app/parse.py @@ -1,14 +1,64 @@ """File parsing module""" -from flask import Blueprint +import os + +from flask import request, url_for, redirect, Blueprint, current_app as app + +from quality_control.errors import ParseError +from quality_control.parsing import ( + FileType, + parse_file, + strain_names, + parse_strains) parsebp = Blueprint("parse", __name__) -@parsebp.route("/parse/<filename>", methods=["GET"]) -def parse_file(filename): +@parsebp.route("/parse", methods=["GET"]) +def parse(): """Trigger file parsing""" - return f"STUB: Parse of '{filename}' ongoing!!!" + # TODO: figure out how to redirect with post + # TODO: figure out how to stat file and get: total number of lines + # TODO: Maybe implement external process to parse the files + errors = False + filename = request.args.get("filename") + filetype = request.args.get("filetype") + if filename is None: + flash("No file provided", "alert-error") + errors = True + + if filetype is None: + flash("No filetype provided", "alert-error") + errors = True + + filepath = os.path.join(app.config["UPLOAD_FOLDER"], filename) + if not os.path.exists(filepath): + flash("Selected file does not exist (any longer)", "alert-danger") + errors = True + + if errors: + return redirect(url_for("entry.index")) + + filetype = ( + FileType.AVERAGE if filetype == "average" else FileType.STANDARD_ERROR) + try: + parsed = parse_file( + filepath, filetype, strain_names(parse_strains("strains.csv"))) + for line in parsed: + pass + os.remove(filepath) + return redirect(url_for( + "parse.success", filename=filename, filetype=filetype)) + except ParseError as pe: + pe_dict = pe.args[0] + return redirect(url_for( + "parse.fail", filename = filename, filetype = filetype, + position = pe_dict["position"])) @parsebp.route("/success", methods=["GET"]) def success(): """Indicates success if parsing the file is successful""" return "STUB: Parse success!!!" + +@parsebp.route("/fail", methods=["GET"]) +def fail(): + """Indicates success if parsing the file is successful""" + return "STUB: Parse Failure!!!" |