main
John Lauer Add conservative JLC sourcing audit, isolated silk candidate and narrated wiki clip provenance 89e01c9
"""Conservative exact-MPN match report; never silently substitute search results."""
import csv,json,pathlib,re
root=pathlib.Path(__file__).resolve().parents[1]; base=root/'jlcpcb-flow'; rows=list(csv.DictReader((base/'bom-assembly-draft.csv').open())); inventory=[]
aliases={x['mpn']:x for x in json.loads((base/'sourcing/manufacturer-identities.json').read_text())['aliases']}
for row in rows:
 if row['fit']:continue
 p=base/'sourcing/catalog'/(re.sub(r'[^A-Za-z0-9_-]','_',row['mpn'])+'.json')
 if not p.exists():inventory.append({'ref':row['ref'],'mpn':row['mpn'],'status':'lookup pending'});continue
 raw=json.loads(p.read_text())
 try:d=json.loads(raw.get('stdout','{}'))
 except json.JSONDecodeError:d={}
 matches=[c for c in d.get('components',[]) if (c.get('mfr')==row['mpn'] if row['mpn'] not in aliases else c.get('mfr')==aliases[row['mpn']]['catalogMpn'] and c.get('lcsc')==aliases[row['mpn']]['lcsc'])]
 if len(matches)==1:
  c=matches[0];row['lcsc']=c['lcsc'];status='MPN catalogue match; manufacturer, live stock and rotation review required'
  inventory.append({'ref':row['ref'],'mpn':row['mpn'],'lcsc':c['lcsc'],'status':status,'snapshotStock':c.get('stock'),'stockAsOf':c.get('stock_as_of')})
 else:inventory.append({'ref':row['ref'],'mpn':row['mpn'],'status':'no unique exact match','candidates':[{'mpn':c.get('mfr'),'lcsc':c.get('lcsc')}for c in d.get('components',[])]})
with (base/'bom-assembly-draft.csv').open('w',newline='')as f:w=csv.DictWriter(f,list(rows[0]));w.writeheader();w.writerows(rows)
(base/'sourcing/exact-match-audit.json').write_text(json.dumps(inventory,indent=2))
from collections import Counter
print(dict(Counter(x['status']for x in inventory)))