Skip to content

Commit

Permalink
Write notebook.json file atomically
Browse files Browse the repository at this point in the history
  • Loading branch information
mdboom committed Feb 5, 2018
1 parent 08a17df commit dd5778b
Showing 1 changed file with 5 additions and 1 deletion.
6 changes: 5 additions & 1 deletion notebook/config_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,12 +91,16 @@ def set(self, section_name, data):
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.
Expand Down

0 comments on commit dd5778b

Please sign in to comment.