Spaces:
Sleeping
Sleeping
Create utils/json_store.py
Browse files- utils/json_store.py +33 -0
utils/json_store.py
ADDED
|
@@ -0,0 +1,33 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import json
|
| 2 |
+
import os
|
| 3 |
+
|
| 4 |
+
SESSION_FILE = "data/session_data.json"
|
| 5 |
+
|
| 6 |
+
# Ensure the file exists
|
| 7 |
+
def init_session_data():
|
| 8 |
+
if not os.path.exists("data"):
|
| 9 |
+
os.makedirs("data")
|
| 10 |
+
if not os.path.exists(SESSION_FILE):
|
| 11 |
+
with open(SESSION_FILE, "w") as f:
|
| 12 |
+
json.dump({}, f)
|
| 13 |
+
|
| 14 |
+
def read_session_data():
|
| 15 |
+
init_session_data()
|
| 16 |
+
with open(SESSION_FILE, "r") as f:
|
| 17 |
+
return json.load(f)
|
| 18 |
+
|
| 19 |
+
def write_session_data(data):
|
| 20 |
+
with open(SESSION_FILE, "w") as f:
|
| 21 |
+
json.dump(data, f, indent=4)
|
| 22 |
+
|
| 23 |
+
def update_session_data(key, value):
|
| 24 |
+
data = read_session_data()
|
| 25 |
+
data[key] = value
|
| 26 |
+
write_session_data(data)
|
| 27 |
+
|
| 28 |
+
def get_session_value(key, default=None):
|
| 29 |
+
data = read_session_data()
|
| 30 |
+
return data.get(key, default)
|
| 31 |
+
|
| 32 |
+
def clear_session_data():
|
| 33 |
+
write_session_data({})
|