Extended Interpolation of Values with "${this}" pattern // Resolve #3953

This commit is contained in:
Ivan Kravets
2022-04-09 20:31:06 +03:00
parent 5e18f9bbda
commit 965feccfdc
4 changed files with 40 additions and 6 deletions

View File

@ -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 <https://github.com/platformio/platformio-core/issues/3735>`_) - Added a new build variable (``COMPILATIONDB_INCLUDE_TOOLCHAIN``) to include toolchain paths in the compilation database (`issue #3735 <https://github.com/platformio/platformio-core/issues/3735>`_)
- Changed default path for compilation database `compile_commands.json <https://docs.platformio.org/en/latest/integration/compile_commands.html>`__ to the project root - Changed default path for compilation database `compile_commands.json <https://docs.platformio.org/en/latest/integration/compile_commands.html>`__ to the project root
* **Project Configuration**
- Extended `Interpolation of Values <https://docs.platformio.org/en/latest/projectconf/interpolation.html>`__ with ``${this}`` pattern (`issue #3953 <https://github.com/platformio/platformio-core/issues/3953>`_)
- Embed environment name of the current section in the `"platformio.ini" <https://docs.platformio.org/en/latest/projectconf.html>`__ configuration file using ``${this.__env__}`` pattern
* **Miscellaneous** * **Miscellaneous**
- Improved PIO Remote setup on credit-card sized computers (Raspberry Pi, BeagleBon, etc) (`issue #3865 <https://github.com/platformio/platformio-core/issues/3865>`_) - Improved PIO Remote setup on credit-card sized computers (Raspberry Pi, BeagleBon, etc) (`issue #3865 <https://github.com/platformio/platformio-core/issues/3865>`_)

2
docs

Submodule docs updated: bde1247be3...ae9af9e833

View File

@ -254,7 +254,7 @@ class ProjectConfigBase(object):
value = ( value = (
default if default != MISSING else self._parser.get(section, option) 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: if option_meta.sysenvvar:
envvar_value = os.getenv(option_meta.sysenvvar) envvar_value = os.getenv(option_meta.sysenvvar)
@ -277,21 +277,31 @@ class ProjectConfigBase(object):
if value == MISSING: if value == MISSING:
return None 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 ( if (
not value not value
or not isinstance(value, string_types) or not isinstance(value, string_types)
or not all(["${" in value, "}" in value]) or not all(["${" in value, "}" in value])
): ):
return 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) section, option = match.group(1), match.group(2)
# handle system environment variables
if section == "sysenv": if section == "sysenv":
return os.getenv(option) 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: try:
value = self.getraw(section, option) value = self.getraw(section, option)
except RecursionError: except RecursionError:

View File

@ -17,6 +17,7 @@
import configparser import configparser
import os import os
import sys import sys
from pathlib import Path
import pytest import pytest
@ -578,3 +579,21 @@ core_dir = ~/.pio
fs.rmtree(win_core_root_dir) fs.rmtree(win_core_root_dir)
except PermissionError: except PermissionError:
pass 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"]