aboutsummaryrefslogtreecommitdiff
path: root/qc_app/parse.py
blob: aa88260cbdb6b4802739d187edeb02df7f102a08 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
"""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!!!"