diff options
Diffstat (limited to 'qc_app/parse.py')
-rw-r--r-- | qc_app/parse.py | 58 |
1 files changed, 54 insertions, 4 deletions
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!!!" |