Skip to content

Commit 71fb2af

Browse files
committed
Add Config.unset_value
1 parent e65fdba commit 71fb2af

3 files changed

Lines changed: 34 additions & 0 deletions

File tree

‎ceph_devstack/__init__.py‎

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,8 @@ def parse_args(args: List[str]) -> argparse.Namespace:
5050
parser_config_set = subparsers_config.add_parser("set")
5151
parser_config_set.add_argument("name")
5252
parser_config_set.add_argument("value")
53+
parser_config_unset = subparsers_config.add_parser("unset")
54+
parser_config_unset.add_argument("name")
5355
parser_doc = subparsers.add_parser(
5456
"doctor", help="Check that the system meets requirements"
5557
)
@@ -129,6 +131,8 @@ class Config(dict):
129131
__slots__ = ["user_obj", "user_path"]
130132

131133
def load(self, config_path: Path | None = None):
134+
args = self.get("args")
135+
self.clear()
132136
parsed = tomlkit.parse((Path(__file__).parent / "config.toml").read_text())
133137
self.update(parsed)
134138
if config_path:
@@ -140,6 +144,8 @@ def load(self, config_path: Path | None = None):
140144
raise OSError(f"Config file at {self.user_path} not found!")
141145
else:
142146
self.user_obj = {}
147+
if args:
148+
self["args"] = args
143149

144150
def dump(self):
145151
return tomlkit.dumps(self)
@@ -182,6 +188,24 @@ def set_value(self, name: str, value: str) -> None:
182188
self.user_path.write_text(tomlkit.dumps(self.user_obj).strip())
183189
i += 1
184190

191+
def unset_value(self, name: str) -> None:
192+
path = name.split(".")
193+
obj = self.user_obj
194+
i = 0
195+
last_index = len(path) - 1
196+
while i <= last_index:
197+
if i < last_index:
198+
if path[i] not in obj:
199+
break
200+
obj = obj[path[i]]
201+
elif i == last_index:
202+
obj.pop(path[i])
203+
self.update(self.user_obj)
204+
self.user_path.parent.mkdir(exist_ok=True)
205+
self.user_path.write_text(tomlkit.dumps(self.user_obj).strip())
206+
i += 1
207+
self.load(self.user_path)
208+
185209

186210
config = Config()
187211
config.load()

‎ceph_devstack/cli.py‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
"dump": lambda config, args: print(config.dump()),
1313
"get": lambda config, args: print(config.get_value(args.name)),
1414
"set": lambda config, args: print(config.set_value(args.name, args.value)),
15+
"unset": lambda config, args: config.unset_value(args.name),
1516
}
1617

1718
COMMAND_HANDLERS = {

‎tests/test_config.py‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,15 @@ def test_set_value_overrides_existing(self, test_config):
8888
assert test_config["containers"]["testnode"]["count"] == new_count
8989

9090

91+
class TestConfigUnset:
92+
def test_unset_value_simple_key(self, test_config):
93+
test_config.set_value("test_key", "test_value")
94+
assert "test_key" in test_config
95+
assert test_config["test_key"] == "test_value"
96+
test_config.unset_value("test_key")
97+
assert "test_key" not in test_config
98+
99+
91100
class TestConfigDefaults:
92101
def test_config_defaults(self):
93102
assert config == {

0 commit comments

Comments
 (0)