文件
phonerail/.claude/k_dump_src.py

46 行
2.0 KiB
Python

#!/usr/bin/env python3
"""把 EasyEDA 工程的每一页原理图源码 dump 到 .claude/eda_src/。
源码是 EasyEDA Pro 的行式 DSL:每条记录 `{header}||{body}|`,换行分隔。
这是转 KiCad 的唯一权威输入,缓存 json 只作交叉校验。"""
import json, os, sys, time
sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))
import eda
ROOT = os.path.dirname(os.path.abspath(__file__))
OUT = os.path.join(ROOT, "eda_src")
PARTS = json.load(open(os.path.join(ROOT, "eda-parts.json"), encoding="utf-8"))
JS = '''
await eda.dmt_EditorControl.openDocument("%s");
await new Promise(r=>setTimeout(r,1500));
const info = await eda.sch_Document?.getCurrentSchematicPageInfo?.();
const s = await eda.sys_FileManager.getDocumentSource();
return { uuid: info?.uuid ?? null, src: s };
'''
def main():
os.makedirs(OUT, exist_ok=True)
pages = PARTS["pages"]
manifest = {}
for name, uuid in pages.items():
r = eda.run(JS % uuid, timeout=180)
if not r.get("success"):
print(f" !! {name} 失败: {json.dumps(r, ensure_ascii=False)[:300]}")
sys.exit(1)
res = r["result"]
# 桥接打的是"当前打开的那一页",必须确认真的切过去了(skill §9l)
if res.get("uuid") and res["uuid"] != uuid:
print(f" !! {name} 切页没生效: 期望 {uuid} 实得 {res['uuid']}")
sys.exit(1)
src = res["src"]
path = os.path.join(OUT, f"{name}.esrc")
open(path, "w", encoding="utf-8", newline="").write(src)
manifest[name] = {"uuid": uuid, "bytes": len(src), "lines": src.count("\n") + 1}
print(f" {name:12s} {uuid} {len(src):>8,d} B {manifest[name]['lines']:>5,d} 行")
json.dump(manifest, open(os.path.join(OUT, "_manifest.json"), "w", encoding="utf-8"),
ensure_ascii=False, indent=1)
print(f"\n共 {len(manifest)} 页,{sum(m['bytes'] for m in manifest.values()):,d} B -> {OUT}")
if __name__ == "__main__":
main()