Merge pull request #507 from corrscope/fix-yaml-write

Unify writing config files (atomically) and global preferences (in UTF-8)
pull/509/head
nyanpasu64 2025-05-15 00:52:00 -07:00 zatwierdzone przez GitHub
commit 4e39934b69
Nie znaleziono w bazie danych klucza dla tego podpisu
ID klucza GPG: B5690EEEBB952194
3 zmienionych plików z 13 dodań i 8 usunięć

Wyświetl plik

@ -14,6 +14,8 @@
### Changelog
- When opening missing file via CLI, show dialog rather than crashing (#499)
- Fix saving global settings after opening config in Unicode folder (#507)
- Save config files atomically (#507)
## 0.10.1

Wyświetl plik

@ -19,10 +19,10 @@ from typing import (
Any,
TextIO,
Union,
IO,
)
import attr
from atomicwrites import atomic_write
from ruamel.yaml import (
yaml_object,
YAML,
@ -61,9 +61,14 @@ class MyYAML(YAML):
# https://bitbucket.org/ruamel/yaml/issues/316/unicode-encoding-decoding-errors-on
# Both are bad, so use UTF-8.
if isinstance(stream, Path):
with stream.open("w", encoding="utf-8") as f:
path = stream
with atomic_write(path, overwrite=True, encoding="utf-8") as f:
self.dump_without_corrupting(data, f, **kwargs)
elif isinstance(stream, TextIO):
# Nobody actually calls dump(..., open()). This branch is never taken.
self.dump_without_corrupting(data, stream, **kwargs)
elif stream is None:
# Possibly only called in unit tests, not in production.
stream = StringIO()
@ -71,8 +76,9 @@ class MyYAML(YAML):
return stream.getvalue()
else:
# with atomic_write(...) as f: dump(..., f)
self.dump_without_corrupting(data, stream, **kwargs)
raise TypeError(
f"stream must be {{Path, TextIO=open(), None}}, but is {type(stream)}"
)
def dump_without_corrupting(self, *args, **kwargs):
YAML.dump(self, *args, **kwargs)

Wyświetl plik

@ -1,8 +1,6 @@
from typing import *
import attr
from atomicwrites import atomic_write
from corrscope.config import DumpableAttrs, yaml
from corrscope.settings import paths
@ -75,5 +73,4 @@ def load_prefs() -> GlobalPrefs:
def dump_prefs(pref: GlobalPrefs) -> None:
with atomic_write(_PREF_PATH, overwrite=True) as f:
yaml.dump(pref, f)
yaml.dump(pref, _PREF_PATH)