forked from RealistikOsu/USSR
-
Notifications
You must be signed in to change notification settings - Fork 0
/
config.py
79 lines (65 loc) · 2.17 KB
/
config.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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
from dataclasses import dataclass
from dataclasses import field
from json import load
from json import dump
from typing import Any
import os
from logger import debug
from logger import info
@dataclass
class Config:
host: str = "127.0.0.1"
port: int = 2137
sql_host: str = "localhost"
sql_user: str = "root"
sql_db: str = "rosu"
sql_pass: str = "password"
redis_host: str = "localhost"
redis_db: str = "0"
redis_pass: str = ""
data_dir: str = ".data"
direct_url: str = "https://catboy.best/api"
api_keys_pool: list[str] = field(default_factory=list)
custom_clients: bool = False
srv_url: str = "https://ussr.pl"
srv_name: str = "RealistikOsu"
srv_verified_badge: int = 1005
discord_first_place: str = ""
discord_admin_hook: str = ""
pp_cap_vn: int = 700
pp_cap_rx: int = 1200
pp_cap_ap: int = 1200
ws_write_key: str = ""
bot_user_id: int = 1000
def read_config_json() -> dict[str, Any]:
with open("config.json", "r") as f:
return load(f)
def write_config(config: Config):
with open("config.json", "w") as f:
dump(config.__dict__, f, indent=4)
def load_config() -> Config:
"""Loads the config from the file, handling config updates.
Note:
Raises `SystemExit` on config update.
"""
config_dict = {}
if os.path.exists("config.json"):
config_dict = read_config_json()
# Compare config json attributes with config class attributes
missing_keys = [
key for key in Config.__annotations__ if key not in config_dict
]
# Remove extra fields
for key in tuple(config_dict): # Tuple cast is necessary to create a copy of the keys.
if key not in Config.__annotations__:
del config_dict[key]
# Create config regardless, populating it with missing keys and removing
# unnecessary keys.
config = Config(**config_dict)
if missing_keys:
info(f"Your config has been updated with {len(missing_keys)} new keys.")
debug("Missing keys: " + ", ".join(missing_keys))
write_config(config)
raise SystemExit(0)
return config
config = load_config()