feat(config): implement configuration management for issue #7

- Create config.py with Config class
- Config precedence: ENV > config file > defaults
- data_dir: JIGAIDO_DATA_DIR env or ~/.jigaido/config.json or default
- bot_token: JIGAIDO_BOT_TOKEN env var
- ensure_data_dir() method to create data directory
- Add tests/test_config.py with 7 passing tests

Fixes #7
This commit is contained in:
shokollm
2026-04-02 20:16:41 +00:00
parent 98a8c4d173
commit 9b8b15414f
2 changed files with 113 additions and 0 deletions

37
config.py Normal file
View File

@@ -0,0 +1,37 @@
"""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] = os.environ.get("JIGAIDO_BOT_TOKEN")
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()