config from docker envs

This commit is contained in:
2026-02-24 21:45:13 +00:00
parent a0977c40de
commit 36a4583d63
3 changed files with 34 additions and 5 deletions

View File

@@ -8,6 +8,6 @@ __all__ = (
) )
BOT_NAME = 'CraftSleuthBot' BOT_NAME = 'DeletedPostsBot'
BASE_DIR = Path(__file__).parent.parent.parent BASE_DIR = Path(__file__).parent.parent.parent
MSG_AWAIT_THRESHOLD = 5 MSG_AWAIT_THRESHOLD = 5

View File

@@ -3,7 +3,7 @@ Configuration for the DeletedPosts bot.
This module exposes a single dictionary called ``config`` which holds all This module exposes a single dictionary called ``config`` which holds all
of the parameters required to connect to Reddit and control the behaviour of of the parameters required to connect to Reddit and control the behaviour of
the bot. When you first run the program the file will be created for you the bot. When you first run the program the file will be created for you
with empty values; you should edit it before starting the bot. You can also with empty values; you should edit it before starting the bot. You can also
reset the file to defaults by running the application with the reset the file to defaults by running the application with the
``reset_config`` command-line argument. ``reset_config`` command-line argument.
@@ -12,9 +12,10 @@ Example usage::
from config import config from config import config
print(config['client_id']) print(config['client_id'])
""" """
import os
config = { config = {
"client_id": "", "client_id": "",
"client_secret": "", "client_secret": "",
@@ -28,6 +29,26 @@ config = {
"sleep_minutes": 5, "sleep_minutes": 5,
} }
# allow container/WC users to override values via environment variables
# variables are expected to be UPPERCASE versions of the keys (or prefixed
# with DP_). Numeric values will be converted to ints automatically.
for key in list(config):
# check both bare and prefixed variants
env_names = [key.upper(), f"DP_{key.upper()}"]
for env in env_names:
val = os.getenv(env)
if val is not None:
# cast numeric entries back to int when appropriate
if isinstance(config[key], int):
try:
config[key] = int(val)
except ValueError:
pass # leave original if conversion fails
else:
config[key] = val
break
# same data as a text template. both ``main.py`` and ``utils.actions`` import # same data as a text template. both ``main.py`` and ``utils.actions`` import
# this so that the file can be created or reset without duplicating the # this so that the file can be created or reset without duplicating the
# literal configuration body. # literal configuration body.

View File

@@ -5,5 +5,13 @@ services:
restart: unless-stopped restart: unless-stopped
volumes: volumes:
- /docker/data/deletedposts:/app/config - /docker/data/deletedposts:/app/config
environment:
CLIENT_ID: "abc123"
CLIENT_SECRET: "secret"
USER_AGENT: "DeletedPostsBot/1.0"
USERNAME: "botuser"
PASSWORD: "botpass"
SUB_NAME: "my_subreddit"
MAX_DAYS: "90" # strings are converted to ints when the
MAX_POSTS: "500" # corresponding config value is an int
SLEEP_MINUTES: "10"