You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

initialization.py 3.5 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. # api/initialization.py
  2. import os
  3. from pathlib import Path
  4. from typing import Dict
  5. from pydantic import BaseModel
  6. from loguru import logger
  7. from dotenv import load_dotenv
  8. from .config import Settings
  9. class _AppPaths(BaseModel):
  10. """Internal model representing all application paths"""
  11. app_root: Path
  12. static_root: Path
  13. user_files: Path
  14. ui_root: Path
  15. config_dir: Path
  16. database_uri: str
  17. class AppInitializer:
  18. """Handles application initialization including paths and environment setup"""
  19. def __init__(self, settings: Settings, app_path: str):
  20. """
  21. Initialize the application structure.
  22. Args:
  23. settings: Application settings
  24. app_path: Path to the application code directory
  25. """
  26. self.settings = settings
  27. self._app_path = Path(app_path)
  28. self._paths = self._init_paths()
  29. self._create_directories()
  30. self._load_environment()
  31. logger.info(f"Initialized application data folder: {self.app_root}")
  32. def _get_app_root(self) -> Path:
  33. """Determine application root directory"""
  34. if app_dir := os.getenv("AUTOGENSTUDIO_APPDIR"):
  35. return Path(app_dir)
  36. return Path.home() / ".autogenstudio"
  37. def _get_database_uri(self, app_root: Path) -> str:
  38. """Generate database URI based on settings or environment"""
  39. if db_uri := os.getenv("AUTOGENSTUDIO_DATABASE_URI"):
  40. return db_uri
  41. return self.settings.DATABASE_URI.replace(
  42. "./", str(app_root) + "/"
  43. )
  44. def _init_paths(self) -> _AppPaths:
  45. """Initialize and return AppPaths instance"""
  46. app_root = self._get_app_root()
  47. return _AppPaths(
  48. app_root=app_root,
  49. static_root=app_root / "files",
  50. user_files=app_root / "files" / "user",
  51. ui_root=self._app_path / "ui",
  52. config_dir=app_root / self.settings.CONFIG_DIR,
  53. database_uri=self._get_database_uri(app_root)
  54. )
  55. def _create_directories(self) -> None:
  56. """Create all required directories"""
  57. self.app_root.mkdir(parents=True, exist_ok=True)
  58. dirs = [self.static_root, self.user_files,
  59. self.ui_root, self.config_dir]
  60. for path in dirs:
  61. path.mkdir(parents=True, exist_ok=True)
  62. def _load_environment(self) -> None:
  63. """Load environment variables from .env file if it exists"""
  64. env_file = self.app_root / ".env"
  65. if env_file.exists():
  66. logger.info(f"Loading environment variables from {env_file}")
  67. load_dotenv(str(env_file))
  68. # Properties for accessing paths
  69. @property
  70. def app_root(self) -> Path:
  71. """Root directory for the application"""
  72. return self._paths.app_root
  73. @property
  74. def static_root(self) -> Path:
  75. """Directory for static files"""
  76. return self._paths.static_root
  77. @property
  78. def user_files(self) -> Path:
  79. """Directory for user files"""
  80. return self._paths.user_files
  81. @property
  82. def ui_root(self) -> Path:
  83. """Directory for UI files"""
  84. return self._paths.ui_root
  85. @property
  86. def config_dir(self) -> Path:
  87. """Directory for configuration files"""
  88. return self._paths.config_dir
  89. @property
  90. def database_uri(self) -> str:
  91. """Database connection URI"""
  92. return self._paths.database_uri