- config.py: Added _resolve_bot_token() to read from config file - bot.py: Uses config.config.bot_token instead of env var directly - test_config.py: Added test for config file token reading
55 lines
1.5 KiB
Python
55 lines
1.5 KiB
Python
"""JIGAIDO Configuration Management."""
|
|
|
|
import json
|
|
import os
|
|
from pathlib import Path
|
|
from typing import Optional
|
|
|
|
DEFAULT_DATA_DIR = Path.home() / ".jigaido"
|
|
|
|
|
|
class Config:
|
|
"""JIGAIDO configuration with precedence: ENV > config file > defaults."""
|
|
|
|
def __init__(self):
|
|
self.data_dir: Path = self._resolve_data_dir()
|
|
self.bot_token: Optional[str] = self._resolve_bot_token()
|
|
|
|
def _resolve_bot_token(self) -> Optional[str]:
|
|
env_token = os.environ.get("JIGAIDO_BOT_TOKEN")
|
|
if env_token:
|
|
return env_token
|
|
|
|
config_file = Path("~/.jigaido/config.json").expanduser()
|
|
if config_file.exists():
|
|
with open(config_file) as f:
|
|
config_data = json.load(f)
|
|
if "JIGAIDO_BOT_TOKEN" in config_data:
|
|
return config_data["JIGAIDO_BOT_TOKEN"]
|
|
|
|
return None
|
|
|
|
def _resolve_data_dir(self) -> Path:
|
|
env_dir = os.environ.get("JIGAIDO_DATA_DIR")
|
|
if env_dir:
|
|
return Path(env_dir)
|
|
|
|
config_file = Path("~/.jigaido/config.json").expanduser()
|
|
if config_file.exists():
|
|
with open(config_file) as f:
|
|
config_data = json.load(f)
|
|
if "data_dir" in config_data:
|
|
return Path(config_data["data_dir"])
|
|
|
|
return DEFAULT_DATA_DIR
|
|
|
|
def ensure_data_dir(self) -> None:
|
|
"""Ensure the data directory exists."""
|
|
self.data_dir.mkdir(parents=True, exist_ok=True)
|
|
|
|
|
|
config = Config()
|
|
|
|
|
|
config = Config()
|