From 512776ce8e37d705c7f1c4177e79d033ae281bcd Mon Sep 17 00:00:00 2001 From: "Salvador E. Tropea" Date: Thu, 10 Sep 2020 12:56:53 -0300 Subject: [PATCH] Added a mechanism to set a default variant for all outputs. It can be used to overwrite the output file template too. --- kibot/__main__.py | 11 ++++++++++- kibot/config_reader.py | 21 +++++++++++++++++---- kibot/gs.py | 10 ++++++---- kibot/optionable.py | 2 ++ kibot/out_base.py | 2 +- 5 files changed, 36 insertions(+), 10 deletions(-) diff --git a/kibot/__main__.py b/kibot/__main__.py index 75728672..75704e65 100644 --- a/kibot/__main__.py +++ b/kibot/__main__.py @@ -9,7 +9,7 @@ Usage: 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...] [-b BOARD] [-d OUT_DIR] [-p | -P] --example kibot [-v...] --help-list-outputs @@ -28,6 +28,7 @@ Options: -c CONFIG, --plot-config CONFIG The plotting config file to use -d OUT_DIR, --out-dir OUT_DIR The output directory [default: .] -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-output HELP_OUTPUT Help for this particular output --help-outputs List supported outputs and details @@ -257,6 +258,14 @@ def main(): GS.debug_enabled = logger.getEffectiveLevel() <= DEBUG 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() # Output dir: relative to CWD (absolute path overrides) diff --git a/kibot/config_reader.py b/kibot/config_reader.py index 3726dcd0..09b07683 100644 --- a/kibot/config_reader.py +++ b/kibot/config_reader.py @@ -150,17 +150,27 @@ class CfgYamlReader(object): config_error("In preflight '"+k+"': "+str(e)) 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): """ Get global options """ logger.debug("Parsing global options: {}".format(gb)) if not isinstance(gb, dict): config_error("Incorrect `global` section") - + # Parse all keys inside it for k, v in gb.items(): if k == 'output': - if not isinstance(v, str): - config_error("Global `output` must be a string") - GS.global_output = v + GS.global_output = self._parse_global_str(k, v, GS.global_output) + elif k == 'variant': + GS.global_variant = self._parse_global_str(k, v, GS.global_variant) else: logger.warning("Unknown global option `{}`".format(k)) @@ -174,6 +184,9 @@ class CfgYamlReader(object): data = yaml.safe_load(fstream) except yaml.YAMLError as 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 outputs = [] version = None diff --git a/kibot/gs.py b/kibot/gs.py index 1dac6c3f..31f35d8c 100644 --- a/kibot/gs.py +++ b/kibot/gs.py @@ -22,12 +22,12 @@ class GS(object): pcb_file = None # /.../dir/pcb.kicad_pcb pcb_no_ext = None # /.../dir/pcb pcb_dir = None # /.../dir - pcb_basename = None # file + pcb_basename = None # pcb # SCH name and useful parts sch_file = None # /.../dir/file.sch - sch_basename = None # /.../dir/file - sch_no_ext = None # /.../dir - sch_dir = None # pcb + sch_no_ext = None # /.../dir/file + sch_dir = None # /.../dir + sch_basename = None # file # Main output dir out_dir = None filter_file = None @@ -56,7 +56,9 @@ class GS(object): # This value will overwrite GS.def_global_output if defined # Classes supporting global "output" option must call super().__init__() # after defining its own options to allow Optionable do the overwrite. + global_from_cli = {} global_output = None + global_variant = None @staticmethod def set_sch(name): diff --git a/kibot/optionable.py b/kibot/optionable.py index 5d3b00d9..3f3b745c 100644 --- a/kibot/optionable.py +++ b/kibot/optionable.py @@ -31,6 +31,8 @@ class Optionable(object): super().__init__() if GS.global_output is not None and getattr(self, 'output', None): setattr(self, 'output', GS.global_output) + if GS.global_variant is not None and hasattr(self, 'variant'): + setattr(self, 'variant', GS.global_variant) @staticmethod def _check_str(key, val, doc): diff --git a/kibot/out_base.py b/kibot/out_base.py index 8964bec9..337134ef 100644 --- a/kibot/out_base.py +++ b/kibot/out_base.py @@ -74,13 +74,13 @@ class BoMRegex(Optionable): class VariantOptions(BaseOptions): """ BaseOptions plus generic support for variants. """ def __init__(self): - super().__init__() with document: self.variant = '' """ Board variant to apply """ self.dnf_filter = Optionable """ [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 """ + super().__init__() self._comps = None def config(self):