[Global options][Added] `environment`.`extra_os` to define environment variables

This commit is contained in:
Salvador E. Tropea 2023-05-02 13:12:50 -03:00
parent 4e72af9761
commit 4fbd69ac3b
5 changed files with 51 additions and 3 deletions

View File

@ -10,6 +10,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- OS environment expansion in ${VAR}
- Global options:
- `use_os_env_for_expand` to disable OS environment expansion
- `environment`.`extra_os` to define environment variables
### Changed
- KiCad v6/7 schematic:

View File

@ -786,7 +786,12 @@ global:
The KIPRJMOD is also available for expansion.
* Valid keys:
- `define_old`: [boolean=false] Also define legacy versions of the variables.
Useful when using KiCad 6 and some libs uses old KiCad 5 names.
Useful when using KiCad 6+ and some libs uses old KiCad 5 names.
- `extra_os`: [list(dict)] Extra variables to export as OS environment variables.
Note that you can also define them using `- NAME: VALUE`.
* Valid keys:
- **`name`**: [string=''] Name of the variable.
- **`value`**: [string=''] Value for the variable.
- `footprints`: [string=''] System level footprints (aka modules) dir. KiCad 5: KICAD_FOOTPRINT_DIR and KISYSMOD.
KiCad 6: KICAD6_FOOTPRINT_DIR.
- `models_3d`: [string=''] System level 3D models dir. KiCad 5: KISYS3DMOD. KiCad 6: KICAD6_3DMODEL_DIR.

View File

@ -11,12 +11,40 @@ from .kicad.config import expand_env
from .macros import macros, document # noqa: F401
from .pre_filters import FiltersOptions, FilterOptionsKiBot
from .log import get_logger, set_filters
from .misc import W_MUSTBEINT
from .misc import W_MUSTBEINT, W_ENVEXIST
from .kicad.config import KiConf
from .kicad.sexpdata import load, SExpData, sexp_iter, Symbol
from .kicad.v6_sch import PCBLayer
class OSVariables(Optionable):
""" Name/Value pairs """
def __init__(self):
super().__init__()
self._unknown_is_error = True
with document:
self.name = ''
""" *Name of the variable """
self.value = ''
""" *Value for the variable """
self._name_example = 'ROOT_DIR'
self._value_example = '/root'
def config(self, parent):
# Support for - NAME: VALUE
if len(self._tree) == 1:
v0 = tuple(self._tree.values())[0]
n0 = tuple(self._tree.keys())[0]
if n0 != 'name' and n0 != 'value' and isinstance(v0, str):
logger.error(self._tree)
self.name = n0
self.value = v0
return
super().config(parent)
if not self.name:
raise KiPlotConfigurationError("Missing or empty `name` in environment variable ({})".format(str(self._tree)))
class Environment(Optionable):
""" Used to define the KiCad environment vars """
def __init__(self):
@ -38,7 +66,10 @@ class Environment(Optionable):
""" 3rd party dir. KiCad 6: KICAD6_3RD_PARTY """
self.define_old = False
""" Also define legacy versions of the variables.
Useful when using KiCad 6 and some libs uses old KiCad 5 names """
Useful when using KiCad 6+ and some libs uses old KiCad 5 names """
self.extra_os = OSVariables
""" [list(dict)] Extra variables to export as OS environment variables.
Note that you can also define them using `- NAME: VALUE` """
def define_k5_vars(self, defs):
if self.symbols:
@ -89,6 +120,12 @@ class Environment(Optionable):
v = expand_env(v, env, os.environ)
logger.debug('- {} = "{}"'.format(n, v))
os.environ[n] = v
if isinstance(self.extra_os, list):
for v in self.extra_os:
if v.name in os.environ:
logger.warning(W_ENVEXIST+"Overwriting {} environment variable".format(v.name))
logger.debug('- {} = "{}"'.format(v.name, v.value))
os.environ[v.name] = v.value
class KiCadAlias(Optionable):

View File

@ -273,6 +273,7 @@ W_RESVALISSUE = '(W124) '
W_RES3DNAME = '(W125) '
W_ESCINV = '(W126) '
W_BADVAL4 = '(W127) '
W_ENVEXIST = '(W128) '
# Somehow arbitrary, the colors are real, but can be different
PCB_MAT_COLORS = {'fr1': "937042", 'fr2': "949d70", 'fr3': "adacb4", 'fr4': "332B16", 'fr5': "6cc290"}
PCB_FINISH_COLORS = {'hal': "8b898c", 'hasl': "8b898c", 'imag': "8b898c", 'enig': "cfb96e", 'enepig': "cfb96e",

View File

@ -10,6 +10,10 @@ globals:
user_templates: '/test4'
third_party: '/test5'
define_old: true
extra_os:
- name: TEST1
value: Hello
- TEST2: Bye
outputs:
- name: Step