-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconfig.py
More file actions
84 lines (63 loc) · 2.18 KB
/
config.py
File metadata and controls
84 lines (63 loc) · 2.18 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
"""Application configuration."""
from functools import cached_property
from pathlib import Path
from pydantic import model_validator
from pydantic_settings import BaseSettings, SettingsConfigDict
from typing_extensions import Self
class DB(BaseSettings):
"""Database connection settings."""
model_config = SettingsConfigDict(
extra="ignore",
env_prefix="postgres_",
env_file=".env",
env_file_encoding="utf-8",
)
username: str | None = None
user: str | None = None # alias for username
database: str | None = None
db: str | None = None # alias for database
password: str
host: str
port: int
@cached_property
def uri(self) -> str:
"""Build PostgreSQL connection URI."""
user = self.username
password = self.password
host = self.host
port = self.port
db = self.database
return f"postgresql://{user}:{password}@{host}:{port}/{db}"
@model_validator(mode="after")
def check_username_is_set(self) -> Self:
"""Check that username is provided (user or username)."""
if self.username is None and self.user is None:
raise ValueError("postgres_user or postgres_username must be set.")
if self.username is None:
self.username = self.user
return self
@model_validator(mode="after")
def check_database_is_set(self) -> Self:
"""Check that database name is provided (db or database)."""
if self.database is None and self.db is None:
raise ValueError("postgres_database or postgres_db must be set.")
if self.database is None:
self.database = self.db
return self
class Log(BaseSettings):
"""Logging configuration."""
model_config = SettingsConfigDict(
extra="ignore",
env_file=".env",
env_file_encoding="utf-8",
env_prefix="log_",
)
level: str
config_file_name: str = "logger.ini"
logger_name: str = "database_tests"
class Settings(BaseSettings):
"""Main settings container."""
db: DB = DB()
log: Log = Log()
root_path: str = str(Path(__file__).resolve().parent)
settings: Settings = Settings()