main
John Lauer Add JST GH libraries, native schematic parity and revised sensor harness study with reviewed clips 1ac01c3
"""Send the complete hierarchical schematic for native export and exact pin parity."""
from pathlib import Path
import json,subprocess,xml.etree.ElementTree as ET,hashlib,sys
import sexp
p=Path(__file__).resolve().parents[1];out=p/'checkpoints/sensor-gh-review-v8';remote='C:/Users/john/adom-projects/adom-custom-electronic-load/review/sensor-gh-netlist'
def call(verb,args,label):
 r=subprocess.run(['adom-bridge','--target','AdomLapper','--ai-thread','adom-custom-load',verb,json.dumps(args)],text=True,capture_output=True);j=json.loads(r.stdout);(out/(label+'.json')).write_text(json.dumps(j,indent=2)+'\n');assert r.returncode==0 and j.get('success',j.get('ok',True)) and not j.get('error') and j.get('exitCode',0)==0,j;return j
if '--reuse-export' not in sys.argv:
 call('send_files',{'filePaths':[str(f) for f in out.glob('*.kicad_sch')],'dest':remote+'/','reason':'Include every hierarchical sheet; root-only CLI shim export was empty'},'netlist-send-complete-hierarchy')
 cli=call('kicad_status',{},'netlist-native-status')['install']['kicadCli']
 call('shell_execute',{'command':'"'+cli+'"','args':['sch','export','netlist','--format','kicadxml','--output',remote+'/complete-netlist.xml',remote+'/adom-custom-electronic-load.kicad_sch'],'timeoutSeconds':120,'reason':'Export full native schematic candidate for pin and R7 identity parity'},'netlist-native-export')
 call('pull_file',{'filePaths':[remote+'/complete-netlist.xml'],'saveTo':str(out)},'netlist-native-pull')
n=ET.parse(out/'complete-netlist.xml').getroot();components=n.findall('components/comp');assert len(components)==104,len(components)
t=sexp.parse((out/'sensor-gh-candidate.kicad_pcb').read_text());board={};allpads=set()
for f in sexp.find(t,'footprint'):
 ref=next(q[2] for q in sexp.find(f,'property') if q[1]=='Reference')
 for pad in sexp.find(f,'pad'):
  allpads.add((ref,str(pad[1])))
  nets=sexp.find(pad,'net')
  if nets:board[(ref,str(pad[1]))]=str(nets[0][-1])
sch={};no_connect=[]
for net in n.findall('nets/net'):
 for node in net.findall('node'):
  key=(node.attrib['ref'],node.attrib['pin'])
  if 'no_connect' in node.attrib.get('pintype','').split('+'):
   assert len(net.findall('node'))==1 and key in allpads and key not in board,(net.attrib,key)
   no_connect.append({'pin':key,'nativeNet':net.attrib['name'],'explicitPinType':node.attrib['pintype'],'boardPadPresentAndUnassigned':True})
  else:sch[key]=net.attrib['name']
errors=[{'pin':key,'schematic':sch.get(key),'board':board.get(key)} for key in sorted(set(sch)|set(board)) if sch.get(key)!=board.get(key)]
c=n.find("components/comp[@ref='R7']");mpn=next((x.text for x in c.findall('fields/field') if x.attrib.get('name')=='MPN'),None)
r={'componentCount':len(components),'boardPinCount':len(board),'schematicPinCount':len(sch),'explicitNoConnects':no_connect,'mismatches':errors,'r7Value':c.findtext('value'),'r7Mpn':mpn,'candidateSha256':hashlib.sha256((out/'sensor-gh-candidate.kicad_pcb').read_bytes()).hexdigest(),'scope':'Exact native net names and pin membership plus R7 value/MPN; no electrical performance qualification'}
(out/'netlist-parity.json').write_text(json.dumps(r,indent=2)+'\n');print(json.dumps(r));assert not errors and r['r7Value']=='4.7k' and mpn=='ERJ-2RKF4701X'

for ref in ['Q3','Q4']:
 c=n.find(f"components/comp[@ref='{ref}']");mpn=next(x.text for x in c.findall('fields/field') if x.attrib.get('name')=='MPN');assert c.findtext('value')=='NX3008NBK' and mpn=='NX3008NBK,215'
print('Q3/Q4 exact ordering code and value verified in native schematic export')