mirror of
https://github.com/platformio/platformio-core.git
synced 2025-07-31 02:27:13 +02:00
Implement project config "update" with "clear" option
This commit is contained in:
@ -225,6 +225,10 @@ class ProjectConfigBase(object):
|
|||||||
value = "\n".join(value)
|
value = "\n".join(value)
|
||||||
if value:
|
if value:
|
||||||
value = "\n" + value # start from a new line
|
value = "\n" + value # start from a new line
|
||||||
|
elif isinstance(value, bool):
|
||||||
|
value = "yes" if value else "no"
|
||||||
|
elif isinstance(value, (int, float)):
|
||||||
|
value = str(value)
|
||||||
self._parser.set(section, option, value)
|
self._parser.set(section, option, value)
|
||||||
|
|
||||||
def getraw(self, section, option):
|
def getraw(self, section, option):
|
||||||
@ -418,6 +422,16 @@ class ProjectConfig(ProjectConfigBase, ProjectConfigDirsMixin):
|
|||||||
def to_json(self):
|
def to_json(self):
|
||||||
return json.dumps(self.as_dict())
|
return json.dumps(self.as_dict())
|
||||||
|
|
||||||
|
def update(self, data, clear=False):
|
||||||
|
assert isinstance(data, list)
|
||||||
|
if clear:
|
||||||
|
self._parser = ConfigParser.ConfigParser()
|
||||||
|
for section, options in data:
|
||||||
|
if not self._parser.has_section(section):
|
||||||
|
self._parser.add_section(section)
|
||||||
|
for option, value in options:
|
||||||
|
self.set(section, option, value)
|
||||||
|
|
||||||
def save(self, path=None):
|
def save(self, path=None):
|
||||||
path = path or self.path
|
path = path or self.path
|
||||||
if path in self._instances:
|
if path in self._instances:
|
||||||
@ -425,3 +439,4 @@ class ProjectConfig(ProjectConfigBase, ProjectConfigDirsMixin):
|
|||||||
with open(path or self.path, "w") as fp:
|
with open(path or self.path, "w") as fp:
|
||||||
fp.write(CONFIG_HEADER)
|
fp.write(CONFIG_HEADER)
|
||||||
self._parser.write(fp)
|
self._parser.write(fp)
|
||||||
|
return True
|
||||||
|
@ -274,84 +274,67 @@ def test_items(config):
|
|||||||
]
|
]
|
||||||
|
|
||||||
|
|
||||||
def test_as_tuple(config):
|
def test_update_and_save(tmpdir_factory):
|
||||||
assert config.as_tuple() == [
|
tmpdir = tmpdir_factory.mktemp("project")
|
||||||
(
|
tmpdir.join("platformio.ini").write(
|
||||||
"platformio",
|
"""
|
||||||
|
[platformio]
|
||||||
|
extra_configs = a.ini, b.ini
|
||||||
|
|
||||||
|
[env:myenv]
|
||||||
|
board = myboard
|
||||||
|
"""
|
||||||
|
)
|
||||||
|
config = ProjectConfig(tmpdir.join("platformio.ini").strpath)
|
||||||
|
assert config.envs() == ["myenv"]
|
||||||
|
assert config.as_tuple()[0][1][0][1] == ["a.ini", "b.ini"]
|
||||||
|
|
||||||
|
config.update(
|
||||||
[
|
[
|
||||||
("extra_configs", ["extra_envs.ini", "extra_debug.ini"]),
|
["platformio", [("extra_configs", ["extra.ini"])]],
|
||||||
("default_envs", ["base", "extra_2"]),
|
["env:myenv", [("framework", ["espidf", "arduino"])]],
|
||||||
("workspace_dir", "/tmp/pio-workspaces/$PROJECT_HASH"),
|
["check_types", [("float_option", 13.99), ("bool_option", True)]],
|
||||||
],
|
]
|
||||||
),
|
)
|
||||||
(
|
config.get("platformio", "extra_configs") == "extra.ini"
|
||||||
"env",
|
config.remove_section("platformio")
|
||||||
[
|
assert config.as_tuple() == [
|
||||||
("monitor_speed", "115200"),
|
("env:myenv", [("board", "myboard"), ("framework", ["espidf", "arduino"])]),
|
||||||
("lib_deps", ["Lib1", "Lib2"]),
|
("check_types", [("float_option", "13.99"), ("bool_option", "yes")]),
|
||||||
("lib_ignore", ["LibIgnoreCustom"]),
|
]
|
||||||
],
|
|
||||||
),
|
config.save()
|
||||||
("strict_ldf", [("lib_ldf_mode", "chain+"), ("lib_compat_mode", "strict")]),
|
lines = [
|
||||||
("monitor_custom", [("monitor_speed", "9600")]),
|
line.strip()
|
||||||
(
|
for line in tmpdir.join("platformio.ini").readlines()
|
||||||
"strict_settings",
|
if line.strip() and not line.startswith((";", "#"))
|
||||||
[
|
]
|
||||||
("extends", "strict_ldf, monitor_custom"),
|
assert lines == [
|
||||||
("build_flags", "-D RELEASE"),
|
"[env:myenv]",
|
||||||
("lib_ldf_mode", "chain+"),
|
"board = myboard",
|
||||||
("lib_compat_mode", "strict"),
|
"framework =",
|
||||||
("monitor_speed", "9600"),
|
"espidf",
|
||||||
],
|
"arduino",
|
||||||
),
|
"[check_types]",
|
||||||
(
|
"float_option = 13.99",
|
||||||
"custom",
|
"bool_option = yes",
|
||||||
[
|
]
|
||||||
("debug_flags", "-D DEBUG=1"),
|
|
||||||
("lib_flags", "-lc -lm"),
|
|
||||||
("extra_flags", None),
|
def test_update_and_clear(tmpdir_factory):
|
||||||
("lib_ignore", "LibIgnoreCustom"),
|
tmpdir = tmpdir_factory.mktemp("project")
|
||||||
],
|
tmpdir.join("platformio.ini").write(
|
||||||
),
|
"""
|
||||||
(
|
[platformio]
|
||||||
"env:base",
|
extra_configs = a.ini, b.ini
|
||||||
[
|
|
||||||
("build_flags", ["-D DEBUG=1"]),
|
[env:myenv]
|
||||||
("targets", []),
|
board = myboard
|
||||||
("monitor_speed", "115200"),
|
"""
|
||||||
("lib_deps", ["Lib1", "Lib2"]),
|
)
|
||||||
("lib_ignore", ["LibIgnoreCustom"]),
|
config = ProjectConfig(tmpdir.join("platformio.ini").strpath)
|
||||||
],
|
assert config.sections() == ["platformio", "env:myenv"]
|
||||||
),
|
config.update([["mysection", [("opt1", "value1"), ("opt2", "value2")]]], clear=True)
|
||||||
(
|
assert config.as_tuple() == [
|
||||||
"env:test_extends",
|
("mysection", [("opt1", "value1"), ("opt2", "value2")])
|
||||||
[
|
|
||||||
("extends", ["strict_settings"]),
|
|
||||||
("build_flags", ["-D RELEASE"]),
|
|
||||||
("lib_ldf_mode", "chain+"),
|
|
||||||
("lib_compat_mode", "strict"),
|
|
||||||
("monitor_speed", "9600"),
|
|
||||||
("lib_deps", ["Lib1", "Lib2"]),
|
|
||||||
("lib_ignore", ["LibIgnoreCustom"]),
|
|
||||||
],
|
|
||||||
),
|
|
||||||
(
|
|
||||||
"env:extra_1",
|
|
||||||
[
|
|
||||||
("build_flags", ["-lc -lm -D DEBUG=1"]),
|
|
||||||
("lib_deps", ["574"]),
|
|
||||||
("monitor_speed", "115200"),
|
|
||||||
("lib_ignore", ["LibIgnoreCustom"]),
|
|
||||||
],
|
|
||||||
),
|
|
||||||
(
|
|
||||||
"env:extra_2",
|
|
||||||
[
|
|
||||||
("build_flags", ["-Og"]),
|
|
||||||
("lib_ignore", ["LibIgnoreCustom", "Lib3"]),
|
|
||||||
("upload_port", "/dev/extra_2/port"),
|
|
||||||
("monitor_speed", "115200"),
|
|
||||||
("lib_deps", ["Lib1", "Lib2"]),
|
|
||||||
],
|
|
||||||
),
|
|
||||||
]
|
]
|
||||||
|
Reference in New Issue
Block a user