From 4fbd69ac3bbb15aa9f40769d952f11d4603227e8 Mon Sep 17 00:00:00 2001 From: "Salvador E. Tropea" Date: Tue, 2 May 2023 13:12:50 -0300 Subject: [PATCH] [Global options][Added] `environment`.`extra_os` to define environment variables --- CHANGELOG.md | 1 + README.md | 7 +++- kibot/globals.py | 41 +++++++++++++++++++++-- kibot/misc.py | 1 + tests/yaml_samples/step_gl_env.kibot.yaml | 4 +++ 5 files changed, 51 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 0681724a..2a554aa6 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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: diff --git a/README.md b/README.md index 6bfe0074..e6e7f3f9 100644 --- a/README.md +++ b/README.md @@ -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. diff --git a/kibot/globals.py b/kibot/globals.py index bdc20c59..fa3e2770 100644 --- a/kibot/globals.py +++ b/kibot/globals.py @@ -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): diff --git a/kibot/misc.py b/kibot/misc.py index 37a8f843..306f755f 100644 --- a/kibot/misc.py +++ b/kibot/misc.py @@ -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", diff --git a/tests/yaml_samples/step_gl_env.kibot.yaml b/tests/yaml_samples/step_gl_env.kibot.yaml index 26ff1fc1..2b5b9e28 100644 --- a/tests/yaml_samples/step_gl_env.kibot.yaml +++ b/tests/yaml_samples/step_gl_env.kibot.yaml @@ -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