feat(sdk): desktopauto 0.5.3 银行卡/证件 OCR 增强、dingtalk 1.1.0、aitable 0.1.3

- desktopauto:cardocr 引擎/流水线/校验与 ocr 调整,sdk.json/pyproject/版本三处一致
- dingtalk:AI 表格读写与错误封装扩充
- aitable:client/cli/自测与打包配置更新
This commit is contained in:
2026-11-18 18:56:09 +08:00
parent 50fb138e1b
commit 39b768d0b7
26 changed files with 1043 additions and 93 deletions
+92
View File
@@ -225,6 +225,97 @@ def test_aitable() -> None:
"baseId 幂等")
# ==================== AI 表格:附件下载 ====================
def test_aitable_attachment() -> None:
import socketserver
import http.server as httpserver
import tempfile
import threading
value = [{"resourceId": "r1", "filename": "卡号面.jpg", "size": 12, "type": "image",
"url": "https://x.oss-cn/y.jpg?Expires=1794886717&Signature=abc"},
{"resourceId": "r2", "filename": "身份证.png", "size": 8, "type": "image",
"resourceUrl": "/core/api/resources/img/zz"}]
items = aitable.attachment_items(value)
_eq(len(items), 2, "attachment 字段值拆出 2 个附件")
_eq(items[0]["size"], 12, "声明大小保留")
_eq(items[1]["url"], "", "相对 resourceUrl 不当作可直连地址")
_eq(aitable.attachment_items(None), [], "空附件返回空列表")
_eq(aitable.attachment_items({"filename": "x"})[0]["url"], "", "单个 dict 兼容")
_eq(aitable.parse_expires(items[0]["url"]), 1794886717, "解析预签名 Expires")
_eq(aitable.parse_expires("https://x/y.jpg"), 0, "无 Expires 返回 0")
_eq(aitable.safe_filename('a/b:c*?"<>|.jpg'), "a_b_c_.jpg", "非法字符换下划线")
_eq(aitable.safe_filename("", ""), "file", "空名兜底")
_eq(len(aitable.safe_filename("x" * 200)), 120, "文件名限长 120")
# 本地 HTTP 服务模拟 OSS;不联网、不读生产凭证
png = b"\x89PNG\r\n\x1a\n" + b"\0" * 24
class _Handler(httpserver.BaseHTTPRequestHandler):
def do_GET(self):
if self.path.startswith("/ok"):
self.send_response(200)
self.send_header("content-type", "image/png")
self.send_header("content-length", str(len(png)))
self.end_headers()
self.wfile.write(png)
else:
self.send_response(403)
self.end_headers()
def log_message(self, *args):
pass
table = object.__new__(aitable.AiTable) # 只测下载,不初始化鉴权
table._http = http.HttpClient(retries=2, timeout=5, log=lambda m: None)
with socketserver.TCPServer(("127.0.0.1", 0), _Handler) as srv:
threading.Thread(target=srv.serve_forever, daemon=True).start()
base = f"http://127.0.0.1:{srv.server_address[1]}"
try:
with tempfile.TemporaryDirectory() as tmp:
res = table.download_attachment({"url": f"{base}/ok.png", "size": len(png)},
os.path.join(tmp, "a.png"))
_ok(res["ok"] and res["kind"] == "png" and res["size_match"] is True,
"下载成功 + 魔数/声明大小校验")
dup = table.download_attachment({"url": f"{base}/ok.png"},
os.path.join(tmp, "a.png"))
_eq(os.path.basename(dup["path"]), "a-2.png", "同名附件不覆盖")
_raises(aitable.AttachmentExpiredError,
lambda: table.download_attachment({"url": f"{base}/deny.png"},
os.path.join(tmp, "b.png")),
"403 抛 AttachmentExpiredError")
_raises(aitable.AttachmentExpiredError,
lambda: table.download_attachment(
{"url": f"{base}/ok.png?Expires=1000"},
os.path.join(tmp, "c.png")),
"Expires 已过期 → 不发请求直接报过期")
_raises(config.ConfigError,
lambda: table.download_attachment({"resourceUrl": "/x"},
os.path.join(tmp, "d.png")),
"无可直连 url 抛 ConfigError")
# 批量:单文件失败不中断,过期项打 expired,prefix 命名生效
batch = table.download_attachments(
[{"url": f"{base}/ok.png", "filename": "卡号面.jpg", "size": len(png)},
{"url": f"{base}/deny.png", "filename": "身份证.png"}],
os.path.join(tmp, "sub"), prefix="银行卡-", col="银行卡卡号面照片")
_eq(len(batch), 2, "批量下载 2 个附件")
_ok(batch[0]["ok"] and os.path.basename(batch[0]["path"]) == "银行卡-卡号面.jpg",
"批量成功项落盘且带 prefix")
_ok(batch[1]["ok"] is False and batch[1]["expired"] is True
and "403" in batch[1]["error"], "批量失败项标 expired 并带原因")
_eq(batch[1]["col"], "银行卡卡号面照片", "结果带字段名")
_eq(batch[0]["index"], 1, "结果带序号")
finally:
srv.shutdown()
srv.server_close()
# ==================== 媒体 ====================
def test_media() -> None:
@@ -264,6 +355,7 @@ def main() -> int:
("http", test_http),
("dws", test_dws),
("aitable", test_aitable),
("aitable_attachment", test_aitable_attachment),
("media", test_media),
("report", test_report),
]