From a5477c59452cdb01ab536f11eb5ed6fab015f3af Mon Sep 17 00:00:00 2001 From: Frederick Muriuki Muriithi Date: Mon, 25 Apr 2022 16:22:33 +0300 Subject: Call parsing function. Fix a few issues. * qc_app/entry.py: Pass filetype onward to parsing endpoint * qc_app/parse.py: Call the function(s) necessary to parse a file * quality_control/errors.py: Fix argument passing to super class --- qc_app/entry.py | 9 +++----- qc_app/parse.py | 58 +++++++++++++++++++++++++++++++++++++++++++---- quality_control/errors.py | 8 +++---- 3 files changed, 61 insertions(+), 14 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/", 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!!!" diff --git a/quality_control/errors.py b/quality_control/errors.py index 0802159..29a38f9 100644 --- a/quality_control/errors.py +++ b/quality_control/errors.py @@ -4,21 +4,21 @@ class InvalidCellValue(Exception): """Raised when a function encounters an invalid value""" def __init__(self, *args): - super().__init__(self, *args) + super().__init__(*args) class InvalidHeaderValue(Exception): """Raised when a header contains values not in the reference file.""" def __init__(self, *args): - super().__init__(self, *args) + super().__init__(*args) class DuplicateHeader(Exception): """Raised when a header contains 2 similar headers.""" def __init__(self, *args): - super().__init__(self, *args) + super().__init__(*args) class ParseError(Exception): """Raised if any of the above exceptions are raised""" def __init__(self, *args): - super().__init__(self, *args) + super().__init__(*args) -- cgit v1.2.3