feat(preflight): check 报告改为 xlsx,冻结首行 + 筛选(默认只看 PREP/BLOCKED)

- 表头冻结 A2,AutoFilter 覆盖全表,verdict 列默认勾选 PREP/BLOCKED 并隐藏 READY 行
- BLOCKED 橙红底、PREP 黄底,path 列带 file:// 超链接
- 若报告全部为 READY 则不隐藏任何行,避免打开是空表
- 无 openpyxl 时回退 CSV;requirements.txt 增加 openpyxl
This commit is contained in:
ali
2026-11-19 02:05:47 +08:00
parent 565d34c2be
commit 4a81af5a88
2 changed files with 89 additions and 16 deletions
+87 -15
View File
@@ -304,32 +304,104 @@ class PreflightService:
# ------------------------------------------------------------------ 报告 # ------------------------------------------------------------------ 报告
HEADERS = ["path", "verdict", "codec", "height", "mbps", "dv_profile",
"audio_codecs", "sub_codecs", "actions", "reasons"]
@classmethod
def _report_rows(cls, results: list) -> list:
order = {BLOCKED: 0, PREP: 1, READY: 2}
rows = []
for path, r in sorted(results, key=lambda x: (order.get(x[1].verdict, 9), str(x[0]))):
i = r.info
audio = "/".join(f"{a['codec']}{a['channels']}" for a in i.get("audio", []))
subs = "/".join(sorted({s["codec"] for s in i.get("subs", [])}))
rows.append((path, r, [
str(path), r.verdict, i.get("codec", ""), i.get("height", ""),
i.get("mbps", ""), i.get("dv_profile") or "",
audio, subs, "/".join(r.actions), " | ".join(r.reasons),
]))
return rows
@classmethod @classmethod
def write_report(cls, root: Path, results: list, only_issues: bool = False) -> Path | None: def write_report(cls, root: Path, results: list, only_issues: bool = False) -> Path | None:
"""results: [(path, PreflightResult)],输出 root/preflight_report.csv""" """results: [(path, PreflightResult)],输出 root/preflight_report.xlsx(无 openpyxl 时退回 csv)。"""
if not config.PREFLIGHT_REPORT or not results: if not config.PREFLIGHT_REPORT or not results:
return None return None
if only_issues: if only_issues:
results = [(p, r) for p, r in results if r.verdict != READY] results = [(p, r) for p, r in results if r.verdict != READY]
if not results: if not results:
return None return None
report = root / "preflight_report.csv"
header = "path,verdict,codec,height,mbps,dv_profile,audio_codecs,sub_codecs,actions,reasons\n" rows = cls._report_rows(results)
lines = [header]
for path, r in results: try:
i = r.info from openpyxl import Workbook
audio = "/".join(f"{a['codec']}{a['channels']}" for a in i.get("audio", [])) from openpyxl.styles import Alignment, Font, PatternFill
subs = "/".join(sorted({s["codec"] for s in i.get("subs", [])})) from openpyxl.utils import get_column_letter
row = [ except ImportError:
str(path), r.verdict, i.get("codec", ""), i.get("height", ""), log.warning("⚠️ 未安装 openpyxl,报告退回 CSV(pip install openpyxl 可启用 xlsx)")
i.get("mbps", ""), i.get("dv_profile") or "", return cls._write_csv(root, rows)
audio, subs, "/".join(r.actions), " | ".join(r.reasons),
] wb = Workbook()
lines.append(",".join('"' + str(c).replace('"', '""') + '"' for c in row) + "\n") ws = wb.active
report.write_text("".join(lines), "utf-8") ws.title = "preflight"
ws.append(cls.HEADERS)
header_fill = PatternFill("solid", fgColor="305496")
for cell in ws[1]:
cell.font = Font(bold=True, color="FFFFFF")
cell.fill = header_fill
cell.alignment = Alignment(vertical="center")
fills = {
BLOCKED: PatternFill("solid", fgColor="F8CBAD"),
PREP: PatternFill("solid", fgColor="FFE699"),
}
for path, r, values in rows:
ws.append(values)
row_idx = ws.max_row
if r.verdict in fills:
for cell in ws[row_idx]:
cell.fill = fills[r.verdict]
try:
link = ws.cell(row=row_idx, column=1)
link.hyperlink = path.as_uri()
link.style = "Hyperlink"
except (ValueError, OSError):
pass
last_col = get_column_letter(len(cls.HEADERS))
ws.auto_filter.ref = f"A1:{last_col}{ws.max_row}"
# verdict 列(0-based colId=1)默认只勾选 PREP/BLOCKED,并把 READY 行隐藏;
# 若整份报告全是 READY,就保持全部可见,避免打开是空表
has_issues = any(r.verdict != READY for _, r, _ in rows)
if has_issues:
ws.auto_filter.add_filter_column(1, [PREP, BLOCKED])
for offset, (_, r, _) in enumerate(rows):
if r.verdict == READY:
ws.row_dimensions[offset + 2].hidden = True
ws.freeze_panes = "A2"
widths = {"A": 80, "B": 10, "C": 11, "D": 8, "E": 9, "F": 10,
"G": 26, "H": 32, "I": 22, "J": 60}
for col, width in widths.items():
ws.column_dimensions[col].width = width
report = root / "preflight_report.xlsx"
wb.save(report)
log.info(f"📄 兼容性报告: {report}") log.info(f"📄 兼容性报告: {report}")
return report return report
@classmethod
def _write_csv(cls, root: Path, rows: list) -> Path:
report = root / "preflight_report.csv"
lines = [",".join(cls.HEADERS) + "\n"]
for _, _, values in rows:
lines.append(",".join('"' + str(v).replace('"', '""') + '"' for v in values) + "\n")
report.write_text("".join(lines), "utf-8")
log.info(f"📄 兼容性报告(CSV): {report}")
return report
@classmethod @classmethod
def scan(cls, root: Path) -> list: def scan(cls, root: Path) -> list:
"""check 模式:只体检不动文件。""" """check 模式:只体检不动文件。"""
+2 -1
View File
@@ -1,3 +1,4 @@
pysubs2 pysubs2
requests requests
watchdog watchdog
openpyxl