Skip to content

Commit

Permalink
atomically write default config
Browse files Browse the repository at this point in the history
  • Loading branch information
jxu10 authored and manulera committed Oct 8, 2024
1 parent 2be88fb commit 17a1ed1
Showing 1 changed file with 10 additions and 2 deletions.
12 changes: 10 additions & 2 deletions src/pydna/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,7 @@
import logging.handlers as _handlers
import appdirs as _appdirs
import configparser as _configparser
import tempfile as _tempfile
from pydna._pretty import PrettyTable as _PrettyTable


Expand Down Expand Up @@ -183,8 +184,15 @@
_parser.read(_ini_path)
else: # otherwise it is created with default settings
_parser["main"] = default_ini
with open(_ini_path, "w", encoding="utf-8") as f: # TODO needs encoding?
_parser.write(f)
_temp_ini_file = _tempfile.NamedTemporaryFile(dir=_ini_path.parent, delete=False)
_temp_ini_path = _Path(_temp_ini_file.name)
try:
_temp_ini_file.close()
with _temp_ini_path.open("w", encoding="utf-8") as f: # TODO needs encoding?
_parser.write(f)
_temp_ini_path.replace(_ini_path)
finally:
_temp_ini_path.unlink(missing_ok=True)

# pydna related environmental variables are set
# from pydna.ini if they are not set already
Expand Down

0 comments on commit 17a1ed1

Please sign in to comment.