Added a mechanism to set a default variant for all outputs.

It can be used to overwrite the output file template too.
This commit is contained in:
Salvador E. Tropea 2020-09-10 12:56:53 -03:00
parent 709d6facb9
commit 512776ce8e
5 changed files with 36 additions and 10 deletions

View File

@ -9,7 +9,7 @@
Usage: Usage:
kibot [-b BOARD] [-e SCHEMA] [-c CONFIG] [-d OUT_DIR] [-s PRE] kibot [-b BOARD] [-e SCHEMA] [-c CONFIG] [-d OUT_DIR] [-s PRE]
[-q | -v...] [-i] [TARGET...] [-q | -v...] [-i] [-g DEF]... [TARGET...]
kibot [-v...] [-c PLOT_CONFIG] --list kibot [-v...] [-c PLOT_CONFIG] --list
kibot [-v...] [-b BOARD] [-d OUT_DIR] [-p | -P] --example kibot [-v...] [-b BOARD] [-d OUT_DIR] [-p | -P] --example
kibot [-v...] --help-list-outputs kibot [-v...] --help-list-outputs
@ -28,6 +28,7 @@ Options:
-c CONFIG, --plot-config CONFIG The plotting config file to use -c CONFIG, --plot-config CONFIG The plotting config file to use
-d OUT_DIR, --out-dir OUT_DIR The output directory [default: .] -d OUT_DIR, --out-dir OUT_DIR The output directory [default: .]
-e SCHEMA, --schematic SCHEMA The schematic file (.sch) -e SCHEMA, --schematic SCHEMA The schematic file (.sch)
-g DEF, --global-redef DEF Overwrite a global value (VAR=VAL)
--help-list-outputs List supported outputs --help-list-outputs List supported outputs
--help-output HELP_OUTPUT Help for this particular output --help-output HELP_OUTPUT Help for this particular output
--help-outputs List supported outputs and details --help-outputs List supported outputs and details
@ -257,6 +258,14 @@ def main():
GS.debug_enabled = logger.getEffectiveLevel() <= DEBUG GS.debug_enabled = logger.getEffectiveLevel() <= DEBUG
GS.debug_level = args.verbose GS.debug_level = args.verbose
# Parse global overwrite options
for redef in args.global_redef:
if '=' not in redef:
logger.error('Malformed global-redef option, must be VARIABLE=VALUE ({})'.format(redef))
sys.exit(EXIT_BAD_ARGS)
var = redef.split('=')[0]
GS.global_from_cli[var] = redef[len(var)+1:]
clean_cache() clean_cache()
# Output dir: relative to CWD (absolute path overrides) # Output dir: relative to CWD (absolute path overrides)

View File

@ -150,17 +150,27 @@ class CfgYamlReader(object):
config_error("In preflight '"+k+"': "+str(e)) config_error("In preflight '"+k+"': "+str(e))
BasePreFlight.add_preflight(o_pre) BasePreFlight.add_preflight(o_pre)
@staticmethod
def _parse_global_str(k, v, current):
if not isinstance(v, str):
config_error("Global `{}` must be a string".format(k))
if current:
logger.info('Using command line value `{}` for global option `{}`'.format(current, k))
return current
return v
def _parse_global(self, gb): def _parse_global(self, gb):
""" Get global options """ """ Get global options """
logger.debug("Parsing global options: {}".format(gb)) logger.debug("Parsing global options: {}".format(gb))
if not isinstance(gb, dict): if not isinstance(gb, dict):
config_error("Incorrect `global` section") config_error("Incorrect `global` section")
# Parse all keys inside it
for k, v in gb.items(): for k, v in gb.items():
if k == 'output': if k == 'output':
if not isinstance(v, str): GS.global_output = self._parse_global_str(k, v, GS.global_output)
config_error("Global `output` must be a string") elif k == 'variant':
GS.global_output = v GS.global_variant = self._parse_global_str(k, v, GS.global_variant)
else: else:
logger.warning("Unknown global option `{}`".format(k)) logger.warning("Unknown global option `{}`".format(k))
@ -174,6 +184,9 @@ class CfgYamlReader(object):
data = yaml.safe_load(fstream) data = yaml.safe_load(fstream)
except yaml.YAMLError as e: except yaml.YAMLError as e:
config_error("Error loading YAML "+str(e)) config_error("Error loading YAML "+str(e))
# Transfer command line global overwrites
GS.global_output = GS.global_from_cli.get('output', None)
GS.global_variant = GS.global_from_cli.get('variant', None)
# List of outputs # List of outputs
outputs = [] outputs = []
version = None version = None

View File

@ -22,12 +22,12 @@ class GS(object):
pcb_file = None # /.../dir/pcb.kicad_pcb pcb_file = None # /.../dir/pcb.kicad_pcb
pcb_no_ext = None # /.../dir/pcb pcb_no_ext = None # /.../dir/pcb
pcb_dir = None # /.../dir pcb_dir = None # /.../dir
pcb_basename = None # file pcb_basename = None # pcb
# SCH name and useful parts # SCH name and useful parts
sch_file = None # /.../dir/file.sch sch_file = None # /.../dir/file.sch
sch_basename = None # /.../dir/file sch_no_ext = None # /.../dir/file
sch_no_ext = None # /.../dir sch_dir = None # /.../dir
sch_dir = None # pcb sch_basename = None # file
# Main output dir # Main output dir
out_dir = None out_dir = None
filter_file = None filter_file = None
@ -56,7 +56,9 @@ class GS(object):
# This value will overwrite GS.def_global_output if defined # This value will overwrite GS.def_global_output if defined
# Classes supporting global "output" option must call super().__init__() # Classes supporting global "output" option must call super().__init__()
# after defining its own options to allow Optionable do the overwrite. # after defining its own options to allow Optionable do the overwrite.
global_from_cli = {}
global_output = None global_output = None
global_variant = None
@staticmethod @staticmethod
def set_sch(name): def set_sch(name):

View File

@ -31,6 +31,8 @@ class Optionable(object):
super().__init__() super().__init__()
if GS.global_output is not None and getattr(self, 'output', None): if GS.global_output is not None and getattr(self, 'output', None):
setattr(self, 'output', GS.global_output) setattr(self, 'output', GS.global_output)
if GS.global_variant is not None and hasattr(self, 'variant'):
setattr(self, 'variant', GS.global_variant)
@staticmethod @staticmethod
def _check_str(key, val, doc): def _check_str(key, val, doc):

View File

@ -74,13 +74,13 @@ class BoMRegex(Optionable):
class VariantOptions(BaseOptions): class VariantOptions(BaseOptions):
""" BaseOptions plus generic support for variants. """ """ BaseOptions plus generic support for variants. """
def __init__(self): def __init__(self):
super().__init__()
with document: with document:
self.variant = '' self.variant = ''
""" Board variant to apply """ """ Board variant to apply """
self.dnf_filter = Optionable self.dnf_filter = Optionable
""" [string|list(string)=''] Name of the filter to mark components as not fitted. """ [string|list(string)=''] Name of the filter to mark components as not fitted.
A short-cut to use for simple cases where a variant is an overkill """ A short-cut to use for simple cases where a variant is an overkill """
super().__init__()
self._comps = None self._comps = None
def config(self): def config(self):