Skip to content

Commit 9218f62

Browse files
committed
Improve handling of bad encodings
1 parent bf25f65 commit 9218f62

File tree

2 files changed

+22
-22
lines changed

2 files changed

+22
-22
lines changed

alldata/bblab_site/tools/isoforms_plot/run_isoforms.py

Lines changed: 15 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,8 @@
44
sys.path.append(os.environ.get("BBLAB_UTIL_PATH", "fail"))
55

66
import io
7-
import html
87
import traceback
98
from pathlib import Path
10-
from typing import Tuple
119

1210
# Add the parent directory to sys.path to enable isoforms_plot imports
1311
CURDIR = Path(__file__).parent.absolute()
@@ -20,19 +18,16 @@
2018

2119

2220
# -- Utility helpers -------------------------------------------------------
23-
def _read_csv_text(csv_data) -> Tuple[str, str]:
21+
def _read_csv_text(csv_data) -> str:
2422
"""Return CSV text or (None, error_message)."""
25-
try:
26-
if hasattr(csv_data, "read"):
27-
raw = csv_data.read()
28-
if isinstance(raw, bytes):
29-
raw = raw.decode("utf8")
30-
return raw, ""
31-
# allow path-like input
32-
text = Path(csv_data).read_text(encoding="utf8")
33-
return text, ""
34-
except Exception as exc:
35-
return "", f"Could not read input CSV: {html.escape(str(exc))}"
23+
if hasattr(csv_data, "read"):
24+
raw = csv_data.read()
25+
if isinstance(raw, bytes):
26+
raw = raw.decode("utf8")
27+
return raw
28+
# allow path-like input
29+
text = Path(csv_data).read_text(encoding="utf8")
30+
return text
3631

3732

3833
def get_default_csv() -> str:
@@ -46,7 +41,7 @@ def get_default_csv() -> str:
4641
# -- Main runner ----------------------------------------------------------
4742

4843

49-
def run(csv_data):
44+
def run(csv_file):
5045
"""Orchestrate plotting. Returns dict with results.
5146
5247
Returns:
@@ -57,17 +52,17 @@ def run(csv_data):
5752
- 'error_details': str (if not success, traceback)
5853
"""
5954

60-
# Read CSV text
61-
csv_text, read_err = _read_csv_text(csv_data)
62-
if read_err:
63-
return {"success": False, "error_message": read_err, "error_details": None}
64-
6555
# Try to generate plot - all validation happens in the plotter
6656
try:
6757
import isoforms_plot.parser as parser
6858
import isoforms_plot.compiler as compiler
6959
import isoforms_plot.plotter as plotter
7060

61+
# Read CSV text
62+
file_bytes = csv_file.read()
63+
csv_data = file_bytes.decode("utf-8")
64+
csv_text = _read_csv_text(csv_data)
65+
7166
# Parse
7267
input_stream = io.StringIO(csv_text)
7368
parsed = parser.parse(input_stream)

alldata/bblab_site/tools/isoforms_plot/views.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,13 @@ def results(request):
5252
if request.method == "POST":
5353
from . import run_isoforms
5454

55-
csv_data = StringIO(request.FILES["file"].read().decode("utf-8"))
56-
result = run_isoforms.run(csv_data)
55+
try:
56+
csv_file = request.FILES["file"]
57+
except KeyError:
58+
context = _build_context(request, show_results=False)
59+
return render(request, "isoforms_plot/index.html", context)
60+
61+
result = run_isoforms.run(csv_file)
5762

5863
context = _build_context(request, show_results=True, result=result)
5964
return render(request, "isoforms_plot/index.html", context)

0 commit comments

Comments
 (0)