-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathav_dbm.py
37 lines (31 loc) · 949 Bytes
/
av_dbm.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
import json
import os
import av_main
this_dir = av_main.determine_dir()
db_path = os.path.join(this_dir, "data", "database.json")
# Check if JSON file exists, create it if it doesn't
if not os.path.exists(os.path.dirname(db_path)):
os.makedirs(os.path.dirname(db_path))
if not os.path.exists(db_path):
with open(db_path, 'w') as f:
json.dump({}, f)
# Load existing JSON data
def dbread(string):
with open(db_path, 'r') as f:
data = json.load(f)
# print("one:", data.get(string))
return data.get(string)
# Store and write JSON data
def dbwrite(key, value):
with open(db_path, 'r+') as f:
data = json.load(f)
if key in data:
data[key] = value
update = True
else:
data.update({key: value})
update = False
f.seek(0)
f.truncate()
json.dump(data, f)
return update