main
John Lauer
Preserve board29, component assets and scaffold-fit correction candidate with reviewed footage
c54169e
"""Explicit saved-file field edit; preserves all unrelated native board text."""
import re,shutil,json,hashlib
from pathlib import Path
p=Path(__file__).resolve().parents[1]
def blocks(text,kind):
for m in re.finditer(r'\('+kind+r'\s',text):
start=m.start();depth=0;quoted=False;escape=False
for i in range(start,len(text)):
c=text[i]
if quoted:
if escape:escape=False
elif c=='\\':escape=True
elif c=='"':quoted=False
elif c=='"':quoted=True
elif c=='(':depth+=1
elif c==')':
depth-=1
if depth==0:yield start,i+1,text[start:i+1];break
src=p/'checkpoints/usb-live-before-fields/adom-custom-electronic-load.kicad_pcb';s=src.read_text();candidate=(p/'checkpoints/usb-inline/run-v6/board-routed.kicad_pcb').read_text();replacements={}
for _,_,f in blocks(candidate,'footprint'):
ref=re.search(r'\(property "Reference" "([^"]+)"',f).group(1)
if ref in ['U10','R31','R32']:replacements[ref]=next(v for _,_,v in blocks(f,'property')if v.startswith('(property "Reference"'))
edits=[]
for a,b,f in blocks(s,'footprint'):
ref=re.search(r'\(property "Reference" "([^"]+)"',f).group(1)
if ref not in replacements:continue
for x,y,v in reversed(list(blocks(f,'property'))):
if v.startswith('(property "Reference"'):f=f[:x]+replacements[ref]+f[y:]
elif ref in ['R31','R32']and v.startswith('(property "Value"'):f=f[:x]+v.replace('"22"','"0"',1)+f[y:]
elif ref in ['R31','R32']and v.startswith('(property "MPN"'):f=f[:x]+v.replace('CR0402-FX-22R0GLF','CQ02WGF0000TCE')+f[y:]
edits.append((a,b,f))
assert len(edits)==3
for a,b,f in reversed(edits):s=s[:a]+f+s[b:]
out=p/'checkpoints/usb-final';out.mkdir(exist_ok=True);dest=out/src.name;dest.write_text(s)
for ext in ['kicad_pro','kicad_dru']:shutil.copy2(p/'kicad'/('adom-custom-electronic-load.'+ext),out/('adom-custom-electronic-load.'+ext))
print(dest)