DRYer
This commit is contained in:
parent
6571cf2e89
commit
b293e4d516
@ -4,12 +4,11 @@
|
|||||||
|
|
||||||
|
|
||||||
import os
|
import os
|
||||||
import shutil
|
|
||||||
import time
|
import time
|
||||||
from contextlib import suppress
|
from contextlib import suppress
|
||||||
from tempfile import mkstemp
|
|
||||||
from typing import Optional, Union
|
from typing import Optional, Union
|
||||||
|
|
||||||
|
from .config import atomic_save
|
||||||
from .constants import shell_integration_dir
|
from .constants import shell_integration_dir
|
||||||
from .fast_data_types import get_options
|
from .fast_data_types import get_options
|
||||||
from .types import run_once
|
from .types import run_once
|
||||||
@ -23,19 +22,9 @@ if test -e {path}; then source {path}; fi
|
|||||||
|
|
||||||
|
|
||||||
def atomic_write(path: str, data: Union[str, bytes]) -> None:
|
def atomic_write(path: str, data: Union[str, bytes]) -> None:
|
||||||
mode = 'w' + ('b' if isinstance(data, bytes) else '')
|
if isinstance(data, str):
|
||||||
base = os.path.dirname(path)
|
data = data.encode('utf-8')
|
||||||
os.makedirs(base, exist_ok=True)
|
atomic_save(data, path)
|
||||||
fd, tpath = mkstemp(dir=base, text=isinstance(data, str))
|
|
||||||
with open(fd, mode) as f:
|
|
||||||
with suppress(FileNotFoundError):
|
|
||||||
shutil.copystat(path, tpath)
|
|
||||||
f.write(data)
|
|
||||||
try:
|
|
||||||
os.rename(tpath, path)
|
|
||||||
except OSError:
|
|
||||||
os.unlink(tpath)
|
|
||||||
raise
|
|
||||||
|
|
||||||
|
|
||||||
def safe_read(path: str) -> str:
|
def safe_read(path: str) -> str:
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user