Files
mostovik-backend/src/config/settings/env.py
Aleksandr Meshchriakov beec622b75
Some checks failed
CI/CD Pipeline / Code Quality Checks (push) Failing after 10m27s
CI/CD Pipeline / Run Tests (push) Failing after 10m43s
CI/CD Pipeline / Build & Push Images (push) Successful in 7m28s
CI/CD Pipeline / Deploy (prod) (push) Has been skipped
CI/CD Pipeline / Deploy (dev) (push) Successful in 44s
fix: switch to dev settings for Docker services and improve env config
2026-02-10 19:14:36 +01:00

54 lines
1.4 KiB
Python

from __future__ import annotations
from collections.abc import Iterable
from pathlib import Path
from typing import Any
from decouple import AutoConfig, Config, RepositoryEnv
def build_config(base_dir: Path):
env_file = base_dir / ".env"
if env_file.exists():
return Config(RepositoryEnv(str(env_file)))
return AutoConfig(search_path=base_dir)
def env_bool(value: Any, default: bool = False) -> bool:
if value is None:
return default
if isinstance(value, bool):
return value
if isinstance(value, (int, float)):
return bool(value)
s = str(value).strip().lower()
if s in {"1", "true", "yes", "y", "on"}:
return True
if s in {"0", "false", "no", "n", "off"}:
return False
return default
def env_int(value: Any, default: int = 0) -> int:
try:
return int(str(value).strip())
except Exception:
return default
def env_list(value: Any, default: Iterable[str] = ()) -> list[str]:
"""
Поддерживает:
- "a,b,c"
- "a, b, c"
- пустые значения
"""
if value is None:
return list(default)
if isinstance(value, (list, tuple, set)):
return [str(x).strip() for x in value if str(x).strip()]
s = str(value).strip()
if not s:
return list(default)
return [p.strip() for p in s.split(",") if p.strip()]