-
Notifications
You must be signed in to change notification settings - Fork 0
/
jsondata.py
54 lines (46 loc) · 1.5 KB
/
jsondata.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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
"""-----------------------------------
Data manager for storage as json file
-----------------------------------"""
import os
import json
from pathlib import Path
# Default file path to store data
DIR_PATH = Path(__file__).parent / "store"
FILE_PATH = DIR_PATH / "data.json"
def set_data(data={}):
""" Save data in json file.
"""
# Create directory if not exists
if not os.path.isdir(DIR_PATH):
os.makedirs(DIR_PATH)
# Inserts data and creates a new json file with it
if not os.path.exists(FILE_PATH):
with open(FILE_PATH, 'x') as file:
data = json.dumps(data, indent=4)
file.write(data)
file.close()
else:
# Updates data with prior removal of existing json file
with open(FILE_PATH, 'r+') as file:
_data = json.load(file)
_data.update(data)
file.close()
os.remove(FILE_PATH)
with open(FILE_PATH, 'x') as file:
json.dump(_data, file, indent=4)
file.close()
return True
def get_data(key=None, default=None):
""" Get a value by key from the json file.
"""
if not os.path.exists(FILE_PATH):
return {}
with open(FILE_PATH, 'r+') as file:
content = file.read()
if len(content) <= 2:
return {}
data = json.loads(content)
if key == None:
return data
data = json.loads(content)
return data.get(key, default)