| Current Path : /home/otto/python/ |
| Current File : //home/otto/python/config.py |
#!/usr/bin/env python3
"""
Configuration file for Python applications
This file contains configuration settings for the Python mail service
and other applications, independent of the Symfony app.
"""
import os
from pathlib import Path
from typing import Dict, Any
# Load environment variables from .env files
try:
from dotenv import load_dotenv
# Load .env.local first (if exists), then .env
load_dotenv('.env.local')
load_dotenv('.env')
except ImportError:
# python-dotenv not installed, continue without it
pass
# Base directory for the Python app
BASE_DIR = Path(__file__).parent
# Mail Server Configuration
MAIL_CONFIG = {
# SMTP Server Settings
'smtp_host': os.getenv('MAIL_SMTP_HOST', 'smtp.gmail.com'),
'smtp_port': int(os.getenv('MAIL_SMTP_PORT', '587')),
'smtp_username': os.getenv('MAIL_SMTP_USERNAME', 'your-email@gmail.com'),
'smtp_password': os.getenv('MAIL_SMTP_PASSWORD', 'your-app-password'),
'use_tls': os.getenv('MAIL_SMTP_USE_TLS', 'true').lower() == 'true',
'use_ssl': os.getenv('MAIL_SMTP_USE_SSL', 'false').lower() == 'true',
'auth_required': os.getenv('MAIL_SMTP_AUTH_REQUIRED', 'true').lower() == 'true',
# Default sender settings
'from_email': os.getenv('MAIL_FROM_EMAIL', 'noreply@yourdomain.com'),
'from_name': os.getenv('MAIL_FROM_NAME', 'Python Mail Service'),
}
# Server Monitoring Configuration
SERVER_MONITOR_CONFIG = {
'alert_email': os.getenv('SERVER_MONITOR_ALERT_EMAIL', 'server-admin@igot.africa'),
'disk_threshold': float(os.getenv('SERVER_MONITOR_DISK_THRESHOLD', '85.0')),
'memory_threshold': float(os.getenv('SERVER_MONITOR_MEMORY_THRESHOLD', '10.0')),
'cpu_threshold': float(os.getenv('SERVER_MONITOR_CPU_THRESHOLD', '1.5')),
}
# Logging Configuration
LOGGING_CONFIG = {
'level': os.getenv('LOG_LEVEL', 'INFO'),
'file': os.getenv('LOG_FILE', 'python-app.log'),
'format': '%(asctime)s - %(name)s - %(levelname)s - %(message)s',
}
# Alternative mail configurations for different providers
MAIL_PROVIDERS = {
'gmail': {
'smtp_host': 'smtp.gmail.com',
'smtp_port': 587,
'use_tls': True,
'use_ssl': False,
'auth_required': True,
'note': 'Requires App Password for 2FA accounts'
},
'outlook': {
'smtp_host': 'smtp-mail.outlook.com',
'smtp_port': 587,
'use_tls': True,
'use_ssl': False,
'auth_required': True,
'note': 'Standard Outlook/Hotmail configuration'
},
'yahoo': {
'smtp_host': 'smtp.mail.yahoo.com',
'smtp_port': 587,
'use_tls': True,
'use_ssl': False,
'auth_required': True,
'note': 'Yahoo Mail configuration'
},
'custom': {
'smtp_host': 'mail.yourdomain.com',
'smtp_port': 25,
'use_tls': False,
'use_ssl': False,
'auth_required': False,
'note': 'Custom SMTP server configuration'
}
}
def get_mail_config(provider: str = None) -> Dict[str, Any]:
"""
Get mail configuration, optionally overriding with a specific provider.
Args:
provider: Provider name from MAIL_PROVIDERS, or None for current config
Returns:
Dict containing mail configuration
"""
if provider and provider in MAIL_PROVIDERS:
config = MAIL_CONFIG.copy()
config.update(MAIL_PROVIDERS[provider])
return config
return MAIL_CONFIG.copy()
def get_server_monitor_config() -> Dict[str, Any]:
"""Get server monitoring configuration."""
return SERVER_MONITOR_CONFIG.copy()
def get_logging_config() -> Dict[str, Any]:
"""Get logging configuration."""
return LOGGING_CONFIG.copy()
def create_env_template():
"""Create a .env template file with current configuration."""
env_content = """# Python Mail Service Configuration
# Copy this file to .env.local and modify the values as needed
# Mail Server Configuration
MAIL_SMTP_HOST={smtp_host}
MAIL_SMTP_PORT={smtp_port}
MAIL_SMTP_USERNAME={smtp_username}
MAIL_SMTP_PASSWORD={smtp_password}
MAIL_SMTP_USE_TLS={use_tls}
MAIL_SMTP_USE_SSL={use_ssl}
MAIL_SMTP_AUTH_REQUIRED={auth_required}
# Default sender email (optional)
MAIL_FROM_EMAIL={from_email}
MAIL_FROM_NAME={from_name}
# Server monitoring configuration
SERVER_MONITOR_ALERT_EMAIL={alert_email}
SERVER_MONITOR_DISK_THRESHOLD={disk_threshold}
SERVER_MONITOR_MEMORY_THRESHOLD={memory_threshold}
SERVER_MONITOR_CPU_THRESHOLD={cpu_threshold}
# Logging configuration (optional)
LOG_LEVEL={log_level}
LOG_FILE={log_file}
""".format(
smtp_host=MAIL_CONFIG['smtp_host'],
smtp_port=MAIL_CONFIG['smtp_port'],
smtp_username=MAIL_CONFIG['smtp_username'],
smtp_password=MAIL_CONFIG['smtp_password'],
use_tls=str(MAIL_CONFIG['use_tls']).lower(),
use_ssl=str(MAIL_CONFIG['use_ssl']).lower(),
auth_required=str(MAIL_CONFIG['auth_required']).lower(),
from_email=MAIL_CONFIG['from_email'],
from_name=MAIL_CONFIG['from_name'],
alert_email=SERVER_MONITOR_CONFIG['alert_email'],
disk_threshold=SERVER_MONITOR_CONFIG['disk_threshold'],
memory_threshold=SERVER_MONITOR_CONFIG['memory_threshold'],
cpu_threshold=SERVER_MONITOR_CONFIG['cpu_threshold'],
log_level=LOGGING_CONFIG['level'],
log_file=LOGGING_CONFIG['file']
)
env_file = BASE_DIR / '.env.template'
with open(env_file, 'w') as f:
f.write(env_content)
return env_file
if __name__ == "__main__":
# Create .env template when run directly
template_file = create_env_template()
print(f"Created .env template: {template_file}")
print("\nCurrent configuration:")
print(f"Mail: {get_mail_config()}")
print(f"Server Monitor: {get_server_monitor_config()}")
print(f"Logging: {get_logging_config()}")