Skip to content

Allowing multiple locations for repository #185

Description

@vdboor

I'd like the decouple logic to use multiple locations for configuration. For example: .env files for development, but /run/secrets for production.

What is the best way to organize this?

Currently I've done the following:

from pathlib import Path
from decouple import Config, RepositoryEnv, RepositorySecret


class MultiRepository:
    """Allow searching for secrets in multiple sources.
    This allows extending python-decouple with multiple config locations.
    """

    def __init__(self, *repos):
        self.repos = repos

    def __getitem__(self, key):
        for repo in self.repos:
            try:
                return repo[key]
            except KeyError:
                pass
        raise KeyError(key)

    def __contains__(self, key):
        return any(key in repo for repo in self.repos)



SRC_DIR = Path(__file__).parent.parent.parent
TRY_CONFIG = {
    SRC_DIR / ".env": RepositoryEnv,
    "/run/secrets/": RepositorySecret,
}

repos = [cls(file) for file, cls in TRY_CONFIG.items() if os.path.exists(file)]

#: The settings object to read environment variables.
settings = Config(repository=MultiRepository(*repos))

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions