config.py 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. from pathlib import Path
  2. from pydantic_settings import BaseSettings
  3. import logging
  4. # Application version - single source of truth
  5. APP_VERSION = "0.1.5b"
  6. GITHUB_REPO = "maziggy/bambuddy"
  7. # Base directory for path calculations
  8. _base_dir = Path(__file__).resolve().parent.parent.parent.parent
  9. def _migrate_database() -> Path:
  10. """Migrate database from old name to new name if needed."""
  11. old_db = _base_dir / "bambutrack.db"
  12. new_db = _base_dir / "bambuddy.db"
  13. # If old database exists and new one doesn't, rename it
  14. if old_db.exists() and not new_db.exists():
  15. try:
  16. old_db.rename(new_db)
  17. logging.info(f"Migrated database: {old_db} -> {new_db}")
  18. except Exception as e:
  19. logging.warning(f"Could not migrate database: {e}. Using old location.")
  20. return old_db
  21. # If old database exists (and new one now exists too), it was migrated
  22. # If only new exists, use new
  23. # If neither exists, use new (will be created)
  24. return new_db if new_db.exists() or not old_db.exists() else old_db
  25. # Determine database path (handles migration)
  26. _db_path = _migrate_database()
  27. class Settings(BaseSettings):
  28. app_name: str = "Bambuddy"
  29. debug: bool = False # Default to production mode
  30. # Paths
  31. base_dir: Path = _base_dir
  32. archive_dir: Path = base_dir / "archive"
  33. static_dir: Path = base_dir / "static"
  34. log_dir: Path = base_dir / "logs"
  35. database_url: str = f"sqlite+aiosqlite:///{_db_path}"
  36. # Logging
  37. log_level: str = "INFO" # Override with LOG_LEVEL env var or DEBUG=true
  38. log_to_file: bool = True # Set to false to disable file logging
  39. # API
  40. api_prefix: str = "/api/v1"
  41. class Config:
  42. env_file = ".env"
  43. env_file_encoding = "utf-8"
  44. settings = Settings()
  45. # Ensure directories exist
  46. settings.archive_dir.mkdir(exist_ok=True)
  47. settings.static_dir.mkdir(exist_ok=True)
  48. if settings.log_to_file:
  49. settings.log_dir.mkdir(exist_ok=True)