forked from scp-cs/translatordb_web
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.py
27 lines (23 loc) · 770 Bytes
/
utils.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
from os.path import exists
from json import load, dump, JSONDecodeError
from secrets import token_hex
import logging
DEFAULT_CONFIG = {
"SECRET_KEY": token_hex(24),
"DEBUG": False
}
def ensure_config(filename: str) -> None:
if exists(filename):
try:
with open(filename) as file:
_ = load(file)
logging.info('Config file loaded')
return
except JSONDecodeError:
logging.warning('Config file unreadable, creating new')
with open(filename, "w") as file:
dump(DEFAULT_CONFIG, file)
else:
logging.warning('Config file not found, creating new')
with open(filename, "w") as file:
dump(DEFAULT_CONFIG, file)