bom_csv_grouped_extra: use field-specific methods for data.

Fixes https://gitlab.com/kicad/code/kicad/issues/14129


(cherry picked from commit c1f4911d7e)
This commit is contained in:
Alex 2023-03-05 03:23:51 +03:00 committed by dsa-t
parent 2c7f44f78d
commit 8f1237e629
1 changed files with 26 additions and 21 deletions

View File

@ -27,28 +27,36 @@ import sys
# Get extra fields from the command line # Get extra fields from the command line
extra_fields = sys.argv[3:] extra_fields = sys.argv[3:]
comp_fields = ['Value', 'Footprint', 'DNP'] + extra_fields
header_names = ['#', 'Reference', 'Qty'] + comp_fields
def getComponentString(comp, field_name):
if field_name == "Value":
return comp.getValue()
elif field_name == "Footprint":
return comp.getFootprint()
elif field_name == "DNP":
return comp.getDNPString()
elif field_name == "Datasheet":
return comp.getDatasheet()
else:
return comp.getField( field_name )
def myEqu(self, other): def myEqu(self, other):
"""myEqu is a more advanced equivalence function for components which is """myEqu is a more advanced equivalence function for components which is
used by component grouping. Normal operation is to group components based used by component grouping. Normal operation is to group components based
on their Value and Footprint. on their Value and Footprint.
In this example of a more advanced equivalency operator we also compare the In this example of a more advanced equivalency operator we also compare the
Footprint, Value and all extra fields passed from the command line. If Footprint, Value, DNP and all extra fields passed from the command line. If
these fields are not used in some parts they will simply be ignored (they these fields are not used in some parts they will simply be ignored (they
will match as both will be empty strings). will match as both will be empty strings).
""" """
result = True result = True
if self.getValue() != other.getValue(): for field_name in comp_fields:
result = False if getComponentString(self, field_name) != getComponentString(other, field_name):
elif self.getFootprint() != other.getFootprint(): result = False
result = False
elif self.getDNP() != other.getDNP():
result = False
else:
for field_name in extra_fields:
if self.getField(field_name) != other.getField(field_name):
result = False
return result return result
@ -74,7 +82,7 @@ except IOError:
out = csv.writer(f, lineterminator='\n', delimiter=',', quotechar='\"', quoting=csv.QUOTE_ALL) out = csv.writer(f, lineterminator='\n', delimiter=',', quotechar='\"', quoting=csv.QUOTE_ALL)
# Output a CSV header # Output a CSV header
out.writerow(['#', 'Reference', 'Qty', 'Value', 'Footprint', 'DNP'] + extra_fields) out.writerow(header_names)
# Get all of the components in groups of matching parts + values # Get all of the components in groups of matching parts + values
# (see kicad_netlist_reader.py) # (see kicad_netlist_reader.py)
@ -93,20 +101,17 @@ for group in grouped:
# Remove trailing comma # Remove trailing comma
refs = refs[:-2] refs = refs[:-2]
# Fill in the component groups common data # Fill in the component groups common data
row = [] row = []
row.append( index ) row.append( index )
row.append( refs ) row.append( refs )
row.append( len(group) ) row.append( len(group) )
row.append( c.getValue() )
row.append( c.getFootprint() ) # Add the values of component-specific data
row.append( c.getDNPString() ) for field_name in comp_fields:
row.append( getComponentString( c, field_name ) )
# Add the values of extra fields
for field_name in extra_fields:
row.append( c.getField( field_name ) )
out.writerow(row) out.writerow(row)
index += 1 index += 1