college-bot-v2/src/conf.py
nexy7574 c47a38bfb9
All checks were successful
Build and Publish college-bot-v2 / build_and_publish (push) Successful in 14s
Make use of the API for truth social commands
2024-06-05 02:56:03 +01:00

68 lines
2.2 KiB
Python

import logging
import os
import subprocess
from pathlib import Path
import tomllib
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,
check=True,
).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()
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", {"order": []})
CONFIG.setdefault("screenshot", {})
CONFIG.setdefault("responder", {})
CONFIG.setdefault("network", {})
CONFIG.setdefault("quote_a", {"channel": None})
CONFIG.setdefault("redis", {"host": "redis", "port": 6379, "decode_responses": True})
CONFIG.setdefault("starboard", {})
CONFIG.setdefault(
"truth",
{
"api": "https://bots.nexy7574.co.uk/jimmy/v2/",
"username": os.getenv("WEB_USERNAME", os.urandom(32).hex()),
"password": os.getenv("WEB_PASSWORD", os.urandom(32).hex()),
}
)
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.")
if _t := os.getenv("JIMMY_TOKEN"):
log.info("Overriding config with token from $JIMMY_TOKEN.")
CONFIG["jimmy"]["token"] = _t