From f82962e42cfda28e0d4d33712b087eed62f0cea0 Mon Sep 17 00:00:00 2001 From: Ian McInerney Date: Sat, 29 Jun 2019 14:51:07 +0100 Subject: [PATCH] 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 --- .../python_scripts/bom_sorted_by_ref.py | 2 +- .../python_scripts/kicad_netlist_reader.py | 27 ++++++++++++------- 2 files changed, 18 insertions(+), 11 deletions(-) diff --git a/eeschema/plugins/python_scripts/bom_sorted_by_ref.py b/eeschema/plugins/python_scripts/bom_sorted_by_ref.py index 6dd28d5743..aa74dfb19b 100644 --- a/eeschema/plugins/python_scripts/bom_sorted_by_ref.py +++ b/eeschema/plugins/python_scripts/bom_sorted_by_ref.py @@ -36,7 +36,7 @@ except IOError: # Create a new csv writer object to use as the output formatter, although we # 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: def writerow( acsvwriter, columns ): diff --git a/eeschema/plugins/python_scripts/kicad_netlist_reader.py b/eeschema/plugins/python_scripts/kicad_netlist_reader.py index fbf498469d..bf4b11b079 100644 --- a/eeschema/plugins/python_scripts/kicad_netlist_reader.py +++ b/eeschema/plugins/python_scripts/kicad_netlist_reader.py @@ -618,10 +618,14 @@ class netlist(): if not exclude: ret.append(c) - # Sort first by ref as this makes for easier to read BOM's - def f(v): - return re.sub(r'([A-z]+)[0-9]+', r'\1', v) + '%08i' % int(re.sub(r'[A-z]+([0-9]+)', r'\1', v)) - ret.sort(key=lambda g: f(g.getRef())) + # The key to sort the components in the BOM + # This sorts using a natural sorting order (e.g. 100 after 99), and if it wasn't used + # the normal sort would place 100 before 99 since it only would look at the first digit. + 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 @@ -660,15 +664,18 @@ class netlist(): # Add the new component group to the groups list groups.append(newgroup) - # Each group is a list of components, we need to sort each list first - # to get them in order as this makes for easier to read BOM's - def f(v): - return re.sub(r'([A-z]+)[0-9]+', r'\1', v) + '%08i' % int(re.sub(r'[A-z]+([0-9]+)', r'\1', v)) + # The key to sort the components in the BOM + # This sorts using a natural sorting order (e.g. 100 after 99), and if it wasn't used + # the normal sort would place 100 before 99 since it only would look at the first digit. + def sortKey( str ): + return [ int(t) if t.isdigit() else t.lower() + for t in re.split( '(\d+)', str ) ] + 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 - groups = sorted(groups, key=lambda group: f(group[0].getRef())) + groups = sorted(groups, key=lambda group: sortKey(group[0].getRef())) return groups