From 7345d3ea19c400cdf51f403cb72fcb03c02f25ee Mon Sep 17 00:00:00 2001 From: Ivan Kravets Date: Tue, 5 Nov 2019 00:17:39 +0200 Subject: [PATCH] Improve dump of config data --- .../commands/home/rpc/handlers/project.py | 6 +- platformio/project/config.py | 15 ++-- tests/commands/test_check.py | 1 - tests/test_projectconf.py | 84 +++++++++++++++++-- 4 files changed, 89 insertions(+), 17 deletions(-) diff --git a/platformio/commands/home/rpc/handlers/project.py b/platformio/commands/home/rpc/handlers/project.py index b7981f56..e21331d8 100644 --- a/platformio/commands/home/rpc/handlers/project.py +++ b/platformio/commands/home/rpc/handlers/project.py @@ -45,11 +45,13 @@ class ProjectRPC(object): @staticmethod def config_load(path): - return ProjectConfig(path, parse_extra=False).as_tuple() + return ProjectConfig( + path, parse_extra=False, expand_interpolations=False + ).as_tuple() @staticmethod def config_dump(path, data): - config = ProjectConfig(path, parse_extra=False) + config = ProjectConfig(path, parse_extra=False, expand_interpolations=False) config.update(data, clear=True) return config.save() diff --git a/platformio/project/config.py b/platformio/project/config.py index 0ca94704..524879e3 100644 --- a/platformio/project/config.py +++ b/platformio/project/config.py @@ -92,6 +92,8 @@ class ProjectConfigBase(object): if path and os.path.isfile(path): self.read(path, parse_extra) + self._maintain_renaimed_options() + def __getattr__(self, name): return getattr(self._parser, name) @@ -114,8 +116,6 @@ class ProjectConfigBase(object): for item in glob.glob(pattern): self.read(item) - self._maintain_renaimed_options() - def _maintain_renaimed_options(self): # legacy `lib_extra_dirs` in [platformio] if self._parser.has_section("platformio") and self._parser.has_option( @@ -195,6 +195,9 @@ class ProjectConfigBase(object): if not section: section = "env:" + env + if not self.expand_interpolations: + return self._parser.options(section) + for _, option in self.walk_options(section): if option not in result: result.append(option) @@ -260,7 +263,7 @@ class ProjectConfigBase(object): return os.getenv(option) return self.getraw(section, option) - def get(self, section, option, default=None): + def get(self, section, option, default=None): # pylint: disable=too-many-branches value = None try: value = self.getraw(section, option) @@ -296,12 +299,14 @@ class ProjectConfigBase(object): return default if default is not None else option_meta.default try: - return self._cast_to(value, option_meta.type) + return self.cast_to(value, option_meta.type) except click.BadParameter as e: + if not self.expand_interpolations: + return value raise exception.ProjectOptionValueError(e.format_message(), option, section) @staticmethod - def _cast_to(value, to_type): + def cast_to(value, to_type): items = value if not isinstance(value, (list, tuple)): items = [value] diff --git a/tests/commands/test_check.py b/tests/commands/test_check.py index 920ef5fd..64e4602e 100644 --- a/tests/commands/test_check.py +++ b/tests/commands/test_check.py @@ -344,4 +344,3 @@ int main() { assert high_result.exit_code == 0 assert low_result.exit_code != 0 - diff --git a/tests/test_projectconf.py b/tests/test_projectconf.py index a10b33a3..cfede890 100644 --- a/tests/test_projectconf.py +++ b/tests/test_projectconf.py @@ -28,7 +28,8 @@ extra_configs = # global options per [env:*] [env] -monitor_speed = 115200 ; inline comment +monitor_speed = 9600 ; inline comment +custom_monitor_speed = 115200 lib_deps = Lib1 ; inline comment in multi-line value Lib2 @@ -39,7 +40,7 @@ lib_ldf_mode = chain+ lib_compat_mode = strict [monitor_custom] -monitor_speed = 9600 +monitor_speed = ${env.custom_monitor_speed} [strict_settings] extends = strict_ldf, monitor_custom @@ -53,11 +54,13 @@ lib_ignore = LibIgnoreCustom [env:base] build_flags = ${custom.debug_flags} ${custom.extra_flags} +lib_compat_mode = ${strict_ldf.strict} targets = [env:test_extends] extends = strict_settings + """ EXTRA_ENVS_CONFIG = """ @@ -146,8 +149,10 @@ def test_envs(config): def test_options(config): assert config.options(env="base") == [ "build_flags", + "lib_compat_mode", "targets", "monitor_speed", + "custom_monitor_speed", "lib_deps", "lib_ignore", ] @@ -157,6 +162,7 @@ def test_options(config): "lib_ldf_mode", "lib_compat_mode", "monitor_speed", + "custom_monitor_speed", "lib_deps", "lib_ignore", ] @@ -192,6 +198,7 @@ def test_sysenv_options(config): "lib_ldf_mode", "lib_compat_mode", "monitor_speed", + "custom_monitor_speed", "lib_deps", "lib_ignore", "upload_port", @@ -223,15 +230,15 @@ def test_getraw_value(config): # extended assert config.getraw("env:test_extends", "lib_ldf_mode") == "chain+" - assert config.getraw("env", "monitor_speed") == "115200" - assert config.getraw("env:test_extends", "monitor_speed") == "9600" + assert config.getraw("env", "monitor_speed") == "9600" + assert config.getraw("env:test_extends", "monitor_speed") == "115200" def test_get_value(config): assert config.get("custom", "debug_flags") == "-D DEBUG=1" assert config.get("env:extra_1", "build_flags") == ["-lc -lm -D DEBUG=1"] assert config.get("env:extra_2", "build_flags") == ["-Og"] - assert config.get("env:extra_2", "monitor_speed") == 115200 + assert config.get("env:extra_2", "monitor_speed") == 9600 assert config.get("env:base", "build_flags") == ["-D DEBUG=1"] @@ -244,22 +251,26 @@ def test_items(config): ] assert config.items(env="base") == [ ("build_flags", ["-D DEBUG=1"]), + ("lib_compat_mode", "soft"), ("targets", []), - ("monitor_speed", 115200), + ("monitor_speed", 9600), + ("custom_monitor_speed", "115200"), ("lib_deps", ["Lib1", "Lib2"]), ("lib_ignore", ["LibIgnoreCustom"]), ] assert config.items(env="extra_1") == [ ("build_flags", ["-lc -lm -D DEBUG=1"]), ("lib_deps", ["574"]), - ("monitor_speed", 115200), + ("monitor_speed", 9600), + ("custom_monitor_speed", "115200"), ("lib_ignore", ["LibIgnoreCustom"]), ] assert config.items(env="extra_2") == [ ("build_flags", ["-Og"]), ("lib_ignore", ["LibIgnoreCustom", "Lib3"]), ("upload_port", "/dev/extra_2/port"), - ("monitor_speed", 115200), + ("monitor_speed", 9600), + ("custom_monitor_speed", "115200"), ("lib_deps", ["Lib1", "Lib2"]), ] assert config.items(env="test_extends") == [ @@ -267,7 +278,8 @@ def test_items(config): ("build_flags", ["-D RELEASE"]), ("lib_ldf_mode", "chain+"), ("lib_compat_mode", "strict"), - ("monitor_speed", 9600), + ("monitor_speed", 115200), + ("custom_monitor_speed", "115200"), ("lib_deps", ["Lib1", "Lib2"]), ("lib_ignore", ["LibIgnoreCustom"]), ] @@ -337,3 +349,57 @@ board = myboard assert config.as_tuple() == [ ("mysection", [("opt1", "value1"), ("opt2", "value2")]) ] + + +def test_dump(tmpdir_factory): + tmpdir = tmpdir_factory.mktemp("project") + tmpdir.join("platformio.ini").write(BASE_CONFIG) + tmpdir.join("extra_envs.ini").write(EXTRA_ENVS_CONFIG) + tmpdir.join("extra_debug.ini").write(EXTRA_DEBUG_CONFIG) + config = ProjectConfig( + tmpdir.join("platformio.ini").strpath, + parse_extra=False, + expand_interpolations=False, + ) + assert config.as_tuple() == [ + ( + "platformio", + [ + ("extra_configs", ["extra_envs.ini", "extra_debug.ini"]), + ("default_envs", ["base", "extra_2"]), + ], + ), + ( + "env", + [ + ("monitor_speed", 9600), + ("custom_monitor_speed", "115200"), + ("lib_deps", ["Lib1", "Lib2"]), + ("lib_ignore", ["${custom.lib_ignore}"]), + ], + ), + ("strict_ldf", [("lib_ldf_mode", "chain+"), ("lib_compat_mode", "strict")]), + ("monitor_custom", [("monitor_speed", "${env.custom_monitor_speed}")]), + ( + "strict_settings", + [("extends", "strict_ldf, monitor_custom"), ("build_flags", "-D RELEASE")], + ), + ( + "custom", + [ + ("debug_flags", "-D RELEASE"), + ("lib_flags", "-lc -lm"), + ("extra_flags", "${sysenv.__PIO_TEST_CNF_EXTRA_FLAGS}"), + ("lib_ignore", "LibIgnoreCustom"), + ], + ), + ( + "env:base", + [ + ("build_flags", ["${custom.debug_flags} ${custom.extra_flags}"]), + ("lib_compat_mode", "${strict_ldf.strict}"), + ("targets", []), + ], + ), + ("env:test_extends", [("extends", ["strict_settings"])]), + ]