2024-01-04 15:43:40 +00:00
|
|
|
import logging
|
2024-04-29 01:41:02 +01:00
|
|
|
import os
|
2024-02-07 16:53:29 +00:00
|
|
|
import subprocess
|
2024-01-04 15:43:40 +00:00
|
|
|
from pathlib import Path
|
|
|
|
|
2024-05-01 01:23:41 +01:00
|
|
|
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"
|
|
|
|
|
2024-01-04 15:43:40 +00:00
|
|
|
try:
|
2024-05-01 01:23:41 +01:00
|
|
|
with open("config.toml", "rb") as file:
|
|
|
|
CONFIG = tomllib.load(file)
|
2024-01-04 15:43:40 +00:00
|
|
|
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})
|
|
|
|
|
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
|
|
|
|
|
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
|