Arguments are now handled with argparse
- User can pass verbosity option - User can specify a config file, overwriting default bom.ini - User can not specify an output file, and a default output will be used Preferences object now has a verbosity switch This will be (eventually) passed down-stream to relevent places Added zener aliases Added diode aliases
This commit is contained in:
parent
f0985b2623
commit
8aaa2bb110
|
|
@ -49,6 +49,7 @@ class BomPref:
|
||||||
self.useRegex = True #Test various columns with regex
|
self.useRegex = True #Test various columns with regex
|
||||||
self.compareFootprints = True #test footprints when comparing components
|
self.compareFootprints = True #test footprints when comparing components
|
||||||
self.buildNumber = 0
|
self.buildNumber = 0
|
||||||
|
self.verbose = False #by default, is not verbose
|
||||||
|
|
||||||
#default reference exclusions
|
#default reference exclusions
|
||||||
self.excluded_references = [
|
self.excluded_references = [
|
||||||
|
|
@ -74,7 +75,9 @@ class BomPref:
|
||||||
["c", "c_small", "cap", "capacitor"],
|
["c", "c_small", "cap", "capacitor"],
|
||||||
["r", "r_small", "res", "resistor"],
|
["r", "r_small", "res", "resistor"],
|
||||||
["sw", "switch"],
|
["sw", "switch"],
|
||||||
["l", "l_small", "inductor"]
|
["l", "l_small", "inductor"],
|
||||||
|
["zener","zenersmall"],
|
||||||
|
["d","diode","d_small"]
|
||||||
]
|
]
|
||||||
|
|
||||||
#dictionary of possible regex expressions for ignoring component row(s)
|
#dictionary of possible regex expressions for ignoring component row(s)
|
||||||
|
|
|
||||||
93
KiBOM_CLI.py
93
KiBOM_CLI.py
|
|
@ -6,6 +6,8 @@ import sys
|
||||||
import os
|
import os
|
||||||
import shutil
|
import shutil
|
||||||
|
|
||||||
|
import argparse
|
||||||
|
|
||||||
here = os.path.abspath(os.path.dirname(sys.argv[0]))
|
here = os.path.abspath(os.path.dirname(sys.argv[0]))
|
||||||
|
|
||||||
sys.path.append(here)
|
sys.path.append(here)
|
||||||
|
|
@ -15,58 +17,77 @@ from KiBOM.netlist_reader import *
|
||||||
from KiBOM.bom_writer import *
|
from KiBOM.bom_writer import *
|
||||||
from KiBOM.preferences import BomPref
|
from KiBOM.preferences import BomPref
|
||||||
|
|
||||||
|
verbose = False
|
||||||
|
|
||||||
def close(*arg):
|
def close(*arg):
|
||||||
print(*arg)
|
print(*arg)
|
||||||
sys.exit(0)
|
sys.exit(0)
|
||||||
|
|
||||||
if len(sys.argv) < 2:
|
def say(*arg):
|
||||||
close("No input file supplied")
|
if verbose:
|
||||||
|
print(*arg)
|
||||||
|
|
||||||
input_file = sys.argv[1].replace("\\",os.path.sep).replace("/",os.path.sep)
|
parser = argparse.ArgumentParser(description="KiBOM Bill of Materials generator script")
|
||||||
|
|
||||||
input_file = os.path.abspath(input_file)
|
parser.add_argument("netlist", help='xml netlist file. Use "%%I" when running from within KiCad')
|
||||||
|
parser.add_argument("--output", "-o", help='BoM output file name.\nUse "%%O" when running from within KiCad to use the default output name (csv file).\nFor e.g. HTML output, use "%%O.html"\r\nIf output file is unspecified, default output filename (csv format) will be used', default=None)
|
||||||
|
|
||||||
|
parser.add_argument("-v", "--verbose", help="Enable verbose output", action='count')
|
||||||
|
parser.add_argument("--cfg", help="BoM config file (script will try to use 'bom.ini' if not specified here)")
|
||||||
|
|
||||||
|
args = parser.parse_args()
|
||||||
|
|
||||||
|
input_file = args.netlist
|
||||||
|
|
||||||
if not input_file.endswith(".xml"):
|
if not input_file.endswith(".xml"):
|
||||||
close("Supplied file is not .xml")
|
close("{i} is not a valid xml file".format(i=input_file))
|
||||||
|
|
||||||
#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
|
verbose = args.verbose is not None
|
||||||
|
|
||||||
for e in [".xml",".csv",".txt",".tsv",".html"]:
|
input_file = os.path.abspath(input_file)
|
||||||
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)
|
|
||||||
|
|
||||||
#preferences
|
say("Input:",input_file)
|
||||||
ignore = []
|
|
||||||
ignoreDNF = False
|
|
||||||
numberRows = True
|
|
||||||
|
|
||||||
#Look for a '.bom' preference file
|
output_file = args.output
|
||||||
pref_file = os.path.join(os.path.dirname(input_file) , "bom.ini")
|
|
||||||
|
if output_file is None:
|
||||||
|
output_file = input_file.replace(".xml","_bom.csv")
|
||||||
|
|
||||||
|
#enfore a proper extension
|
||||||
|
valid = False
|
||||||
|
extensions = [".xml",".csv",".txt",".tsv",".html"]
|
||||||
|
for e in extensions:
|
||||||
|
if output_file.endswith(e):
|
||||||
|
valid = True
|
||||||
|
break
|
||||||
|
if not valid:
|
||||||
|
close("Extension must be one of",extensions)
|
||||||
|
|
||||||
|
output_file = os.path.abspath(output_file)
|
||||||
|
|
||||||
|
say("Output:",output_file)
|
||||||
|
|
||||||
|
#look for a config file!
|
||||||
|
#bom.ini by default
|
||||||
|
config_file = os.path.abspath(os.path.join(os.path.dirname(input_file), "bom.ini"))
|
||||||
|
|
||||||
|
#user can overwrite with a specific config file
|
||||||
|
if args.cfg:
|
||||||
|
config_file = args.cfg
|
||||||
|
|
||||||
#read preferences from file. If file does not exists, default preferences will be used
|
#read preferences from file. If file does not exists, default preferences will be used
|
||||||
pref = BomPref()
|
pref = BomPref()
|
||||||
pref.Read(pref_file)
|
|
||||||
|
#verbosity options
|
||||||
|
pref.verbose = verbose
|
||||||
|
|
||||||
|
if os.path.exists(config_file):
|
||||||
|
pref.Read(config_file)
|
||||||
|
say("Config:",config_file)
|
||||||
|
|
||||||
#write preference file back out (first run will generate a file with default preferences)
|
#write preference file back out (first run will generate a file with default preferences)
|
||||||
pref.Write(pref_file)
|
if not os.path.exists("bom.ini"):
|
||||||
|
pref.Write("bom.ini")
|
||||||
|
|
||||||
#individual components
|
#individual components
|
||||||
components = []
|
components = []
|
||||||
|
|
@ -92,7 +113,7 @@ for g in groups:
|
||||||
|
|
||||||
if pref.buildNumber < 1:
|
if pref.buildNumber < 1:
|
||||||
columns.RemoveColumn(ColumnList.COL_GRP_BUILD_QUANTITY)
|
columns.RemoveColumn(ColumnList.COL_GRP_BUILD_QUANTITY)
|
||||||
print("Removing:",ColumnList.COL_GRP_BUILD_QUANTITY)
|
say("Removing:",ColumnList.COL_GRP_BUILD_QUANTITY)
|
||||||
|
|
||||||
#Finally, write the BoM out to file
|
#Finally, write the BoM out to file
|
||||||
result = WriteBoM(output_file, groups, net, columns.columns, pref)
|
result = WriteBoM(output_file, groups, net, columns.columns, pref)
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue