Skip to content

config

dev_tool.db.config

log = logging.getLogger(__name__) module-attribute

DatabaseConfig dataclass

A data class for database configuration.

This class stores the configuration parameters for a database connection.

engine instance-attribute

host instance-attribute

port instance-attribute

name instance-attribute

user instance-attribute

password instance-attribute

from_env staticmethod

A method that creates a database configuration from environment variables.

Parameters:

  • prefix (str, default: '' ) –

    The prefix for environment variable names.

Returns:

  • Self

    A new DatabaseConfig instance.

Source code in dev_tool/db/config.py
@staticmethod
def from_env(prefix: str = '') -> Self:
    """
    A method that creates a database configuration from environment variables.

    :param prefix: The prefix for environment variable names.
    :return: A new DatabaseConfig instance.
    """

    return DatabaseConfig(
        engine=os.getenv(f'{prefix}DATABASE_ENGINE') or 'postgresql',
        host=os.getenv(f'{prefix}DATABASE_HOST') or 'localhost',
        port=os.getenv(f'{prefix}DATABASE_PORT') or '5432',
        name=os.getenv(f'{prefix}DATABASE_NAME') or 'postgres',
        user=os.getenv(f'{prefix}DATABASE_USER') or 'postgres',
        password=os.getenv(f'{prefix}DATABASE_PASSWORD') or 'postgres',
    )

__post_init__

A post-initialization method that validates the configuration.

Source code in dev_tool/db/config.py
def __post_init__(self) -> None:
    """A post-initialization method that validates the configuration."""

    for field in fields(self):
        value = getattr(self, field.name)

        if not value and value is not None:
            message = f'The "{field.name}" field must not be empty or None.'
            CONTEXT.notification.error_banner(message)

            log.exception(message)
            raise ValueError(message)