Spaces:
Running
Running
File size: 8,642 Bytes
117344d f4e0360 117344d 02a65a5 117344d 02a65a5 117344d f4e0360 117344d f4e0360 117344d f4e0360 117344d f4e0360 117344d c7010cf 117344d c7010cf 117344d 02a65a5 117344d | 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 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 | """Bounded CSV and XLSX parsing for temporary upload sessions."""
from __future__ import annotations
from dataclasses import dataclass
from io import BytesIO, StringIO
from pathlib import Path
from typing import Any
from xml.etree.ElementTree import ParseError
from zipfile import BadZipFile, ZipFile
import pandas as pd
from openpyxl import load_workbook
from openpyxl.utils.exceptions import InvalidFileException
from .errors import UploadError
MEBIBYTE = 1024 * 1024
@dataclass(frozen=True)
class ParseLimits:
max_upload_bytes: int = 10 * MEBIBYTE
max_rows: int = 100_000
max_columns: int = 200
max_dataframe_bytes: int = 100 * MEBIBYTE
max_archive_uncompressed_bytes: int = 100 * MEBIBYTE
@property
def max_upload_mebibytes(self) -> int:
return max(1, self.max_upload_bytes // MEBIBYTE)
@dataclass
class ParsedTable:
filename: str
sheet_name: str | None
display_headers: list[str]
column_ids: list[str]
dataframe: pd.DataFrame
memory_bytes: int
def _invalid_file(message: str) -> UploadError:
return UploadError(422, "invalid_file", message)
def upload_size_error_message(limits: ParseLimits) -> str:
return (
"Please choose a spreadsheet no larger than "
f"{limits.max_upload_mebibytes} MB."
)
def _display_header(value: Any) -> str:
if value is None or pd.isna(value):
return ""
return str(value)
def _is_populated(value: Any) -> bool:
if value is None:
return False
if isinstance(value, str):
return bool(value.strip())
try:
return not bool(pd.isna(value))
except (TypeError, ValueError):
return True
def _build_table(
*,
filename: str,
sheet_name: str | None,
headers: list[Any],
rows: list[list[Any]] | pd.DataFrame,
limits: ParseLimits,
) -> ParsedTable:
display_headers = [_display_header(value) for value in headers]
if not display_headers:
raise UploadError(422, "empty_table", "The spreadsheet does not contain any columns.")
column_ids = [f"column_{position + 1}" for position in range(len(display_headers))]
dataframe = (
rows.copy()
if isinstance(rows, pd.DataFrame)
else pd.DataFrame(rows, columns=column_ids)
)
dataframe.columns = column_ids
dataframe.reset_index(drop=True, inplace=True)
if dataframe.empty:
raise UploadError(422, "empty_table", "The spreadsheet does not contain any data rows.")
if not any(
_is_populated(value)
for row in dataframe.itertuples(index=False, name=None)
for value in row
):
raise UploadError(422, "empty_table", "The spreadsheet does not contain any data rows.")
if len(dataframe) > limits.max_rows or len(dataframe.columns) > limits.max_columns:
raise UploadError(
413,
"table_too_large",
(
f"Please use a table with at most {limits.max_rows:,} rows "
f"and {limits.max_columns:,} columns."
),
)
memory_bytes = int(dataframe.memory_usage(index=True, deep=True).sum())
if memory_bytes > limits.max_dataframe_bytes:
raise UploadError(
413,
"table_too_large",
"This spreadsheet expands to more data than a temporary session can safely hold.",
)
return ParsedTable(
filename=filename,
sheet_name=sheet_name,
display_headers=display_headers,
column_ids=column_ids,
dataframe=dataframe,
memory_bytes=memory_bytes,
)
def _parse_csv(filename: str, payload: bytes, limits: ParseLimits) -> ParsedTable:
try:
text = payload.decode("utf-8-sig")
except UnicodeDecodeError:
try:
text = payload.decode("cp1252")
except UnicodeDecodeError as exc: # pragma: no cover - cp1252 maps every byte
raise _invalid_file("The CSV text encoding could not be read.") from exc
try:
raw = pd.read_csv(
StringIO(text),
header=None,
dtype=str,
keep_default_na=False,
na_filter=False,
sep=",",
engine="python",
on_bad_lines="error",
skip_blank_lines=False,
)
except (pd.errors.ParserError, UnicodeError, ValueError) as exc:
raise _invalid_file("The CSV file is malformed or could not be read.") from exc
if raw.empty:
raise UploadError(422, "empty_table", "The spreadsheet does not contain any data rows.")
headers = raw.iloc[0].tolist()
data = raw.iloc[1:].copy()
return _build_table(
filename=filename,
sheet_name=None,
headers=headers,
rows=data,
limits=limits,
)
def _inspect_xlsx(payload: bytes, limits: ParseLimits) -> None:
try:
with ZipFile(BytesIO(payload)) as archive:
names = set(archive.namelist())
if {"EncryptionInfo", "EncryptedPackage"} & names:
raise _invalid_file("Encrypted Excel workbooks are not supported.")
expanded_bytes = sum(member.file_size for member in archive.infolist())
if expanded_bytes > limits.max_archive_uncompressed_bytes:
raise UploadError(
413,
"file_too_large",
"This Excel workbook expands beyond the safe processing limit.",
)
except BadZipFile as exc:
raise _invalid_file("The Excel workbook is corrupt, encrypted, or unreadable.") from exc
def _parse_xlsx(filename: str, payload: bytes, limits: ParseLimits) -> ParsedTable:
_inspect_xlsx(payload, limits)
try:
workbook = load_workbook(BytesIO(payload), read_only=True, data_only=True)
except (
BadZipFile,
EOFError,
InvalidFileException,
KeyError,
OSError,
ParseError,
ValueError,
) as exc:
raise _invalid_file("The Excel workbook is corrupt, encrypted, or unreadable.") from exc
try:
visible_sheet = next(
(sheet for sheet in workbook.worksheets if sheet.sheet_state == "visible"),
None,
)
if visible_sheet is None:
raise _invalid_file("The Excel workbook does not contain a visible worksheet.")
if (
max((visible_sheet.max_row or 0) - 1, 0) > limits.max_rows
or (visible_sheet.max_column or 0) > limits.max_columns
):
raise UploadError(
413,
"table_too_large",
(
f"Please use a table with at most {limits.max_rows:,} rows "
f"and {limits.max_columns:,} columns."
),
)
row_iterator = visible_sheet.iter_rows(values_only=True)
try:
headers = list(next(row_iterator))
except StopIteration:
raise UploadError(422, "empty_table", "The spreadsheet does not contain any data rows.")
rows: list[list[Any]] = []
for row_number, row in enumerate(row_iterator, start=1):
if row_number > limits.max_rows or len(row) > limits.max_columns:
raise UploadError(
413,
"table_too_large",
(
f"Please use a table with at most {limits.max_rows:,} rows "
f"and {limits.max_columns:,} columns."
),
)
rows.append(list(row))
return _build_table(
filename=filename,
sheet_name=visible_sheet.title,
headers=headers,
rows=rows,
limits=limits,
)
finally:
workbook.close()
def parse_upload(
filename: str,
payload: bytes,
*,
limits: ParseLimits | None = None,
) -> ParsedTable:
selected_limits = limits or ParseLimits()
suffix = Path(filename).suffix.lower()
if suffix not in {".csv", ".xlsx"}:
raise UploadError(
415,
"unsupported_file_type",
"Please choose a CSV or XLSX spreadsheet.",
)
if not payload:
raise UploadError(422, "empty_file", "The selected file is empty.")
if len(payload) > selected_limits.max_upload_bytes:
raise UploadError(
413,
"file_too_large",
upload_size_error_message(selected_limits),
)
if suffix == ".csv":
return _parse_csv(filename, payload, selected_limits)
return _parse_xlsx(filename, payload, selected_limits)
|