Fix: The behavior of saving the configuration will be atomic

This commit is contained in:
SarContDeli 2022-03-31 19:57:05 +08:00
parent 919d52f84e
commit 100c02ed62
5 changed files with 8 additions and 3 deletions

1
.gitignore vendored
View File

@ -6,6 +6,7 @@ config/*.ini
config/*.yaml
!config/deploy.*.yaml
config/*.json
config/tmp*
!config/template.json
*.pyw
dev_tools/debug_tools

View File

@ -20,4 +20,5 @@ wrapt==1.13.1
prettytable==2.2.1
pypresence==4.2.1
rich==11.0.0
zerorpc
zerorpc
atomicwrites

View File

@ -4,6 +4,7 @@ import random
import string
from datetime import datetime, timedelta, timezone
from filelock import FileLock
from atomicwrites import atomic_write
import yaml
@ -119,7 +120,7 @@ def write_file(file, data):
yaml.safe_dump(data, f, default_flow_style=False, encoding='utf-8', allow_unicode=True,
sort_keys=False)
elif ext == '.json':
with open(file, mode='w', encoding='utf-8') as f:
with atomic_write(file, overwrite=True, encoding='utf-8') as f:
s = json.dumps(data, indent=2, ensure_ascii=False, sort_keys=False, default=str)
f.write(s)
else:

View File

@ -22,4 +22,5 @@ prettytable==2.2.1
pypresence==4.2.1
alas-webapp==0.3.7
rich==11.0.0
zerorpc
zerorpc
atomicwrites

View File

@ -11,6 +11,7 @@ anyio==1.3.1 # via -r requirements-in.txt
apkutils2==1.0.0 # via adbutils
asgiref==3.4.1 # via uvicorn
async-generator==1.10 # via anyio
atomicwrites==1.4.0 # via -r requirements-in.txt
cached-property==1.5.2 # via uiautomator2
certifi==2021.5.30 # via requests
cffi==1.15.0 # via gevent