-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathconfig_manager.py
53 lines (46 loc) · 1.97 KB
/
config_manager.py
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
import os
import json
import logging
from typing import Dict, Optional
from functools import lru_cache
logger = logging.getLogger(__name__)
class ConfigManager:
"""
Handles configuration management with file-based storage and caching
- Uses JSON file to persist channel IDs
- Implements LRU cache for better performance
"""
def __init__(self, config_file: str = 'config.json'):
self.config_file = config_file
self.config: Dict[str, Optional[int]] = self._load_config()
@lru_cache(maxsize=1)
def _load_config(self) -> Dict[str, Optional[int]]:
"""
Load config from JSON file with error handling and caching
Returns default config if file doesn't exist or is invalid
"""
if os.path.exists(self.config_file):
try:
with open(self.config_file, 'r') as f:
return json.load(f)
except (json.JSONDecodeError, Exception) as e:
logger.error(f"Error reading {self.config_file}: {e}")
return {'source_id': None, 'destination_id': None}
def save_config(self) -> None:
"""Save config and invalidate cache"""
with open(self.config_file, 'w') as f:
json.dump(self.config, f)
self._load_config.cache_clear()
def validate_config(self):
"""
Validate configuration with:
- Channel ID format checks
- Access verification
- Self-forwarding prevention
"""
if self.config['source_id'] == self.config['destination_id']:
raise ValueError("Source and destination channels cannot be the same")
if not (isinstance(self.config['source_id'], int) and self.config['source_id'] < 0):
raise ValueError("Invalid source channel ID format")
if not (isinstance(self.config['destination_id'], int) and self.config['destination_id'] < 0):
raise ValueError("Invalid destination channel ID format")