import os from dataclasses import dataclass class ConfigError(Exception): pass @dataclass class DatabaseSettings: host: str port: int username: str password: str dbname: str @staticmethod def from_env() -> "DatabaseSettings": host = os.environ.get("APP_DATABASE_HOST") port = os.environ.get("APP_DATABASE_PORT") username = os.environ.get("APP_DATABASE_USERNAME") password = os.environ.get("APP_DATABASE_PASSWORD") dbname = os.environ.get("APP_DATABASE_NAME") if not host: raise ConfigError("No `host` variable provided!") if not port: raise ConfigError("No `port` variable provided!") if not username: raise ConfigError("No `username` variable provided!") if not password: raise ConfigError("No `password` variable provided!") if not dbname: raise ConfigError("No `dbname` variable provided!") return DatabaseSettings(host, int(port), username, password, dbname)