mirror of
https://github.com/platformio/platformio-core.git
synced 2025-07-30 01:57:13 +02:00
Improve project config parser to resolve renamed options // Issue #4259
This commit is contained in:
2
docs
2
docs
Submodule docs updated: a997e10df9...1759320cdc
2
examples
2
examples
Submodule examples updated: 042c58bb71...c328c386fe
@ -132,7 +132,7 @@ class ProjectConfigBase(object):
|
|||||||
renamed_options.update({name: option.name for name in option.oldnames})
|
renamed_options.update({name: option.name for name in option.oldnames})
|
||||||
|
|
||||||
for section in self._parser.sections():
|
for section in self._parser.sections():
|
||||||
scope = section.split(":", 1)[0]
|
scope = self.get_section_scope(section)
|
||||||
if scope not in ("platformio", "env"):
|
if scope not in ("platformio", "env"):
|
||||||
continue
|
continue
|
||||||
for option in self._parser.options(section):
|
for option in self._parser.options(section):
|
||||||
@ -164,6 +164,10 @@ class ProjectConfigBase(object):
|
|||||||
)
|
)
|
||||||
return True
|
return True
|
||||||
|
|
||||||
|
@staticmethod
|
||||||
|
def get_section_scope(section):
|
||||||
|
return section.split(":", 1)[0] if ":" in section else section
|
||||||
|
|
||||||
def walk_options(self, root_section):
|
def walk_options(self, root_section):
|
||||||
extends_queue = (
|
extends_queue = (
|
||||||
["env", root_section] if root_section.startswith("env:") else [root_section]
|
["env", root_section] if root_section.startswith("env:") else [root_section]
|
||||||
@ -195,7 +199,7 @@ class ProjectConfigBase(object):
|
|||||||
result.append(option)
|
result.append(option)
|
||||||
|
|
||||||
# handle system environment variables
|
# handle system environment variables
|
||||||
scope = section.split(":", 1)[0]
|
scope = self.get_section_scope(section)
|
||||||
for option_meta in ProjectOptions.values():
|
for option_meta in ProjectOptions.values():
|
||||||
if option_meta.scope != scope or option_meta.name in result:
|
if option_meta.scope != scope or option_meta.name in result:
|
||||||
continue
|
continue
|
||||||
@ -233,9 +237,29 @@ class ProjectConfigBase(object):
|
|||||||
value = "\n" + value
|
value = "\n" + value
|
||||||
self._parser.set(section, option, value)
|
self._parser.set(section, option, value)
|
||||||
|
|
||||||
def getraw( # pylint: disable=too-many-branches
|
def getraw(self, section, option, default=MISSING):
|
||||||
self, section, option, default=MISSING
|
try:
|
||||||
):
|
return self._getraw(section, option, default)
|
||||||
|
except configparser.NoOptionError as exc:
|
||||||
|
renamed_option = self._resolve_renamed_option(section, option)
|
||||||
|
if renamed_option:
|
||||||
|
return self._getraw(section, renamed_option, default)
|
||||||
|
raise exc
|
||||||
|
|
||||||
|
def _resolve_renamed_option(self, section, old_name):
|
||||||
|
scope = self.get_section_scope(section)
|
||||||
|
if scope not in ("platformio", "env"):
|
||||||
|
return None
|
||||||
|
for option_meta in ProjectOptions.values():
|
||||||
|
if (
|
||||||
|
option_meta.oldnames
|
||||||
|
and option_meta.scope == scope
|
||||||
|
and old_name in option_meta.oldnames
|
||||||
|
):
|
||||||
|
return option_meta.name
|
||||||
|
return None
|
||||||
|
|
||||||
|
def _getraw(self, section, option, default): # pylint: disable=too-many-branches
|
||||||
if not self.expand_interpolations:
|
if not self.expand_interpolations:
|
||||||
return self._parser.get(section, option)
|
return self._parser.get(section, option)
|
||||||
|
|
||||||
@ -245,7 +269,9 @@ class ProjectConfigBase(object):
|
|||||||
value = self._parser.get(sec, option)
|
value = self._parser.get(sec, option)
|
||||||
break
|
break
|
||||||
|
|
||||||
option_meta = ProjectOptions.get("%s.%s" % (section.split(":", 1)[0], option))
|
option_meta = ProjectOptions.get(
|
||||||
|
"%s.%s" % (self.get_section_scope(section), option)
|
||||||
|
)
|
||||||
if not option_meta:
|
if not option_meta:
|
||||||
if value == MISSING:
|
if value == MISSING:
|
||||||
value = (
|
value = (
|
||||||
@ -316,7 +342,9 @@ class ProjectConfigBase(object):
|
|||||||
except configparser.Error as e:
|
except configparser.Error as e:
|
||||||
raise exception.InvalidProjectConfError(self.path, str(e))
|
raise exception.InvalidProjectConfError(self.path, str(e))
|
||||||
|
|
||||||
option_meta = ProjectOptions.get("%s.%s" % (section.split(":", 1)[0], option))
|
option_meta = ProjectOptions.get(
|
||||||
|
"%s.%s" % (self.get_section_scope(section), option)
|
||||||
|
)
|
||||||
if not option_meta:
|
if not option_meta:
|
||||||
return value
|
return value
|
||||||
|
|
||||||
|
@ -310,6 +310,9 @@ def test_getraw_value(config):
|
|||||||
)
|
)
|
||||||
assert config.getraw("platformio", "build_dir") == "~/tmp/pio-$PROJECT_HASH"
|
assert config.getraw("platformio", "build_dir") == "~/tmp/pio-$PROJECT_HASH"
|
||||||
|
|
||||||
|
# renamed option
|
||||||
|
assert config.getraw("env:base", "debug_load_cmd") == ["load"]
|
||||||
|
|
||||||
|
|
||||||
def test_get_value(config):
|
def test_get_value(config):
|
||||||
assert config.get("custom", "debug_flags") == "-D DEBUG=1"
|
assert config.get("custom", "debug_flags") == "-D DEBUG=1"
|
||||||
|
Reference in New Issue
Block a user