Add Additional Config Stability

This commit is contained in:
David Sangrey committed 2026-01-25 10:43:18 -05:00
1 parent 6f8c23d140
commit 884e91deae
1 file changed
+16 -3
+16 -3
View File
@@ -264,8 +264,15 @@ class Config:
tomli_w.dump(default_data, f)
# Load the TOML file
with self.toml_path.open("rb") as f:
data = tomllib.load(f)
try:
with self.toml_path.open("rb") as f:
data = tomllib.load(f)
except tomllib.TOMLDecodeError:
# rename broken file
self.toml_path.rename(self.toml_path.with_suffix(".toml.broken"))
# regenerate defaults
data = {"generated": "", "source": "", "settings": {}}
self.save()
# Capture metadata, fallback to empty string if missing
self.generated = data.get("generated", "")
@@ -516,8 +523,14 @@ class Config:
"settings": self.settings,
}
with self.toml_path.open("wb") as f:
tmp = self.toml_path.with_suffix(".toml.tmp")
with tmp.open("wb") as f:
tomli_w.dump(data, f)
f.flush()
os.fsync(f.fileno())
tmp.replace(self.toml_path)
def close(self) -> None:
"""Save config changes before closing."""