implemented changes from Jens review
This commit is contained in:
52
scripts/extract_lcsc.py
Normal file
52
scripts/extract_lcsc.py
Normal file
@@ -0,0 +1,52 @@
|
||||
#!/usr/bin/env python3
|
||||
"""Extract all LCSC part numbers from a KiCad schematic and print a sorted table."""
|
||||
|
||||
import re
|
||||
import sys
|
||||
|
||||
SCH_FILE = 'cm4-nrf54.kicad_sch'
|
||||
|
||||
if len(sys.argv) > 1:
|
||||
SCH_FILE = sys.argv[1]
|
||||
|
||||
with open(SCH_FILE, 'r') as f:
|
||||
content = f.read()
|
||||
|
||||
lcsc_pattern = re.compile(r'\(property\s*"(?:LCSC|lcsc|LCSC Part)"\s*"([^"]+)"')
|
||||
field_pattern = re.compile(r'\(property\s*"(LCSC|lcsc|LCSC Part)"\s*"([^"]+)"')
|
||||
|
||||
blocks = re.split(r'\n\s*\(symbol\s+\(lib_id', content)
|
||||
|
||||
dnp_pattern = re.compile(r'\(dnp\s+yes\)')
|
||||
|
||||
results = []
|
||||
for block in blocks[1:]:
|
||||
ref_m = re.search(r'\(property\s*"Reference"\s*"([^"]+)"', block)
|
||||
val_m = re.search(r'\(property\s*"Value"\s*"([^"]+)"', block)
|
||||
lcsc_m = lcsc_pattern.search(block)
|
||||
field_m = field_pattern.search(block)
|
||||
|
||||
ref = ref_m.group(1) if ref_m else '?'
|
||||
val = val_m.group(1) if val_m else '?'
|
||||
lcsc = lcsc_m.group(1) if lcsc_m else ''
|
||||
field = field_m.group(1) if field_m else ''
|
||||
dnp = bool(dnp_pattern.search(block))
|
||||
|
||||
if ref.startswith('#') or ref == '?':
|
||||
continue
|
||||
results.append((ref, val, lcsc, field, dnp))
|
||||
|
||||
def sort_key(x):
|
||||
letters = ''.join(filter(str.isalpha, x[0]))
|
||||
digits = ''.join(filter(str.isdigit, x[0]))
|
||||
return (letters, int(digits) if digits else 0)
|
||||
|
||||
results.sort(key=sort_key)
|
||||
|
||||
print(f'{"REF":12} {"VALUE":42} {"LCSC":15} {"DNP":3} FIELD')
|
||||
print('-' * 87)
|
||||
for ref, val, lcsc, field, dnp in results:
|
||||
dnp_str = 'DNP' if dnp else ' '
|
||||
skip = dnp or ref.startswith(('H', 'JP', 'TP'))
|
||||
flag = ' <-- MISSING' if not lcsc and not skip else ''
|
||||
print(f'{ref:12} {val:42} {lcsc:15} {dnp_str} {field}{flag}')
|
||||
Reference in New Issue
Block a user