eeschema: Fix python BOM generators to handle symbols

* Implement proper natural sorting of the reference values
* Perform quoting of all items in BOM generators that use csv.writer

Fixes: lp:1833822
* https://bugs.launchpad.net/kicad/+bug/1833822
This commit is contained in:
Ian McInerney 2019-06-29 14:51:07 +01:00 committed by jean-pierre charras
parent a498d7e9c5
commit 3b645ed305
2 changed files with 18 additions and 11 deletions

View File

@ -36,7 +36,7 @@ except IOError:
# Create a new csv writer object to use as the output formatter, although we # Create a new csv writer object to use as the output formatter, although we
# are created a tab delimited list instead! # are created a tab delimited list instead!
out = csv.writer(f, lineterminator='\n', delimiter='\t', quoting=csv.QUOTE_NONE) out = csv.writer(f, lineterminator='\n', delimiter='\t', quotechar="\"", quoting=csv.QUOTE_ALL)
# override csv.writer's writerow() to support utf8 encoding: # override csv.writer's writerow() to support utf8 encoding:
def writerow( acsvwriter, columns ): def writerow( acsvwriter, columns ):

View File

@ -618,10 +618,14 @@ class netlist():
if not exclude: if not exclude:
ret.append(c) ret.append(c)
# Sort first by ref as this makes for easier to read BOM's # The key to sort the components in the BOM
def f(v): # This sorts using a natural sorting order (e.g. 100 after 99), and if it wasn't used
return re.sub(r'([A-z]+)[0-9]+', r'\1', v) + '%08i' % int(re.sub(r'[A-z]+([0-9]+)', r'\1', v)) # the normal sort would place 100 before 99 since it only would look at the first digit.
ret.sort(key=lambda g: f(g.getRef())) def sortKey( str ):
return [ int(t) if t.isdigit() else t.lower()
for t in re.split( '(\d+)', str ) ]
ret.sort(key=lambda g: sortKey(g.getRef()))
return ret return ret
@ -660,15 +664,18 @@ class netlist():
# Add the new component group to the groups list # Add the new component group to the groups list
groups.append(newgroup) groups.append(newgroup)
# Each group is a list of components, we need to sort each list first # The key to sort the components in the BOM
# to get them in order as this makes for easier to read BOM's # This sorts using a natural sorting order (e.g. 100 after 99), and if it wasn't used
def f(v): # the normal sort would place 100 before 99 since it only would look at the first digit.
return re.sub(r'([A-z]+)[0-9]+', r'\1', v) + '%08i' % int(re.sub(r'[A-z]+([0-9]+)', r'\1', v)) def sortKey( str ):
return [ int(t) if t.isdigit() else t.lower()
for t in re.split( '(\d+)', str ) ]
for g in groups: for g in groups:
g = sorted(g, key=lambda g: f(g.getRef())) g = sorted(g, key=lambda g: sortKey(g.getRef()))
# Finally, sort the groups to order the references alphabetically # Finally, sort the groups to order the references alphabetically
groups = sorted(groups, key=lambda group: f(group[0].getRef())) groups = sorted(groups, key=lambda group: sortKey(group[0].getRef()))
return groups return groups