Skip to content

Commit d2f1951

Browse files
authored
Merge pull request #887 from allthingslinux/config-autogen
fix(config.py): fix crashes if settings.yml is not up to date
2 parents b996ad1 + 8fdf5d7 commit d2f1951

File tree

1 file changed

+17
-11
lines changed

1 file changed

+17
-11
lines changed

tux/utils/config.py

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import base64
22
import os
33
from pathlib import Path
4-
from typing import Final
4+
from typing import Any, Final, cast
55

66
import yaml
77
from dotenv import load_dotenv
@@ -34,23 +34,29 @@ def convert_dict_str_to_int(original_dict: dict[str, int]) -> dict[int, int]:
3434
workspace_root = Path(__file__).parent.parent.parent
3535

3636
config_file = workspace_root / "config/settings.yml"
37+
config_file_example = workspace_root / "config/settings.yml.example"
3738
config = yaml.safe_load(config_file.read_text())
39+
config_example = yaml.safe_load(config_file_example.read_text())
40+
41+
42+
# Recursively merge defaults into user config (fills nested missing keys too)
43+
def merge_defaults(user: dict[str, Any], default: dict[str, Any]) -> None:
44+
for key, default_val in default.items():
45+
if key not in user:
46+
user[key] = default_val
47+
logger.warning(f"Added missing config key: {key}")
48+
elif isinstance(default_val, dict) and isinstance(user.get(key), dict):
49+
merge_defaults(user[key], cast(dict[str, Any], default_val))
50+
51+
52+
merge_defaults(config, config_example)
3853

3954

4055
class Config:
4156
# Permissions
4257
BOT_OWNER_ID: Final[int] = config["USER_IDS"]["BOT_OWNER"]
4358
SYSADMIN_IDS: Final[list[int]] = config["USER_IDS"]["SYSADMINS"]
44-
# ALLOW_SYSADMINS_EVAL: Final[bool] = config["ALLOW_SYSADMINS_EVAL"]
45-
# default to false if not specified in config
46-
_allow_sysadmins_eval = config.get("ALLOW_SYSADMINS_EVAL")
47-
if _allow_sysadmins_eval is None:
48-
logger.warning(
49-
"ALLOW_SYSADMINS_EVAL not found in config, defaulting to False. "
50-
"If you want to use the old behavior, please set it to True in your config.",
51-
)
52-
_allow_sysadmins_eval = False
53-
ALLOW_SYSADMINS_EVAL: Final[bool] = _allow_sysadmins_eval
59+
ALLOW_SYSADMINS_EVAL: Final[bool] = config["ALLOW_SYSADMINS_EVAL"]
5460

5561
# Production env
5662
DEFAULT_PROD_PREFIX: Final[str] = config["BOT_INFO"]["PROD_PREFIX"]

0 commit comments

Comments
 (0)