diff --git a/KiBOM/bom_writer.py b/KiBOM/bom_writer.py index c3ab6a6d..37f65875 100644 --- a/KiBOM/bom_writer.py +++ b/KiBOM/bom_writer.py @@ -1,6 +1,7 @@ import csv import columns from component import * +from xml.etree import ElementTree import os, shutil @@ -11,6 +12,51 @@ def TmpFileCopy(filename): if os.path.exists(filename) and os.path.isfile(filename): shutil.copyfile(filename, filename + ".tmp") + +def WriteXML(filename, groups, source, version, date, headings = columns.ColumnList._COLUMNS_ALL, ignore=[], ignoreDNF = False): + + filename = os.path.abspath(filename) + + if not filename.endswith(".xml"): + return False + + headings = [h for h in headings if h not in ignore] + + try: + TmpFileCopy(filename) + + xml = ElementTree.Element('"KiCAD Bom"', attrib = { + '"source"' : source, + '"version"' : version, + '"date"' : date, + '"groups"' : str(len(groups)), + '"components"' : str(sum([group.getCount() for group in groups])) + }) + + for group in groups: + row = group.getKicadRow(headings) + + attrib = {} + + for i,h in enumerate(headings): + attrib['"' + h + '"'] = row[i] + + sub = ElementTree.SubElement(xml, "group", attrib=attrib) + + #write the document + tree = ElementTree.ElementTree(xml) + + tree.write(filename) + + return True + + except BaseException as e: + print(str(e)) + return False + + return True + + def WriteCSV(filename, groups, source, version, date, headings = columns.ColumnList._COLUMNS_ALL, ignore=[], ignoreDNF=False, numberRows=True): @@ -22,7 +68,7 @@ def WriteCSV(filename, groups, source, version, date, headings = columns.ColumnL elif filename.endswith(".tsv") or filename.endswith(".txt"): delimiter = "\t" else: - return + return False headings = [h for h in headings if h not in ignore] #copy across the headings @@ -71,10 +117,10 @@ def WriteCSV(filename, groups, source, version, date, headings = columns.ColumnL return True - except NameError: - pass -# except BaseException as e: -# print(str(e)) + + except BaseException as e: + print(str(e)) + return False - return False + return True \ No newline at end of file