This repository was archived by the owner on Jun 10, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathconfig.py
69 lines (49 loc) · 1.65 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
from __future__ import annotations
import os
from dataclasses import dataclass
from json import dump
from json import load
from typing import Any
import logger
@dataclass
class Config:
http_sock: str = "/tmp/gdpys.sock"
http_max_conn: int = 5
sql_host: str = "localhost"
sql_user: str = "root"
sql_db: str = "GDPyS"
sql_password: str = ""
dir_levels: str = ".data/levels"
command_prefix: str = "/"
def read_config_json() -> dict[str, Any]:
with open("config.json") 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:
logger.info(f"Your config has been updated with {len(missing_keys)} new keys.")
logger.debug("Missing keys: " + ", ".join(missing_keys))
write_config(config)
raise SystemExit(0)
return config
conf = load_config()