main
John Lauer Add JST GH libraries, native schematic parity and revised sensor harness study with reviewed clips 1ac01c3
"""Isolate JST GH sensor termination candidate; no formal BOM or board adoption."""
from pathlib import Path
import json,copy,shutil,uuid,hashlib
import sexp
R=Path(__file__).resolve().parents[1];O=R/'checkpoints/sensor-gh-review-v5';O.mkdir(exist_ok=True);src=R/'checkpoints/scaffold-fit-review/native-saved/scaffold-fit-candidate.kicad_pcb';dst=O/'sensor-gh-candidate.kicad_pcb';S=sexp.Sym
for folder in ['3d','lib']:shutil.copytree(R/'checkpoints/scaffold-fit-review'/folder,O/folder,dirs_exist_ok=True)
shutil.copy2(R/'checkpoints/scaffold-fit-review/fp-lib-table',O/'fp-lib-table')
for ext in ['kicad_pro','kicad_dru']:shutil.copy2(R/'checkpoints/scaffold-fit-review/scaffold-fit-candidate.kicad_pcb'.replace('.kicad_pcb','.'+ext),dst.with_suffix('.'+ext))
b=sexp.parse(src.read_text());old=next(f for f in sexp.find(b,'footprint')if any(p[1:3]==['Reference','J10']for p in sexp.find(f,'property')))
f=sexp.parse((R/'components/bm02b-ghs-tbt/BM02B-GHS-TBT.kicad_mod').read_text());f[1]='Load:JST_GH_BM02B-GHS-TBT_1x02-1MP_P1.25mm_Vertical'
f[:]=[v for v in f if not(isinstance(v,list)and v and v[0] in ['version','generator','property','model'])]
f.extend([[S('at'),S('108.625'),S('37.05')],[S('uuid'),str(uuid.uuid4())]])
for p in sexp.find(old,'property'):
 p=copy.deepcopy(p)
 if p[1]=='MPN':p[2]='BM02B-GHS-TBT'
 if p[1]=='Value':p[2]='JST GH case sensor'
 if p[1]=='KiLib_Generator':continue
 f.append(p)
for p in sexp.find(f,'pad'):
 p[:]=[v for v in p if not(isinstance(v,list)and v and v[0]in ['uuid','net'])];p.append([S('uuid'),str(uuid.uuid4())])
 if p[1]in ['1','2']:p.append([S('net'),'TEMP_SENSE' if p[1]=='1' else 'GND'])
 else:assert p[1]=='MP' # Explicit unnetted retention pads preserved, not hidden from the audit.
model=R/'components/bm02b-ghs-tbt/review/JST_GH_BM02B-GHS-TBT_1x02-1MP_P1.25mm_Vertical.step';shutil.copy2(model,O/'3d/bm02b-ghs-tbt-kicad.step')
f.append([S('model'),'${KIPRJMOD}/3d/bm02b-ghs-tbt-kicad.step',[S('offset'),[S('xyz'),S('0'),S('0'),S('0')]],[S('scale'),[S('xyz'),S('1'),S('1'),S('1')]],[S('rotate'),[S('xyz'),S('0'),S('0'),S('0')]]])
b[b.index(old)]=f
net='TEMP_SENSE'
for start,end,layer in [([108,39],[100,39],'F.Cu'),([100,39],[100,64],'F.Cu'),([100,64],[103,64],'B.Cu'),([103,64],[103,73],'F.Cu'),([103,73],[104,74],'F.Cu'),([104,74],[104,74.6],'F.Cu')]:
 b.append([S('segment'),[S('start'),*[S(str(v))for v in start]],[S('end'),*[S(str(v))for v in end]],[S('width'),S('0.25')],[S('layer'),layer],[S('net'),net],[S('uuid'),str(uuid.uuid4())]])
for x,y in [(100,64),(103,64),(104,74.6)]:
 b.append([S('via'),[S('at'),S(str(x)),S(str(y))],[S('size'),S('0.6')],[S('drill'),S('0.3')],[S('layers'),'F.Cu','B.Cu'],[S('net'),net],[S('uuid'),str(uuid.uuid4())]])
for prop in sexp.find(f,'property'):
 if prop[1]=='Reference':sexp.first(prop,'at')[1:]=[S('0'),S('-4'),S('0')]
for z in sexp.find(b,'zone'):z[:]=[v for v in z if not(isinstance(v,list)and v and v[0]in ['filled_polygon','fill_segments'])]
dst.write_text(sexp.dump(b)+'\n')
lib=copy.deepcopy(f);lib[1]='JST_GH_BM02B-GHS-TBT_1x02-1MP_P1.25mm_Vertical';lib[:]=[v for v in lib if not(isinstance(v,list)and v and v[0]in ['at','uuid'])]
for p in sexp.find(lib,'pad'):p[:]=[v for v in p if not(isinstance(v,list)and v and v[0]in ['net','uuid'])]
(O/'lib/Load.pretty/JST_GH_BM02B-GHS-TBT_1x02-1MP_P1.25mm_Vertical.kicad_mod').write_text(sexp.dump(lib)+'\n')
n=json.loads((R/'design/netlist.json').read_text());next(c for c in n['components']if c['ref']=='J10')['mpn']='BM02B-GHS-TBT';(O/'netlist.json').write_text(json.dumps(n,indent=2)+'\n')
r={'adopted':False,'sourceSha256':hashlib.sha256(src.read_bytes()).hexdigest(),'candidateSha256':hashlib.sha256(dst.read_bytes()).hexdigest(),'J10':{'mpn':'BM02B-GHS-TBT','pin1':'TEMP_SENSE','pin2':'GND','retentionPads':{'number':'MP','count':2,'net':None,'scope':'Mechanical retention solder pads from existing KiCad footprint, not signal pins; explicit library audit required'}},'addedRoute':{'viaXY':[[100,64],[103,64],[104,74.6]],'viaDiameter':.6,'drill':.3,'topTrackWidth':.25,'pin1XY':[108,39]},'limitations':['Candidate only; GH housing sourcing unresolved','Exact terminal/body landing and crimp preparation remain open','No canonical schematic/BOM or formal acceptance changed','Sensor wire route and mating connector geometry require update']};(O/'selection-review.json').write_text(json.dumps(r,indent=2)+'\n');print(json.dumps(r))