From 13f3e2dc3f3593779f9d0805b366b4b767820e56 Mon Sep 17 00:00:00 2001 From: Stephen John Cholvat Date: Tue, 22 Aug 2017 21:38:26 -0700 Subject: [PATCH] Created initial script- needs more testing --- bom/generate_warg_bom.py | 47 ++++++++++++++++++++++++++++++++++++++++ component_library.csv | 4 ++-- 2 files changed, 49 insertions(+), 2 deletions(-) create mode 100644 bom/generate_warg_bom.py diff --git a/bom/generate_warg_bom.py b/bom/generate_warg_bom.py new file mode 100644 index 0000000..64543be --- /dev/null +++ b/bom/generate_warg_bom.py @@ -0,0 +1,47 @@ +import sys, os +import xml.etree.ElementTree as ET +import csv + +def main(): + if(len(sys.argv) != 3): + print "Make sure there are only 2 arguments!" #The user passes 2 arguments and the 1st argument is always the script filepath + filepath, input_filename, output_filename = sys.argv + output_filename += ".csv" + + #get filename for component library + component_lib_filename = os.path.join(os.path.dirname(filepath), os.pardir, 'component_library.csv') + + #output_dict stores the data to be written to csv + #found_parts is used to quickly find what index a component is at in the output_dict + output_dict = [] + found_parts = [] + + #parse xml netlist + tree = ET.parse(input_filename) + root = tree.getroot() + + #find all part numbers + for field in root.findall(".//*[@name='part_num']"): + if field.text not in found_parts: + found_parts.append(field.text) + output_dict.append({"part_num":field.text, + "quantity":1}) + else: + output_dict[found_parts.index(field.text)]["quantity"] +=1 + + reader = csv.DictReader(open(component_lib_filename, 'rb')) + component_library = [] + for line in reader: + if line["part_num"] in found_parts: + #add the extra information from the component library + output_dict[found_parts.index(line["part_num"])].update(line) + + #write the csv + with open(output_filename, 'wb') as output_file: + dict_writer = csv.DictWriter(output_file, output_dict[0].keys()) + dict_writer.writeheader() + dict_writer.writerows(output_dict) + print "wrote to {} file.".format(output_filename) + +if __name__ == "__main__": + main() diff --git a/component_library.csv b/component_library.csv index aac2fae..cdf8b5d 100644 --- a/component_library.csv +++ b/component_library.csv @@ -1,4 +1,4 @@ -WARG-part-#,Distrubuter,Distributer #,description,Manufacturer Part #,Inventory,Inventory Date,Alternative distributer #s,cost (qty. 1 CAD),Value,Footprint,link +part_num,Distrubuter,Distributer #,description,Manufacturer Part #,Inventory,Inventory Date,Alternative distributer #s,cost (qty. 1 CAD),Value,Footprint,link CAPP-001,Digi-Key,1276-1119-1-ND ,CAP CER 10UF 6.3V X5R 0603,,31,6/18/17,445-7492-1-ND / 490-3896-1-ND / 445-4112-1-ND,,,, CAPP-002,Digi-Key,1276-1104-1-ND,CAP CER 0.022UF 50V 10% X7R 0603,,100,6/18/17,,,,, CAPP-003,Digi-Key,1276-6504-1-ND,CAP CER 22UF 10V X5R 0603,,0,6/18/17,,,,, @@ -62,4 +62,4 @@ CAPP-008,,587-2909-1-ND,CAP CER 2.2UF 25V 10% X5R 0603,,99,6/18/17,,,,, CAPP-004,,587-1262-1-ND,CAP CER 1.0UF 16V Y5V 0603,,90,6/18/17,,,,, CAPP-005,,490-3285-1-ND,CAP CER .1UF 100V X7R 0603,,1,6/18/17,,,,, CAPP-004,,399-5090-1-ND,CAP CERAMIC 1.00UF 16V X5R 0603,,83,6/18/17,,,,, -RESS-003,,311-100KGRCT-ND,RES SMD 100K OHM 5% 1/10W 0603,,100,6/18/17,,,,, \ No newline at end of file +RESS-003,,311-100KGRCT-ND,RES SMD 100K OHM 5% 1/10W 0603,,100,6/18/17,,,,,