-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstate_utils.py
More file actions
42 lines (30 loc) · 827 Bytes
/
state_utils.py
File metadata and controls
42 lines (30 loc) · 827 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
30
31
32
33
34
35
36
37
38
39
40
41
42
import os
import json
STATE_PATH = os.path.join(os.path.dirname(__file__), 'state.json')
print(f"State path: {STATE_PATH}")
def _load():
if os.path.exists(STATE_PATH):
with open(STATE_PATH, 'r', encoding='utf-8') as f:
try:
return json.load(f)
except Exception:
return {}
return {}
def _save(data):
with open(STATE_PATH, 'w', encoding='utf-8') as f:
json.dump(data, f, ensure_ascii=False, indent=2)
def read_state():
return _load()
def write_state(updates: dict):
data = _load()
data.update(updates)
_save(data)
return data
def set_field(key, value):
data = _load()
data[key] = value
_save(data)
return data
def get_field(key, default=None):
data = _load()
return data.get(key, default)