Skip to content

Instantly share code, notes, and snippets.

@TheMatt2
Created March 20, 2023 01:08
Show Gist options
  • Save TheMatt2/7edbbadb5e644a0cf5f0d08c42f585c3 to your computer and use it in GitHub Desktop.
Save TheMatt2/7edbbadb5e644a0cf5f0d08c42f585c3 to your computer and use it in GitHub Desktop.
# Because it turns out writing files is hard
# Process can crash halfway through, and a collision can overwrite the file
# mid operation.
# https://stackoverflow.com/questions/7645338/how-to-do-atomic-file-replacement
# http://www.weirdnet.nl/apple/rename.html
import os
import hjson
import filelock
def safe_json_read(filename, timeout = None):
"""
Turns out reading a json file safely, atomically,
and without corrupting the file is a bit tricky.
"""
with filelock.FileLock(f"{filename}.lck", timeout = timeout):
# Read the file
with open(filename) as f:
return hjson.load(f)
def safe_json_write(filename, data, *json_args, **json_kwargs):
"""
Turns out writing a json file safely, atomically,
and without corrupting the file is a bit tricky.
"""
with filelock.FileLock(f"{filename}.lck"):
# Write to a temp file
with open(f"{filename}.tmp", "w") as f:
hjson.dump(data, f, *json_args, **json_kwargs)
# Replace the original file
os.replace(f"{filename}.tmp", filename)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment