mirror of
https://github.com/ArchiveBox/ArchiveBox.git
synced 2025-05-14 15:14:31 -04:00
use atomic writes inside to_json helper func
This commit is contained in:
parent
717e390ef6
commit
22bea7a4f6
2 changed files with 4 additions and 3 deletions
|
@ -20,7 +20,6 @@ from .config import (
|
||||||
)
|
)
|
||||||
from .util import (
|
from .util import (
|
||||||
scheme,
|
scheme,
|
||||||
fuzzy_url,
|
|
||||||
ts_to_date,
|
ts_to_date,
|
||||||
urlencode,
|
urlencode,
|
||||||
htmlencode,
|
htmlencode,
|
||||||
|
|
|
@ -618,8 +618,10 @@ class ExtendedEncoder(JSONEncoder):
|
||||||
|
|
||||||
def to_json(obj: Any, file: IO=None, indent: Optional[int]=4, sort_keys: bool=True, cls=ExtendedEncoder) -> Optional[str]:
|
def to_json(obj: Any, file: IO=None, indent: Optional[int]=4, sort_keys: bool=True, cls=ExtendedEncoder) -> Optional[str]:
|
||||||
if file:
|
if file:
|
||||||
json.dump(obj, file, indent=indent, sort_keys=sort_keys, cls=ExtendedEncoder)
|
path = os.path.realpath(file.name)
|
||||||
return None
|
contents = json.dumps(obj, indent=indent, sort_keys=sort_keys, cls=ExtendedEncoder)
|
||||||
|
atomic_write(contents, path)
|
||||||
|
return contents
|
||||||
else:
|
else:
|
||||||
return json.dumps(obj, indent=indent, sort_keys=sort_keys, cls=ExtendedEncoder)
|
return json.dumps(obj, indent=indent, sort_keys=sort_keys, cls=ExtendedEncoder)
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue