41 lines
1.3 KiB
Python
41 lines
1.3 KiB
Python
"""环境配置管理"""
|
|
import os
|
|
from typing import Optional
|
|
|
|
|
|
class Settings:
|
|
"""应用配置"""
|
|
|
|
def __init__(self):
|
|
"""从环境变量加载配置"""
|
|
self.telegram_token: Optional[str] = os.getenv('TELEGRAM_TOKEN')
|
|
self.telegram_chat_id: Optional[str] = os.getenv('TELEGRAM_CHAT_ID')
|
|
self.max_threads: int = int(os.getenv('MAX_THREADS', '3'))
|
|
self.default_threads: int = int(os.getenv('DEFAULT_THREADS', '1'))
|
|
self.timeout: int = int(os.getenv('TIMEOUT', '15'))
|
|
self.output_file: str = os.getenv('OUTPUT_FILE', 'approvedcard.txt')
|
|
|
|
@classmethod
|
|
def from_env(cls, env_file: str = '.env') -> 'Settings':
|
|
"""从.env文件加载配置
|
|
|
|
Args:
|
|
env_file: .env文件路径
|
|
|
|
Returns:
|
|
Settings实例
|
|
"""
|
|
if os.path.exists(env_file):
|
|
with open(env_file, 'r', encoding='utf-8') as f:
|
|
for line in f:
|
|
line = line.strip()
|
|
if line and not line.startswith('#') and '=' in line:
|
|
key, value = line.split('=', 1)
|
|
os.environ[key.strip()] = value.strip()
|
|
|
|
return cls()
|
|
|
|
|
|
# 全局设置实例
|
|
settings = Settings()
|