Files
monitor-shared-utils/src/monitor_utils/config.py
T

63 lines
1.9 KiB
Python

"""Config loading and mode helpers shared across monitors and the bot."""
import json
import logging
import pathlib
from enum import Enum
from monitor_utils.constants import DEVELOPMENT_BOT_TOKEN, LOGGERS, PRODUCTION_BOT_TOKEN
class DevelopmentMode(str, Enum):
"""Runtime mode selecting production or development behavior."""
PRODUCTION = "PRODUCTION"
DEVELOPMENT = "DEVELOPMENT"
def is_production(development_mode: DevelopmentMode) -> bool:
"""Return True when the mode is PRODUCTION."""
return development_mode == DevelopmentMode.PRODUCTION
def get_bot_token(is_production: bool) -> str:
"""Return the production or development bot token for the mode."""
if is_production:
return PRODUCTION_BOT_TOKEN
return DEVELOPMENT_BOT_TOKEN
def get_config(
monitor_name: str,
config_dir: str | pathlib.Path = "configs",
test_config_dir: str | pathlib.Path = "tests/configs",
use_production_path: bool = False,
) -> tuple[dict, pathlib.Path]:
"""Load a monitor's JSON config from the production or test directory.
Args:
monitor_name: Monitor name with or without a .json suffix.
config_dir: Directory holding production configs.
test_config_dir: Directory holding test configs.
use_production_path: Load from config_dir instead of test_config_dir.
Returns:
Tuple of the parsed config dict and the resolved config path.
"""
monitor_name = monitor_name.replace(".json", "")
base = config_dir if use_production_path else test_config_dir
config_path = pathlib.Path(base) / f"{monitor_name}.json"
with open(config_path) as f:
return json.load(f), config_path
def set_logger_levels(loggers: list[str] = LOGGERS, level: int = logging.WARNING):
"""Set the given loggers to the given level to quiet noisy libraries."""
for name in loggers:
logging.getLogger(name).setLevel(level)