feat:增加分析进度接口

This commit is contained in:
2026-10-10 07:26:24 -07:00
parent e3699d1a30
commit cd5a2b4d8f
15 changed files with 632 additions and 143 deletions
+23 -5
View File
@@ -8,7 +8,14 @@ import re
from dataclasses import dataclass, field
from typing import Any
from app.datafiles.base import MAX_TABLE_ROWS, brief, rows_exceeded, where
from app.datafiles.base import (
MAX_TABLE_ROWS,
Reporter,
brief,
no_report,
rows_exceeded,
where,
)
from app.datafiles.columns import as_text, classify
from app.datafiles.readers import Grid
from app.schemas.data_file import ParseWarning
@@ -60,13 +67,21 @@ class _Piece:
warnings: list[ParseWarning]
def split_tables(grid: Grid) -> tuple[list[RawTable], list[ParseWarning]]:
"""返回识别出的表和不属于任何表的警告(被丢弃的块)"""
def split_tables(
grid: Grid, report: Reporter = no_report
) -> tuple[list[RawTable], list[ParseWarning]]:
"""返回识别出的表和不属于任何表的警告(被丢弃的块)
几步都要扫一遍所有行,每步结束报告一次进度,比例来自大表上的实测耗时
"""
rows = _normalize(grid.rows)
report(0.25)
sheet = grid.sheet
pieces: list[_Piece] = []
dropped: list[ParseWarning] = []
for block in _blocks(rows):
blocks = _blocks(rows)
report(0.3)
for number, block in enumerate(blocks, start=1):
columns = [
c for c in range(len(rows[0])) if any(rows[r][c] is not None for r in block)
]
@@ -88,10 +103,12 @@ def split_tables(grid: Grid) -> tuple[list[RawTable], list[ParseWarning]]:
)
continue
pieces.append(_read_block(rows, block, columns, sheet))
report(0.3 + 0.25 * number / len(blocks))
tables = []
for piece in pieces:
for number, piece in enumerate(pieces, start=1):
table = _finish(rows, piece, sheet)
report(0.55 + 0.45 * number / len(pieces))
if table is None:
dropped.append(
ParseWarning(
@@ -101,6 +118,7 @@ def split_tables(grid: Grid) -> tuple[list[RawTable], list[ParseWarning]]:
)
else:
tables.append(table)
report(1.0)
if len(tables) > 1:
for part, table in enumerate(tables, start=1):
table.part = part