Skip to content

env

dev_tool.config.env

log = logging.getLogger(__name__) module-attribute

EnvConfig

A class for managing environment configuration.

This class provides methods for loading, reloading, and creating default environment variable configurations.

reload

A method that reloads environment variables from a .env file.

This method loads environment variables from the specified file path, overriding any existing variables with the same names.

Parameters:

  • dotenv_path (Path | str | None, default: None ) –

    The path to the .env file to load.

  • prompt (bool, default: False ) –

    Whether to prompt the user for the env file path.

Source code in dev_tool/config/env.py
def reload(self, dotenv_path: Path | str | None = None, prompt: bool = False) -> None:
    """
    A method that reloads environment variables from a .env file.

    This method loads environment variables from the specified file path,
    overriding any existing variables with the same names.

    :param dotenv_path: The path to the .env file to load.
    :param prompt: Whether to prompt the user for the env file path.
    """

    if dotenv_path is None:
        dotenv_path = DEVELOPMENT_ENV

    if prompt:
        dotenv_path = (
            input('Which .env file do you want to use? (default: "development.env"): ') or
            'development.env'
        )

    if isinstance(dotenv_path, str):
        dotenv_path = Path(dotenv_path)

    if dotenv_path.exists():
        load_dotenv(dotenv_path=dotenv_path, override=True)
        self._setup_parallel_development()

create_default_env

A method that creates a default environment file.

This method copies the content from a template file to create a new environment configuration file if it doesn't already exist.

Parameters:

  • path (Path) –

    The path where the new environment file should be created.

  • template (Path) –

    The path to the template environment file.

Source code in dev_tool/config/env.py
def create_default_env(self, path: Path, template: Path) -> None:
    """
    A method that creates a default environment file.

    This method copies the content from a template file to create a new
    environment configuration file if it doesn't already exist.

    :param path: The path where the new environment file should be created.
    :param template: The path to the template environment file.
    """

    if path.exists():
        return

    if not template.exists():
        return

    with template.open('r', encoding='utf-8') as handle:
        content = handle.read()

    with path.open('w', encoding='utf-8') as handle:
        handle.write(content)

get_docker_authorization

A method that retrieves Docker authorization credentials.

This method gets the username and password for Docker from environment variables, falling back to default values if not set.

Returns:

  • tuple[str, str]

    A tuple containing (username, password).

Raises:

  • OSError

    If the username or password is empty.

Source code in dev_tool/config/env.py
def get_docker_authorization(self) -> tuple[str, str]:
    """
    A method that retrieves Docker authorization credentials.

    This method gets the username and password for Docker from environment variables,
    falling back to default values if not set.

    :return: A tuple containing (username, password).
    :raises OSError: If the username or password is empty.
    """

    username = os.getenv('DATABASE_USER', 'stratus')
    password = os.getenv('DATABASE_PASSWORD', 'stratus')

    if not username or not password:
        message = 'DATABASE_USER and DATABASE_PASSWORD must be set in the environment.'
        raise OSError(message)

    return username, password