You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

49 lines
2.0 KiB

2 days ago
# -*- coding: utf-8 -*-
"""诊断 A4 文档响应表“说明”列的真实质量,不只看表头。"""
import re, json
from pathlib import Path
DOCS = Path(r"D:\code\crm-api-docs\A4 客户管理")
PLACEHOLDER_DESCS = {
"无业务返回数据",
"标量业务返回值",
"二进制文件流;成功时不使用 JSON 信封",
"返回对象;源码未找到可静态展开的字段",
"分页内容,元素结构如下",
"总条数", "每页条数", "当前页", "总页数", "是否为空",
}
stats = {"total_rows": 0, "empty": 0, "field_name_copy": 0, "placeholder": 0, "meaningful": 0}
per_file = []
for f in sorted(DOCS.rglob("*.bru")):
if f.name == "folder.bru":
continue
text = f.read_text(encoding="utf-8-sig")
m = re.search(r'## 响应 data 结构\s*\n(.*?)(?=\n ## |\n\}\s*$|\Z)', text, re.S)
if not m:
continue
table = m.group(1)
file_bad = 0
file_total = 0
for line in table.splitlines():
rm = re.match(r'\s*\|\s*([^|]+?)\s*\|\s*([^|]+?)\s*\|\s*([^|]*?)\s*\|$', line)
if not rm:
continue
field, ftype, desc = (g.strip() for g in rm.groups())
if field in ("字段",) or set(field) <= {"-"}:
continue
stats["total_rows"] += 1
file_total += 1
if desc == "":
stats["empty"] += 1; file_bad += 1
elif desc == field or desc == field.replace("content[].", "").replace("[].", ""):
stats["field_name_copy"] += 1; file_bad += 1
elif desc in PLACEHOLDER_DESCS:
stats["placeholder"] += 1; file_bad += 1
else:
stats["meaningful"] += 1
if file_total and file_bad == file_total:
per_file.append(("全行糊弄", f.relative_to(DOCS).as_posix()))
elif file_bad and file_bad >= file_total * 0.5:
per_file.append(("半数糊弄", f.relative_to(DOCS).as_posix()))
print(json.dumps({"stats": stats, "bad_files_count": len(per_file), "bad_files_sample": per_file[:20]}, ensure_ascii=False, indent=2))