Fixes and enhancements in python scripts relative to BOM generation.

This commit is contained in:
unknown 2015-06-26 19:52:49 +02:00 committed by jean-pierre charras
parent 1c5cde94f0
commit f194905117
4 changed files with 55 additions and 34 deletions

View File

@ -40,7 +40,7 @@ out = csv.writer(f, lineterminator='\n', delimiter=',', quotechar="\"", quoting=
def writerow( acsvwriter, columns ): def writerow( acsvwriter, columns ):
utf8row = [] utf8row = []
for col in columns: for col in columns:
utf8row.append( str(col).encode('utf8') ) utf8row.append( str(col) )
acsvwriter.writerow( utf8row ) acsvwriter.writerow( utf8row )
components = net.getInterestingComponents() components = net.getInterestingComponents()

View File

@ -9,7 +9,7 @@
Generate a HTML BOM list. Generate a HTML BOM list.
Components are sorted and grouped by ref Components are sorted and grouped by ref
Fields are (if exist) Fields are (if exist)
Ref, Quantity, Value, Part, Datasheet, Description, Vendor Ref, Quantity, Value, Symbol, footprint, Description, Vendor
""" """
@ -26,7 +26,7 @@ html = """
<html xmlns="http://www.w3.org/1999/xhtml"> <html xmlns="http://www.w3.org/1999/xhtml">
<head> <head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>KiCad BOM Example 5</title> <title>KiCad BOM grouped by value and footprint </title>
</head> </head>
<body> <body>
<h1><!--SOURCE--></h1> <h1><!--SOURCE--></h1>
@ -60,12 +60,12 @@ def myEqu(self, other):
result = False result = False
elif self.getFootprint() != other.getFootprint(): elif self.getFootprint() != other.getFootprint():
result = False result = False
elif self.getField("Tolerance") != other.getField("Tolerance"): # elif self.getField("Tolerance") != other.getField("Tolerance"):
result = False # result = False
elif self.getField("Manufacturer") != other.getField("Manufacturer"): # elif self.getField("Manufacturer") != other.getField("Manufacturer"):
result = False # result = False
elif self.getField("Voltage") != other.getField("Voltage"): # elif self.getField("Voltage") != other.getField("Voltage"):
result = False # result = False
return result return result
@ -97,10 +97,12 @@ html = html.replace('<!--COMPCOUNT-->', "<b>Component Count:</b>" + \
str(len(components))) str(len(components)))
row = "<tr><th style='width:640px'>Ref</th>" + "<th>Qnty</th>" row = "<tr><th style='width:640px'>Ref</th>" + "<th>Qnty</th>"
row += "<th>Value</th>" + "<th>Part</th>" row += "<th>Value</th>"
row += "<th>Description</th>" row += "<th>Symbol</th>"
#row += "<th>Datasheet</th>" row += "<th>Footprint</th>"
row += "<th>PartNumber</th>" + "<th>Vendor</th></tr>" row += "<th>Description</th>"
row += "<th>PartNumber</th>"
row += "<th>Vendor</th></tr>"
html = html.replace('<!--TABLEROW-->', row + "<!--TABLEROW-->") html = html.replace('<!--TABLEROW-->', row + "<!--TABLEROW-->")
@ -120,14 +122,17 @@ for group in grouped:
refs += component.getRef() refs += component.getRef()
c = component c = component
row = "<tr><td>" + refs +"</td><td>" + str(len(group)) row = "<tr>"
row += "</td><td>" + c.getValue() + "</td><td>" row += "<td>" + refs +"</td>"
row += c.getLibName() + ":" + c.getPartName() + "</td><td>" row += "<td align=center>" + str(len(group)) + "</td>"
#row += c.getDatasheet() + "</td><td>" row += "<td align=center>" + c.getValue() + "</td>"
row += c.getDescription() + "</td><td>" # row += "<td align=center>" + c.getLibName() + ":" + c.getPartName() + "</td>"
row += c.getField("PartNumber") + "</td><td>" row += "<td align=center>" + c.getPartName() + "</td>"
row += c.getField("Vendor") row += "<td align=center>" + c.getFootprint() + "</td>"
row += "</td></tr>" row += "<td align=center>" + c.getDescription() + "</td>"
row += "<td align=center>" + c.getField("PartNumber") + "</td>"
row += "<td align=center>" + c.getField("Vendor") + "</td>"
row += "</tr>"
html = html.replace('<!--TABLEROW-->', row + "<!--TABLEROW-->") html = html.replace('<!--TABLEROW-->', row + "<!--TABLEROW-->")

View File

@ -40,7 +40,7 @@ def writerow( acsvwriter, columns ):
utf8row = [] utf8row = []
for col in columns: for col in columns:
txt=str(col); txt=str(col);
utf8row.append( txt.encode('utf-8') ) utf8row.append( txt )
acsvwriter.writerow( utf8row ) acsvwriter.writerow( utf8row )
components = net.getInterestingComponents() components = net.getInterestingComponents()

View File

@ -165,6 +165,7 @@ class xmlElement():
def addAttribute(self, attr, value): def addAttribute(self, attr, value):
"""Add an attribute to this element""" """Add an attribute to this element"""
if type(value) != str: value = value.encode('utf-8')
self.attributes[attr] = value self.attributes[attr] = value
def setAttribute(self, attr, value): def setAttribute(self, attr, value):
@ -220,20 +221,31 @@ class xmlElement():
try: try:
if attrmatch != "": if attrmatch != "":
if self.attributes[attribute] == attrmatch: if self.attributes[attribute] == attrmatch:
return self.chars ret = self.chars
if type(ret) != str: ret = ret.encode('utf-8')
return ret
else: else:
return self.attributes[attribute] ret = self.attributes[attribute]
if type(ret) != str: ret = ret.encode('utf-8')
return ret
except AttributeError: except AttributeError:
return "" ret = ""
if type(ret) != str: ret = ret.encode('utf-8')
return ret
else: else:
return self.chars ret = self.chars
if type(ret) != str: ret = ret.encode('utf-8')
return ret
for child in self.children: for child in self.children:
ret = child.get(elemName, attribute, attrmatch) ret = child.get(elemName, attribute, attrmatch)
if ret != "": if ret != "":
if type(ret) != str: ret = ret.encode('utf-8')
return ret return ret
return "" ret = ""
if type(ret) != str: ret = ret.encode('utf-8')
return ret
@ -345,7 +357,7 @@ class comp():
v.setChars(value) v.setChars(value)
def getValue(self): def getValue(self):
return self.element.get("value").encode( "utf-8" ) return self.element.get("value")
def getField(self, name, libraryToo=True): def getField(self, name, libraryToo=True):
"""Return the value of a field named name. The component is first """Return the value of a field named name. The component is first
@ -361,7 +373,7 @@ class comp():
field = self.element.get("field", "name", name) field = self.element.get("field", "name", name)
if field == "" and libraryToo: if field == "" and libraryToo:
field = self.libpart.getField(name).encode( "utf-8" ) field = self.libpart.getField(name)
return field return field
def getFieldNames(self): def getFieldNames(self):
@ -374,7 +386,7 @@ class comp():
fields = self.element.getChild('fields') fields = self.element.getChild('fields')
if fields: if fields:
for f in fields.getChildren(): for f in fields.getChildren():
fieldNames.append( f.get('field','name').encode( "utf-8" ) ) fieldNames.append( f.get('field','name') )
return fieldNames return fieldNames
def getRef(self): def getRef(self):
@ -396,7 +408,7 @@ class comp():
return self.element.get("tstamp") return self.element.get("tstamp")
def getDescription(self): def getDescription(self):
return self.libpart.getDescription().encode( "utf-8" ) return self.libpart.getDescription()
class netlist(): class netlist():
@ -607,7 +619,9 @@ class netlist():
ret.append(c) ret.append(c)
# Sort first by ref as this makes for easier to read BOM's # Sort first by ref as this makes for easier to read BOM's
ret.sort(key=lambda g: g.getRef()) 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()))
return ret return ret
@ -648,11 +662,13 @@ class netlist():
# Each group is a list of components, we need to sort each list first # 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 # 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))
for g in groups: for g in groups:
g = sorted(g, key=lambda g: g.getRef()) g = sorted(g, key=lambda g: f(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: group[0].getRef()) groups = sorted(groups, key=lambda group: f(group[0].getRef()))
return groups return groups