main
John Lauer Cooling clearance revision: native probe/fan relocation, checked copper cleanup, fitted-body screen and recorded review… 04019ce
"""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/cooling-before-fields/adom-custom-electronic-load.kicad_pcb';s=src.read_text();candidate=(p/'checkpoints/cooling-clearance/run-v9/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 ['TP4','TP8','J9','D4','TP6']: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)==5
for a,b,f in reversed(edits):s=s[:a]+f+s[b:]
out=p/'checkpoints/cooling-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)