diff --git a/HISTORY.rst b/HISTORY.rst index f47b6f22..3e4abf98 100644 --- a/HISTORY.rst +++ b/HISTORY.rst @@ -53,6 +53,11 @@ PlatformIO Core 5 - Added a new build variable (``COMPILATIONDB_INCLUDE_TOOLCHAIN``) to include toolchain paths in the compilation database (`issue #3735 `_) - Changed default path for compilation database `compile_commands.json `__ to the project root +* **Project Configuration** + + - Extended `Interpolation of Values `__ with ``${this}`` pattern (`issue #3953 `_) + - Embed environment name of the current section in the `"platformio.ini" `__ configuration file using ``${this.__env__}`` pattern + * **Miscellaneous** - Improved PIO Remote setup on credit-card sized computers (Raspberry Pi, BeagleBon, etc) (`issue #3865 `_) diff --git a/docs b/docs index bde1247b..ae9af9e8 160000 --- a/docs +++ b/docs @@ -1 +1 @@ -Subproject commit bde1247be3f01a63c523d1e43aeb7a949904e653 +Subproject commit ae9af9e83309ca0ea49641b655f66be8328c683b diff --git a/platformio/project/config.py b/platformio/project/config.py index abce4475..2746899c 100644 --- a/platformio/project/config.py +++ b/platformio/project/config.py @@ -254,7 +254,7 @@ class ProjectConfigBase(object): value = ( default if default != MISSING else self._parser.get(section, option) ) - return self._expand_interpolations(value) + return self._expand_interpolations(section, value) if option_meta.sysenvvar: envvar_value = os.getenv(option_meta.sysenvvar) @@ -277,21 +277,31 @@ class ProjectConfigBase(object): if value == MISSING: return None - return self._expand_interpolations(value) + return self._expand_interpolations(section, value) - def _expand_interpolations(self, value): + def _expand_interpolations(self, parent_section, value): if ( not value or not isinstance(value, string_types) or not all(["${" in value, "}" in value]) ): return value - return self.VARTPL_RE.sub(self._re_interpolation_handler, value) + return self.VARTPL_RE.sub( + lambda match: self._re_interpolation_handler(parent_section, match), value + ) - def _re_interpolation_handler(self, match): + def _re_interpolation_handler(self, parent_section, match): section, option = match.group(1), match.group(2) + # handle system environment variables if section == "sysenv": return os.getenv(option) + # handle ${this.*} + if section == "this": + section = parent_section + if option == "__env__": + assert parent_section.startswith("env:") + return parent_section[4:] + # handle nested calls try: value = self.getraw(section, option) except RecursionError: diff --git a/tests/project/test_config.py b/tests/project/test_config.py index 879e681a..02cd4501 100644 --- a/tests/project/test_config.py +++ b/tests/project/test_config.py @@ -17,6 +17,7 @@ import configparser import os import sys +from pathlib import Path import pytest @@ -578,3 +579,21 @@ core_dir = ~/.pio fs.rmtree(win_core_root_dir) except PermissionError: pass + + +def test_this(tmp_path: Path): + project_conf = tmp_path / "platformio.ini" + project_conf.write_text( + """ +[common] +board = uno + +[env:myenv] +extends = common +build_flags = -D${this.__env__} +custom_option = ${this.board} + """ + ) + config = ProjectConfig(str(project_conf)) + assert config.get("env:myenv", "custom_option") == "uno" + assert config.get("env:myenv", "build_flags") == ["-Dmyenv"]