from pathlib import Path
import sys,re,shutil
R=Path(__file__).resolve().parent;sys.path.insert(0,str(R.parent/'parity_metadata_closure_A22'));from prepare_candidates import forms
P=R/'board/led_driver.kicad_pcb';shutil.copy2(P,R/'spacing_attempt1_board.kicad_pcb');s=P.read_text()
for st,en,t in reversed(list(forms(s,'segment'))):
 n=re.search(r'\(net "([^"]+)"',t)[1]
 def fix(m):
  x,y=float(m[2]),float(m[3])
  if n=='/LED_K28':
   if x==54.75:x=54.8017
   if y==48.75:y=48.8217
  if n=='/LED_K38' and (x,y)==(54.9794,49):y=49.1
  if n=='/LED_K58' and x==71.5581:x=71.65
  if n=='/LED_RESET_N' and y==55.4648:y=55.3348
  return '(%s %.6f %.6f)'%(m[1],x,y)
 t=re.sub(r'\((start|end) ([\d.-]+) ([\d.-]+)\)',fix,t);s=s[:st]+t+s[en:]
P.write_text(s)
