-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconfig.py
More file actions
67 lines (57 loc) · 2.42 KB
/
config.py
File metadata and controls
67 lines (57 loc) · 2.42 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
import os
import sys
import logging
from pathlib import Path
from typing import Optional, Dict, Any
ROOT = Path(__file__).resolve().parent
# Configure logging
logging.basicConfig(
level=logging.INFO,
format='%(asctime)s - %(name)s - %(levelname)s - %(message)s',
)
logger = logging.getLogger(__name__)
def load_environment() -> Dict[str, Any]:
"""Load and parse environment variables from .env file."""
try:
from dotenv import load_dotenv
load_dotenv(ROOT / '.env')
except ImportError:
logger.warning("python-dotenv not installed; skipping .env loading")
return {
'token': os.getenv('DISCORD_TOKEN'),
'my_id': os.getenv('MY_ID'),
'oauth_client_id': os.getenv('OAUTH_CLIENT_ID'),
'oauth_client_secret': os.getenv('OAUTH_CLIENT_SECRET'),
'json_folder': Path(os.getenv('JSON_FOLDER', './friends_data')),
'cache_dir': Path(os.getenv('CACHE_DIR', './icon_cache')),
'canvas_size': os.getenv('CANVAS_SIZE', '50,50'),
'dpi': int(os.getenv('DPI', '300')),
'avatar_pixels': int(os.getenv('AVATAR_PIXELS', '256')),
'avatar_zoom': float(os.getenv('AVATAR_ZOOM', '0.18')),
'peer_weight': float(os.getenv('PEER_WEIGHT', '1.5')),
'hero_weight': float(os.getenv('HERO_WEIGHT', '2.5')),
'heatmap_grid': int(os.getenv('HEATMAP_GRID', '100')),
'min_delay': float(os.getenv('MIN_DELAY', '1.5')),
'max_delay': float(os.getenv('MAX_DELAY', '4.0')),
'batch_size': int(os.getenv('BATCH_SIZE', '30')),
'batch_pause': float(os.getenv('BATCH_PAUSE', '60')),
'refresh_days': int(os.getenv('REFRESH_DAYS', '7')),
}
def validate_config(config: Dict[str, Any], required_keys: list) -> bool:
"""Validate that required keys exist and are non-empty."""
missing = [k for k in required_keys if not config.get(k)]
if missing:
logger.error(f"Missing required configuration: {', '.join(missing)}")
return False
return True
def ensure_env_file() -> bool:
"""Ensure .env file exists, with helpful error message if not."""
env_path = ROOT / '.env'
if env_path.exists():
return True
logger.error(f"No .env file found at {env_path}")
example_path = ROOT / '.env.example'
if example_path.exists():
logger.error(f"Please create .env from the example at {example_path}")
logger.error("Set DISCORD_TOKEN and MY_ID at minimum.")
return False