Merge pull request #3305 from mdboom/atomic-write-notebook-json

Write notebook.json file atomically
This commit is contained in:
Thomas Kluyver 2018-02-06 11:18:55 +00:00 committed by GitHub
commit e250b0b7bf
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -91,12 +91,16 @@ class BaseJSONConfigManager(LoggingConfigurable):
filename = self.file_name(section_name)
self.ensure_config_dir_exists()
# Generate the JSON up front, since it could raise an exception,
# in order to avoid writing half-finished corrupted data to disk.
json_content = json.dumps(data, indent=2)
if PY3:
f = io.open(filename, 'w', encoding='utf-8')
else:
f = open(filename, 'wb')
with f:
json.dump(data, f, indent=2)
f.write(json_content)
def update(self, section_name, new_data):
"""Modify the config section by recursively updating it with new_data.