-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconfig.py
More file actions
49 lines (37 loc) · 1.38 KB
/
config.py
File metadata and controls
49 lines (37 loc) · 1.38 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
from pathlib import Path
from pydantic import BaseModel, Field
import yaml
class DevDBConfig(BaseModel):
container_host: str
container_name: str = Field(min_length=1)
container_port: int = Field(ge=1024, le=65535)
postgres_db: str | None = Field(min_length=1, default=None)
postgres_user: str = Field(min_length=1)
postgres_password: str = Field(min_length=1)
class Config(BaseModel):
dev_db: DevDBConfig
@property
def conn_str(self) -> str:
""" Connection string for Psycopg. """
return f"postgresql://" + self._conn_str_params()
@property
def sqlalchemy_url(self) -> str:
""" Connection string for SQLAlchemy. """
return f"postgresql+psycopg://" + self._conn_str_params()
def _conn_str_params(self) -> str:
""" Connection string without a URI scheme. """
return \
f"{self.dev_db.postgres_user}:{self.dev_db.postgres_password}" + \
f"@{self.dev_db.container_host}:{self.dev_db.container_port}" + \
f"/{self.dev_db.postgres_db}"
class TestDBConfig(Config):
"""
Subclass of `Config`, which contains parameters of a test database.
Added mainly for a better typing semantics.
"""
pass
def load_config() -> Config:
path = Path(__file__).parent / "config.yml"
with open(path) as f:
data = yaml.safe_load(f)
return Config(**data)