[Added] OS environment expansion in ${VAR}

This commit is contained in:
Salvador E. Tropea 2023-04-28 13:46:58 -03:00
parent e4ba2d7d8a
commit 4e72af9761
5 changed files with 19 additions and 2 deletions

View File

@ -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

View File

@ -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.

View File

@ -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

View File

@ -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))

View File

@ -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: