|
|
@ -233,18 +233,24 @@ else:
|
|
|
|
def write_json_file(obj, fn):
|
|
|
|
def write_json_file(obj, fn):
|
|
|
|
""" Encode obj as JSON and write it to fn, atomically """
|
|
|
|
""" Encode obj as JSON and write it to fn, atomically """
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
args = {
|
|
|
|
|
|
|
|
'suffix': '.tmp',
|
|
|
|
|
|
|
|
'prefix': os.path.basename(fn) + '.',
|
|
|
|
|
|
|
|
'dir': os.path.dirname(fn),
|
|
|
|
|
|
|
|
'delete': False,
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
# In Python 2.x, json.dump expects a bytestream.
|
|
|
|
# In Python 2.x, json.dump expects a bytestream.
|
|
|
|
# In Python 3.x, it writes to a character stream
|
|
|
|
# In Python 3.x, it writes to a character stream
|
|
|
|
if sys.version_info < (3, 0):
|
|
|
|
if sys.version_info < (3, 0):
|
|
|
|
mode = 'wb'
|
|
|
|
args['mode'] = 'wb'
|
|
|
|
encoding = None
|
|
|
|
|
|
|
|
else:
|
|
|
|
else:
|
|
|
|
mode = 'w'
|
|
|
|
args.update({
|
|
|
|
encoding = 'utf-8'
|
|
|
|
'mode': 'w',
|
|
|
|
tf = tempfile.NamedTemporaryFile(
|
|
|
|
'encoding': 'utf-8',
|
|
|
|
suffix='.tmp', prefix=os.path.basename(fn) + '.',
|
|
|
|
})
|
|
|
|
dir=os.path.dirname(fn),
|
|
|
|
|
|
|
|
delete=False)
|
|
|
|
tf = tempfile.NamedTemporaryFile(**args)
|
|
|
|
|
|
|
|
|
|
|
|
try:
|
|
|
|
try:
|
|
|
|
with tf:
|
|
|
|
with tf:
|
|
|
|