-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathuser_data.py
More file actions
35 lines (27 loc) · 752 Bytes
/
Copy pathuser_data.py
File metadata and controls
35 lines (27 loc) · 752 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
import json
import os
from typing import TypedDict
import platformdirs
class UserData(TypedDict):
data_version: int
username: str
uuid: str
token: str
dirs = platformdirs.PlatformDirs("muncher")
DATA_FILE = "data.json"
DATA_PATH = dirs.user_data_path / DATA_FILE
user_data: None | UserData = None
# load
try:
with open(DATA_PATH) as f:
as_json = json.load(f)
user_data = UserData(**as_json)
print("Loaded user data for: ", user_data["username"])
except FileNotFoundError:
print("user data not found")
def save(new_data: UserData):
global user_data
user_data = new_data
os.makedirs(dirs.user_data_path, exist_ok=True)
with open(DATA_PATH, "w") as f:
json.dump(new_data, f)