From 4e72af9761b9e781e31450728da279500ac26182 Mon Sep 17 00:00:00 2001 From: "Salvador E. Tropea" Date: Fri, 28 Apr 2023 13:46:58 -0300 Subject: [PATCH] [Added] OS environment expansion in ${VAR} --- CHANGELOG.md | 6 ++++++ README.md | 1 + kibot/globals.py | 2 ++ kibot/gs.py | 3 +++ kibot/kicad/config.py | 9 +++++++-- 5 files changed, 19 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index fc005691..0681724a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,12 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). ## [1.6.3] - UNRELEASED +### Added +- General: + - OS environment expansion in ${VAR} +- Global options: + - `use_os_env_for_expand` to disable OS environment expansion + ### Changed - KiCad v6/7 schematic: - The hierarchy is expanded only if needed, i.e. value of an instance changed diff --git a/README.md b/README.md index 533628c2..6bfe0074 100644 --- a/README.md +++ b/README.md @@ -866,6 +866,7 @@ global: - `units`: [string=''] [millimeters,inches,mils] Default units. Affects `position`, `bom` and `panelize` outputs. Also KiCad 6 dimensions. - `use_dir_for_preflights`: [boolean=true] Use the global `dir` as subdir for the preflights. + - `use_os_env_for_expand`: [boolean=true] In addition to KiCad text variables also use the OS environment variables when expanding ${VARIABLE}. - `variant`: [string=''] Default variant to apply to all outputs. diff --git a/kibot/globals.py b/kibot/globals.py index ead90def..bdc20c59 100644 --- a/kibot/globals.py +++ b/kibot/globals.py @@ -283,6 +283,8 @@ class Globals(FiltersOptions): Note this is mainly useful for CI/CD, so you can store fonts and colors in your repo. Also note that the fonts are installed using a mechanism known to work on Debian, which is used by the KiBot docker images, on other OSs *your mileage may vary* """ + self.use_os_env_for_expand = True + """ In addition to KiCad text variables also use the OS environment variables when expanding ${VARIABLE} """ self.set_doc('filters', " [list(dict)] KiBot warnings to be ignored ") self._filter_what = 'KiBot warnings' self.filters = FilterOptionsKiBot diff --git a/kibot/gs.py b/kibot/gs.py index ad85db72..1fb770af 100644 --- a/kibot/gs.py +++ b/kibot/gs.py @@ -170,6 +170,7 @@ class GS(object): global_time_reformat = None global_units = None global_use_dir_for_preflights = None + global_use_os_env_for_expand = None global_variant = None # Only for v7+ global_allow_blind_buried_vias = None @@ -389,6 +390,8 @@ class GS(object): value = vars.get(vname, None) if value is None and extra_vars is not None: value = extra_vars.get(vname, None) + if value is None and GS.global_use_os_env_for_expand: + value = os.environ.get(vname, None) if value is None: value = '${'+vname+'}' logger.warning(W_UNKVAR+"Unknown text variable `{}`".format(vname)) diff --git a/kibot/kicad/config.py b/kibot/kicad/config.py index 8bd01079..50613934 100644 --- a/kibot/kicad/config.py +++ b/kibot/kicad/config.py @@ -95,11 +95,16 @@ def expand_env(val, env, extra_env, used_extra=None): logger.warning(W_MAXDEPTH+'Too much nested variables replacements, possible loop ({})'.format(ori_val)) success = False for var in re.findall(r'\$\{(\S+?)\}', val): + to_replace = '${'+var+'}' if var in env: - val = val.replace('${'+var+'}', env[var]) + val = val.replace(to_replace, env[var]) replaced = True elif var in extra_env: - val = val.replace('${'+var+'}', extra_env[var]) + val = val.replace(to_replace, extra_env[var]) + used_extra[0] = True + replaced = True + elif GS.global_use_os_env_for_expand and var in os.environ: + val = val.replace(to_replace, os.environ[var]) used_extra[0] = True replaced = True else: