todoapp-us6a2a/app/core/config.py
Automated Action 7cbef584c0 Fix pydantic_settings import error
Added fallback implementation for environments without pydantic_settings package
and added pydantic-settings to requirements.txt to prevent future issues.
2025-05-31 21:22:25 +00:00

49 lines
1.4 KiB
Python

import os
from pathlib import Path
from pydantic import BaseModel
# Try to use pydantic_settings if available, otherwise fallback to a simple BaseModel
try:
from pydantic_settings import BaseSettings
except ImportError:
# Fallback for environments without pydantic_settings
# In Pydantic v2, BaseSettings was moved to pydantic_settings package
class BaseSettings(BaseModel):
"""
Simplified BaseSettings implementation for compatibility.
"""
class Config:
env_file = ".env"
case_sensitive = True
def __init__(self, **kwargs):
# Load environment variables
env_values = {}
for field_name, _ in self.__annotations__.items():
env_val = os.environ.get(field_name)
if env_val is not None:
env_values[field_name] = env_val
# Override with any provided values
env_values.update(kwargs)
super().__init__(**env_values)
class Settings(BaseSettings):
PROJECT_NAME: str = "Todo API"
API_V1_STR: str = "/api/v1"
# Database settings
DB_DIR: Path = Path("/app") / "storage" / "db"
DB_NAME: str = "db.sqlite"
class Config:
env_file = ".env"
case_sensitive = True
settings = Settings()
# Ensure DB directory exists
settings.DB_DIR.mkdir(parents=True, exist_ok=True)