Skip to content

decorative_secrets.environment

ApplyEnvironmentArgumentsOptions dataclass

This class contains options governing the behavior of the apply_environment_arguments decorator.

Attributes:

  • env (collections.abc.Mapping[str, str]) –

    If provided, this dictionary of environment variables will be used in lieu of os.environ when retrieving environment variable values.

Source code in src/decorative_secrets/environment.py
23
24
25
26
27
28
29
30
31
32
33
34
35
36
@dataclass(frozen=True)
class ApplyEnvironmentArgumentsOptions:
    """
    This class contains options governing the behavior of the
    [apply_environment_arguments
    ](./#decorative_secrets.environment.apply_environment_arguments) decorator.

    Attributes:
        env: If provided, this dictionary of environment variables will be
            used in lieu of `os.environ` when retrieving environment variable
            values.
    """

    env: Mapping[str, str] = field(default_factory=lambda: os.environ)

apply_environment_arguments

apply_environment_arguments(
    *args: decorative_secrets.environment.ApplyEnvironmentArgumentsOptions,
    **kwargs: str
) -> collections.abc.Callable

This decorator maps parameter names to environment variables. Each key represents the name of a parameter in the decorated function which accepts an explicit input, and the corresponding mapped value is a parameter name accepting an environment variable from which to obtain the value when no value is explicitly provided.

Parameters:

Example
from functools import (
    cache,
)
from decorative_secrets.environment import (
    apply_environment_arguments,
)
from my_client_sdk import (
    Client,
)


@cache
@apply_onepassword_arguments(
    client_id="client_id_environment_variable",
    client_secret="client_secret_environment_variable",
)
def get_client(
    client_id: str | None = None,
    client_secret: str = None,
    client_id_environment_variable: str | None = None,
    client_secret_environment_variable: str | None = None,
) -> Client:
    return Client(
        oauth2_client_id=client_id,
        oauth2_client_secret=client_secret,
    )


client: Client = get_client(
    client_id_environment_variable=("CLIENT_ID",),
    client_secret_environment_variable=("CLIENT_SECRET",),
)
Source code in src/decorative_secrets/environment.py
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
def apply_environment_arguments(
    *args: ApplyEnvironmentArgumentsOptions,
    **kwargs: str,
) -> Callable:
    """
    This decorator maps parameter names to environment variables.
    Each key represents the name of a parameter in the decorated function
    which accepts an explicit input, and the corresponding mapped value is a
    parameter name accepting an environment variable from which to obtain
    the value when no value is explicitly provided.

    Parameters:
        *args: An optional [ApplyEnvironmentArgumentsOptions
            ](./#decorative_secrets.environment.ApplyEnvironmentArgumentsOptions)
            instance governing the behavior of this decorator. If not provided,
            a default instance of [ApplyEnvironmentArgumentsOptions()
            ](./#decorative_secrets.environment.ApplyEnvironmentArgumentsOptions).
            If multiple instances are provided, only the first will be used.
        **kwargs:
            A mapping of static parameter names to the parameter names
            of arguments accepting environment variable names from which to
            retrieve the value when the key argument is not explicitly
            provided.

    Example:
        ```python
        from functools import (
            cache,
        )
        from decorative_secrets.environment import (
            apply_environment_arguments,
        )
        from my_client_sdk import (
            Client,
        )


        @cache
        @apply_onepassword_arguments(
            client_id="client_id_environment_variable",
            client_secret="client_secret_environment_variable",
        )
        def get_client(
            client_id: str | None = None,
            client_secret: str = None,
            client_id_environment_variable: str | None = None,
            client_secret_environment_variable: str | None = None,
        ) -> Client:
            return Client(
                oauth2_client_id=client_id,
                oauth2_client_secret=client_secret,
            )


        client: Client = get_client(
            client_id_environment_variable=("CLIENT_ID",),
            client_secret_environment_variable=("CLIENT_SECRET",),
        )
        ```
    """
    options: ApplyEnvironmentArgumentsOptions
    _, options = _get_args_options(*args)
    return apply_callback_arguments(
        partial(_getenv, options.env),
        partial(_async_getenv, options.env),
        **kwargs,
    )