import os import sys import logging from pathlib import Path def setup_secrets(): """Set up secrets for the ETF Portal application.""" logging.basicConfig(level=logging.INFO) logger = logging.getLogger(__name__) try: # Get the FMP API key from user fmp_api_key = input("Enter your FMP API key: ").strip() if not fmp_api_key: logger.error("āŒ FMP API key is required") return False # Create .streamlit directory if it doesn't exist streamlit_dir = Path(".streamlit") streamlit_dir.mkdir(exist_ok=True) # Create secrets.toml secrets_path = streamlit_dir / "secrets.toml" with open(secrets_path, "w") as f: f.write(f'# FMP API Configuration\n') f.write(f'FMP_API_KEY = "{fmp_api_key}"\n\n') f.write(f'# Cache Configuration\n') f.write(f'CACHE_DURATION_HOURS = 24\n') # Set proper permissions secrets_path.chmod(0o600) # Only owner can read/write logger.info(f"āœ… Secrets file created at {secrets_path}") logger.info("šŸ”’ File permissions set to 600 (owner read/write only)") # Create cache directories cache_dirs = [ Path("cache/FMP_cache"), Path("cache/yfinance_cache") ] for cache_dir in cache_dirs: cache_dir.mkdir(parents=True, exist_ok=True) cache_dir.chmod(0o755) # Owner can read/write/execute, others can read/execute logger.info(f"āœ… Cache directory created: {cache_dir}") return True except Exception as e: logger.error(f"āŒ Setup failed: {str(e)}") return False if __name__ == "__main__": print("šŸ”§ ETF Portal Secrets Setup") print("===========================") print("This script will help you set up the secrets for the ETF Portal application.") print("Make sure you have your FMP API key ready.") print() success = setup_secrets() if success: print("\nāœ… Setup completed successfully!") print("\nNext steps:") print("1. Run the test script to verify the configuration:") print(" python -m ETF_Portal.tests.test_api_config") print("\n2. If you're deploying to a server, make sure to:") print(" - Set the secrets in your Streamlit dashboard") print(" - Create the cache directories with proper permissions") else: print("\nāŒ Setup failed. Check the logs for details.") sys.exit(1)