From dfa025a7c106fde700cee9c1414dd1e7bae2f64e Mon Sep 17 00:00:00 2001 From: Oliver Date: Sun, 15 May 2016 10:27:20 +1000 Subject: [PATCH] added kicad_bom.py --- kicad_bom.py | 100 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 100 insertions(+) create mode 100644 kicad_bom.py diff --git a/kicad_bom.py b/kicad_bom.py new file mode 100644 index 00000000..72ca0cfa --- /dev/null +++ b/kicad_bom.py @@ -0,0 +1,100 @@ + +from __future__ import print_function + +import re +import csv +import sys +import os +import shutil + +DELIMITER = "," + +here = os.path.abspath(os.path.dirname(sys.argv[0])) + +sys.path.append(here) + +from KiBOM.columns import ColumnList +from KiBOM.netlist_reader import * +from KiBOM.bom_writer import * + +#import bomfunk_netlist_reader +#import bomfunk_csv +#from bomfunk_csv import CSV_DEFAULT as COLUMNS + +global DEBUG +DEBUG = True + +def debug(msg): + global DEBUG + if DEBUG == True: + print(msg) + +def close(*arg): + print(*arg) + sys.exit(0) + +def error(*arg): + print(*arg) + sys.exit(-1) + +if len(sys.argv) < 2: + close("No input file supplied") + +input_file = sys.argv[1].replace("\\",os.path.sep).replace("/",os.path.sep) + +input_file = os.path.abspath(input_file) + +if not input_file.endswith(".xml"): + close("Supplied file is not .xml") + +#work out an output file +ext = ".csv" + +if len(sys.argv) < 3: + #no output file supplied, assume .csv + output_file = input_file.replace(".xml",".csv") +else: + output_file = sys.argv[2].replace("\\",os.path.sep).replace("/",os.path.sep) + + valid = False + + for e in [".xml",".csv",".txt",".tsv",".html"]: + if output_file.endswith(e): + valid = True + ext = e + break + if not valid: + output_file += ext + + output_file = os.path.abspath(output_file) + +print("Input File: " + input_file) +print("Output File: " + output_file) + +#individual components +components = [] + +#component groups +groups = [] + +#read out the netlist +net = netlist(input_file) + +#extract the components +components = net.getInterestingComponents() + +#group the components +groups = net.groupComponents(components) + +if ext in [".csv",".tsv",".txt"]: + if WriteCSV(output_file, groups, net.getSource(), net.getVersion(), net.getDate()): + print("Success! Wrote components to " + output_file) + else: + print("Error writing to " + output_file) + +elif ext == ".xml": + WriteXML(output_file, groups, net.getSource(), net.getVersion(), net.getDate()) + +elif ext in ['.htm','.html']: + WriteHTML(output_file, groups, net) +