college-bot-v2/src/conf.py

64 lines
2.1 KiB
Python
Raw Normal View History

import logging
2024-04-29 01:41:02 +01:00
import os
2024-02-07 16:53:29 +00:00
import subprocess
from pathlib import Path
import tomllib
2024-04-18 00:24:58 +01:00
2024-02-07 16:53:29 +00:00
log = logging.getLogger("jimmy.autoconf")
if (Path.cwd() / ".git").exists():
try:
log.debug("Attempting to auto-detect running version using git.")
VERSION = subprocess.run(
["git", "rev-parse", "--short", "HEAD"],
stdout=subprocess.PIPE,
stderr=subprocess.DEVNULL,
text=True,
2024-04-16 00:46:26 +01:00
check=True,
2024-02-07 16:53:29 +00:00
).stdout.strip()
except subprocess.CalledProcessError:
log.debug("Unable to auto-detect running version using git.", exc_info=True)
VERSION = "unknown"
else:
log.debug("Unable to auto-detect running version using git, no .git directory exists.")
VERSION = "unknown"
try:
with open("config.toml", "rb") as file:
CONFIG = tomllib.load(file)
except FileNotFoundError:
cwd = Path.cwd()
2024-04-29 01:41:02 +01:00
log.critical(
"Unable to locate config.toml in %s. Using default configuration. Good luck!",
cwd,
exc_info=True
)
CONFIG = {}
CONFIG.setdefault("logging", {})
CONFIG.setdefault("jimmy", {})
CONFIG.setdefault("ollama", {})
CONFIG.setdefault("screenshot", {})
CONFIG.setdefault("responder", {})
2024-05-01 00:46:03 +01:00
CONFIG.setdefault("network", {})
2024-04-29 01:41:02 +01:00
CONFIG.setdefault("quote_a", {"channel": None})
CONFIG.setdefault("redis", {"host": "redis", "port": 6379, "decode_responses": True})
CONFIG.setdefault("starboard", {})
2024-04-29 01:41:02 +01:00
2024-05-05 02:27:41 +01:00
if CONFIG["redis"].pop("db", None) is not None:
log.warning("`redis.db` cannot be manually specified, each cog that uses redis has its own db value! Value ignored")
if CONFIG["redis"].pop("no_ping", None) is not None:
log.warning("`redis.no_ping` was deprecated after 808D621F. Ping is now always mandatory.")
CONFIG["redis"]["decode_responses"] = True
for logger_name in CONFIG["logging"].get("verbose", []):
_l = logging.getLogger(logger_name)
_l.setLevel(logging.DEBUG)
_l.debug("Logging level for this logger was forced to DEBUG by config.")
2024-04-29 01:41:02 +01:00
if _t := os.getenv("JIMMY_TOKEN"):
log.info("Overriding config with token from $JIMMY_TOKEN.")
CONFIG["jimmy"]["token"] = _t