"""File parsing module""" 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(): """Trigger file parsing""" # 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!!!"