-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathdatabase.py
More file actions
29 lines (22 loc) · 765 Bytes
/
Copy pathdatabase.py
File metadata and controls
29 lines (22 loc) · 765 Bytes
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
28
29
import json
import traceback
import logging
from pathlib import Path
logger = logging.getLogger(__name__)
def read_database(database_path):
path = Path(database_path)
if not path.exists() or path.stat().st_size == 0:
return {}
try:
with path.open() as database_file:
return json.load(database_file)
except (json.JSONDecodeError, ValueError) as exc:
logger.error("%s:%s", type(exc).__name__, exc)
traceback.print_exc()
return {}
def write_database(database_path, data):
path = Path(database_path)
temporary_path = path.with_name(f"{path.name}.tmp")
with temporary_path.open("w") as database_file:
json.dump(data, database_file, indent=4)
temporary_path.replace(path)