Skip to content

Commit

Permalink
build: ⬆️ Upgrade settings to work with newest pydantic versions
Browse files Browse the repository at this point in the history
  • Loading branch information
egrelier committed Jul 12, 2024
1 parent fa8b5b5 commit 682b0cc
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 8 deletions.
5 changes: 3 additions & 2 deletions pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "thermal-events"
version = "1.1.0"
version = "1.1.1"
description = "Python library that handles the thermal events and the interaction with the thermal events database of fusion reactors"
authors = ["Leo Dubus <leo.dubus@cea.fr>", "Erwan Grelier <erwan.grelier@cea.fr>", "Victor Moncada <victor.moncada@cea.fr>"]
readme = "README.md"
Expand All @@ -12,7 +12,8 @@ sqlalchemy = "^1.4.44"
numpy = "^1.23.4"
opencv-python = "^4.6.0.66"
marshmallow-sqlalchemy = "^0.28.1"
pydantic = {extras = ["dotenv"], version = "^1.10.2"}
pydantic = "^2.8.2"
pydantic_settings = "^2.3.4"
pymysql = "^1.0.2"


Expand Down
14 changes: 8 additions & 6 deletions thermal_events/settings.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
from pathlib import Path
from typing import Any, Dict, Optional

from pydantic import BaseSettings, validator
from pydantic import field_validator
from pydantic_settings import BaseSettings


class Settings(BaseSettings):
Expand All @@ -17,7 +18,7 @@ class Settings(BaseSettings):

DATABASE_URI: Optional[str] = None

@validator("DATABASE_URI", pre=True, allow_reuse=True)
@field_validator("DATABASE_URI", mode="before")
def assemble_db_connection(cls, v: Optional[str], values: Dict[str, Any]) -> Any:
"""
Assemble the database connection URI.
Expand All @@ -39,19 +40,20 @@ def assemble_db_connection(cls, v: Optional[str], values: Dict[str, Any]) -> Any
if isinstance(v, str):
return v

if values.get("SQLITE", True):
return f"sqlite:///{values.get('SQLITE_DATABASE_FILE')}"
if values.data.get("SQLITE", True):
return f"sqlite:///{values.data.get('SQLITE_DATABASE_FILE')}"

return (
f"mysql+pymysql://{values.get('MYSQL_USER')}:{values.get('MYSQL_PASSWORD')}@{values.get('MYSQL_HOST')}"
f"/{values.get('MYSQL_DATABASE')}"
f"mysql+pymysql://{values.data.get('MYSQL_USER')}:{values.data.get('MYSQL_PASSWORD')}@{values.data.get('MYSQL_HOST')}"
f"/{values.data.get('MYSQL_DATABASE')}"
)

class Config:
"""Settings configuration."""

case_sensitive = True
env_file = str(Path.home() / ".env.default"), ".env"
extra = "ignore"


settings = Settings()

0 comments on commit 682b0cc

Please sign in to comment.