config.py 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. import logging
  2. import os
  3. from pathlib import Path
  4. from pydantic_settings import BaseSettings
  5. # Application version - single source of truth
  6. APP_VERSION = "0.1.6"
  7. GITHUB_REPO = "maziggy/bambuddy"
  8. # App directory - where the application is installed (for static files)
  9. _app_dir = Path(__file__).resolve().parent.parent.parent.parent
  10. # Data directory - for persistent data (database, archives)
  11. # Use DATA_DIR env var if set (Docker), otherwise use project root (local dev)
  12. _data_dir_env = os.environ.get("DATA_DIR")
  13. _data_dir = Path(_data_dir_env) if _data_dir_env else _app_dir
  14. # Plate calibration directory - special handling to maintain backwards compatibility
  15. # Docker: DATA_DIR/plate_calibration (e.g., /data/plate_calibration)
  16. # Local dev: project_root/data/plate_calibration (original location)
  17. _plate_cal_dir = Path(_data_dir_env) / "plate_calibration" if _data_dir_env else _app_dir / "data" / "plate_calibration"
  18. # Log directory - use LOG_DIR env var if set, otherwise use app_dir/logs
  19. _log_dir_env = os.environ.get("LOG_DIR")
  20. _log_dir = Path(_log_dir_env) if _log_dir_env else _app_dir / "logs"
  21. def _migrate_database() -> Path:
  22. """Migrate database from old name to new name if needed."""
  23. old_db = _data_dir / "bambutrack.db"
  24. new_db = _data_dir / "bambuddy.db"
  25. # If old database exists and new one doesn't, rename it
  26. if old_db.exists() and not new_db.exists():
  27. try:
  28. old_db.rename(new_db)
  29. logging.info(f"Migrated database: {old_db} -> {new_db}")
  30. except Exception as e:
  31. logging.warning(f"Could not migrate database: {e}. Using old location.")
  32. return old_db
  33. # If old database exists (and new one now exists too), it was migrated
  34. # If only new exists, use new
  35. # If neither exists, use new (will be created)
  36. return new_db if new_db.exists() or not old_db.exists() else old_db
  37. # Determine database path (handles migration)
  38. _db_path = _migrate_database()
  39. class Settings(BaseSettings):
  40. app_name: str = "Bambuddy"
  41. debug: bool = False # Default to production mode
  42. # Paths
  43. base_dir: Path = _data_dir # For backwards compatibility
  44. archive_dir: Path = _data_dir / "archive"
  45. plate_calibration_dir: Path = _plate_cal_dir # Plate detection references
  46. static_dir: Path = _app_dir / "static" # Static files are part of app, not data
  47. log_dir: Path = _log_dir
  48. database_url: str = f"sqlite+aiosqlite:///{_db_path}"
  49. # Logging
  50. log_level: str = "INFO" # Override with LOG_LEVEL env var or DEBUG=true
  51. log_to_file: bool = True # Set to false to disable file logging
  52. # API
  53. api_prefix: str = "/api/v1"
  54. class Config:
  55. env_file = ".env"
  56. env_file_encoding = "utf-8"
  57. settings = Settings()
  58. # Ensure directories exist
  59. settings.archive_dir.mkdir(exist_ok=True)
  60. settings.plate_calibration_dir.mkdir(exist_ok=True)
  61. settings.static_dir.mkdir(exist_ok=True)
  62. if settings.log_to_file:
  63. settings.log_dir.mkdir(exist_ok=True)