Public API¶
This section covers the main functions intended for direct use in your applications.
Logging Support¶
All public API functions and EnvResolver methods accept an optional logger parameter for diagnostic logging. See the Logging Guide for details.
import logging
import envresolve
logger = logging.getLogger(__name__)
result = envresolve.resolve_secret("${SECRET}", logger=logger)
envresolve.api
¶
Public API for envresolve.
EnvResolver
¶
Manages provider registration and secret resolution.
This class encapsulates the provider registry and resolver instance, eliminating the need for module-level global variables.
Source code in src/envresolve/api.py
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 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 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 | |
__init__
¶
Initialize an empty provider registry.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
logger
|
Logger | None
|
Optional logger for diagnostic messages. If None, no logging occurs. |
None
|
Source code in src/envresolve/api.py
register_azure_kv_provider
¶
register_azure_kv_provider(
provider: SecretProvider | None = None,
) -> None
Register Azure Key Vault provider for akv:// scheme.
This method is safe to call multiple times (idempotent).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
provider
|
SecretProvider | None
|
Optional custom provider. If None, uses default AzureKVProvider. |
None
|
Raises:
| Type | Description |
|---|---|
ProviderRegistrationError
|
If azure-identity or azure-keyvault-secrets is not installed (only when provider is None) |
Source code in src/envresolve/api.py
resolve_secret
¶
Resolve a secret URI to its value.
This function supports: - Variable expansion: ${VAR} and $VAR syntax using os.environ - Secret URI resolution: akv:// scheme - Idempotent resolution: Plain strings and non-target URIs pass through
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
uri
|
str
|
Secret URI or plain string to resolve |
required |
logger
|
Logger | None
|
Optional logger for diagnostic messages. If provided, overrides the logger set in the constructor. |
None
|
Returns:
| Type | Description |
|---|---|
str
|
Resolved secret value or the original string if not a secret URI |
Raises:
| Type | Description |
|---|---|
URIParseError
|
If the URI format is invalid |
SecretResolutionError
|
If secret resolution fails |
VariableNotFoundError
|
If a referenced variable is not found |
InvalidVariableNameError
|
If a variable name is invalid |
CircularReferenceError
|
If a circular variable reference is detected |
Source code in src/envresolve/api.py
resolve_with_env
¶
Expand variables and resolve secret URIs with custom environment.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
value
|
str
|
Value to resolve (may contain variables or be a secret URI) |
required |
env
|
dict[str, str]
|
Environment dict for variable expansion |
required |
logger
|
Logger | None
|
Optional logger for diagnostic messages. If provided, overrides the logger set in the constructor. |
None
|
Returns:
| Type | Description |
|---|---|
str
|
Resolved value |
Source code in src/envresolve/api.py
load_env
¶
load_env(
dotenv_path: str | Path | None = None,
*,
export: bool = True,
override: bool = False,
stop_on_expansion_error: bool = True,
stop_on_resolution_error: bool = True,
ignore_keys: list[str] | None = None,
ignore_patterns: list[str] | None = None,
logger: Logger | None = None,
) -> dict[str, str]
Load environment variables from a .env file and resolve secret URIs.
This function: 1. Loads variables from the .env file 2. Expands variable references within values 3. Resolves secret URIs (akv://) to actual secret values 4. Optionally exports to os.environ
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
dotenv_path
|
str | Path | None
|
Path to .env file. If None, searches for .env in current directory. Mimics python-dotenv's load_dotenv() behavior. (default: None) |
None
|
export
|
bool
|
If True, export resolved variables to os.environ |
True
|
override
|
bool
|
If True, override existing os.environ variables |
False
|
stop_on_expansion_error
|
bool
|
If False, skip variables with expansion errors (e.g., VariableNotFoundError). CircularReferenceError is always raised as it indicates a configuration error. (default: True) |
True
|
stop_on_resolution_error
|
bool
|
If False, skip variables with resolution errors (e.g., SecretResolutionError). Useful for resilience against transient secret store failures. (default: True) |
True
|
ignore_keys
|
list[str] | None
|
List of keys to skip expansion for. These keys are included in the result as-is without variable expansion or secret resolution. (default: None) |
None
|
ignore_patterns
|
list[str] | None
|
List of glob patterns to match keys for skipping expansion. Keys matching any pattern are included as-is without variable expansion or secret resolution. Supports wildcards: *, ?, [seq]. (default: None) |
None
|
logger
|
Logger | None
|
Optional logger for diagnostic messages. If provided, overrides the logger set in the constructor. |
None
|
Returns:
| Type | Description |
|---|---|
dict[str, str]
|
Dictionary of resolved environment variables |
Raises:
| Type | Description |
|---|---|
EnvironmentVariableResolutionError
|
If a variable resolution error occurs (wraps VariableNotFoundError or SecretResolutionError with context) |
URIParseError
|
If a URI format is invalid |
CircularReferenceError
|
If a circular variable reference is detected |
Source code in src/envresolve/api.py
146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 | |
resolve_os_environ
¶
resolve_os_environ(
keys: list[str] | None = None,
prefix: str | None = None,
*,
overwrite: bool = True,
stop_on_expansion_error: bool = True,
stop_on_resolution_error: bool = True,
ignore_keys: list[str] | None = None,
ignore_patterns: list[str] | None = None,
logger: Logger | None = None,
) -> dict[str, str]
Resolve secret URIs in os.environ.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
keys
|
list[str] | None
|
List of specific environment variable keys to resolve |
None
|
prefix
|
str | None
|
Prefix to filter environment variables |
None
|
overwrite
|
bool
|
If True, overwrite existing os.environ variables |
True
|
stop_on_expansion_error
|
bool
|
If False, skip variables with expansion errors |
True
|
stop_on_resolution_error
|
bool
|
If False, skip variables with resolution errors |
True
|
ignore_keys
|
list[str] | None
|
List of keys to skip expansion for |
None
|
ignore_patterns
|
list[str] | None
|
List of glob patterns to match keys for skipping expansion |
None
|
logger
|
Logger | None
|
Optional logger for diagnostic messages. If provided, overrides the logger set in the constructor. |
None
|
Returns:
| Type | Description |
|---|---|
dict[str, str]
|
Dictionary of resolved environment variables |
Raises:
| Type | Description |
|---|---|
EnvironmentVariableResolutionError
|
If a variable resolution error occurs (wraps VariableNotFoundError or SecretResolutionError with context) |
MutuallyExclusiveArgumentsError
|
If both keys and prefix are specified |
URIParseError
|
If the URI format is invalid |
CircularReferenceError
|
If a circular variable reference is detected |
Source code in src/envresolve/api.py
set_logger
¶
Set the default logger for the global facade functions.
This function configures the logger used by the default EnvResolver instance that backs the module-level facade functions (resolve_secret, load_env, etc.).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
logger
|
Logger | None
|
Logger instance for diagnostic messages. If None, logging is disabled. |
required |
Examples:
>>> import envresolve
>>> import logging
>>> # Set up logging
>>> logger = logging.getLogger(__name__)
>>> envresolve.set_logger(logger)
>>> # Now all module-level functions will use this logger
>>> envresolve.resolve_secret("akv://vault/secret")
>>> # Disable logging
>>> envresolve.set_logger(None)
Source code in src/envresolve/api.py
register_azure_kv_provider
¶
register_azure_kv_provider(
provider: SecretProvider | None = None,
) -> None
Register Azure Key Vault provider for akv:// scheme.
This function should be called before attempting to resolve secrets from Azure Key Vault. It is safe to call multiple times (idempotent).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
provider
|
SecretProvider | None
|
Optional custom provider. If None, uses default AzureKVProvider. |
None
|
Raises:
| Type | Description |
|---|---|
ProviderRegistrationError
|
If azure-identity or azure-keyvault-secrets is not installed (only when provider is None) |
Examples:
>>> import envresolve
>>> # Default behavior
>>> envresolve.register_azure_kv_provider()
>>> # Custom provider (requires Azure SDK imports)
>>> # from envresolve.providers.azure_kv import AzureKVProvider
>>> # from azure.identity import ManagedIdentityCredential
>>> # custom = AzureKVProvider(credential=ManagedIdentityCredential())
>>> # envresolve.register_azure_kv_provider(provider=custom)
>>> # Now you can resolve secrets (requires Azure authentication)
>>> # secret = envresolve.resolve_secret("akv://my-vault/db-password")
Source code in src/envresolve/api.py
resolve_secret
¶
Resolve a secret URI to its value.
This function supports: - Variable expansion: ${VAR} and $VAR syntax using os.environ - Secret URI resolution: akv:// scheme - Idempotent resolution: Plain strings and non-target URIs pass through unchanged
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
uri
|
str
|
Secret URI or plain string to resolve |
required |
logger
|
Logger | None
|
Optional logger for diagnostic messages. If provided, overrides the global default logger set by set_logger(). |
None
|
Returns:
| Type | Description |
|---|---|
str
|
Resolved secret value or the original string if not a secret URI |
Raises:
| Type | Description |
|---|---|
URIParseError
|
If the URI format is invalid |
SecretResolutionError
|
If secret resolution fails |
VariableNotFoundError
|
If a referenced variable is not found |
InvalidVariableNameError
|
If a variable name is invalid |
CircularReferenceError
|
If a circular variable reference is detected |
Examples:
>>> import envresolve
>>> # Idempotent - plain strings pass through
>>> value = envresolve.resolve_secret("just-a-string")
>>> value
'just-a-string'
>>> # Non-target URIs pass through unchanged
>>> uri = envresolve.resolve_secret("postgres://localhost/db")
>>> uri
'postgres://localhost/db'
>>> # Secret URIs require provider registration and authentication
>>> # envresolve.register_azure_kv_provider()
>>> # secret = envresolve.resolve_secret("akv://my-vault/db-password")
Source code in src/envresolve/api.py
load_env
¶
load_env(
dotenv_path: str | Path | None = None,
*,
export: bool = True,
override: bool = False,
stop_on_expansion_error: bool = True,
stop_on_resolution_error: bool = True,
ignore_keys: list[str] | None = None,
ignore_patterns: list[str] | None = None,
logger: Logger | None = None,
) -> dict[str, str]
Load environment variables from a .env file and resolve secret URIs.
This function: 1. Loads variables from the .env file 2. Expands variable references within values 3. Resolves secret URIs (akv://) to actual secret values 4. Optionally exports to os.environ
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
dotenv_path
|
str | Path | None
|
Path to .env file. If None, searches for .env in current directory. Mimics python-dotenv's load_dotenv() behavior. (default: None) |
None
|
export
|
bool
|
If True, export resolved variables to os.environ (default: True) |
True
|
override
|
bool
|
If True, override existing os.environ variables (default: False) |
False
|
stop_on_expansion_error
|
bool
|
If False, skip variables with expansion errors (e.g., VariableNotFoundError). CircularReferenceError is always raised as it indicates a configuration error. (default: True) |
True
|
stop_on_resolution_error
|
bool
|
If False, skip variables with resolution errors (e.g., SecretResolutionError). Useful for resilience against transient secret store failures. (default: True) |
True
|
ignore_keys
|
list[str] | None
|
List of keys to skip expansion for. These keys are included in the result as-is without variable expansion or secret resolution. (default: None) |
None
|
ignore_patterns
|
list[str] | None
|
List of glob patterns to match keys for skipping expansion. Keys matching any pattern are included as-is without variable expansion or secret resolution. Supports wildcards: *, ?, [seq]. (default: None) |
None
|
logger
|
Logger | None
|
Optional logger for diagnostic messages. If provided, overrides the global default logger set by set_logger(). |
None
|
Returns:
| Type | Description |
|---|---|
dict[str, str]
|
Dictionary of resolved environment variables |
Raises:
| Type | Description |
|---|---|
EnvironmentVariableResolutionError
|
If a variable resolution error occurs (wraps VariableNotFoundError or SecretResolutionError with context) |
URIParseError
|
If a URI format is invalid |
CircularReferenceError
|
If a circular variable reference is detected |
Examples:
>>> import envresolve
>>> envresolve.register_azure_kv_provider()
>>> # Load and export to os.environ (searches for .env in cwd)
>>> resolved = envresolve.load_env(export=True)
>>> # Load specific file without exporting
>>> resolved = envresolve.load_env("custom.env", export=False)
>>> # Skip expansion for system variables
>>> resolved = envresolve.load_env(ignore_keys=["PS1"])
Source code in src/envresolve/api.py
resolve_os_environ
¶
resolve_os_environ(
keys: list[str] | None = None,
prefix: str | None = None,
*,
overwrite: bool = True,
stop_on_expansion_error: bool = True,
stop_on_resolution_error: bool = True,
ignore_keys: list[str] | None = None,
ignore_patterns: list[str] | None = None,
logger: Logger | None = None,
) -> dict[str, str]
Resolve secret URIs in os.environ.
This function resolves secret URIs that are already set in environment variables, useful when values are passed from parent shells or container orchestrators.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
keys
|
list[str] | None
|
List of specific keys to resolve. If None, scan all keys. Mutually exclusive with prefix. |
None
|
prefix
|
str | None
|
Only process keys with this prefix, strip prefix from output. Mutually exclusive with keys. |
None
|
overwrite
|
bool
|
If True, update os.environ with resolved values (default: True). |
True
|
stop_on_expansion_error
|
bool
|
If False, skip variables with expansion errors (e.g., VariableNotFoundError). CircularReferenceError is always raised as it indicates a configuration error. (default: True) |
True
|
stop_on_resolution_error
|
bool
|
If False, skip variables with resolution errors (e.g., SecretResolutionError). Useful for resilience against transient secret store failures. (default: True) |
True
|
ignore_keys
|
list[str] | None
|
List of keys to skip expansion for. These keys are included in the result as-is without variable expansion or secret resolution. (default: None) |
None
|
ignore_patterns
|
list[str] | None
|
List of glob patterns to match keys for skipping expansion. Keys matching any pattern are included as-is without variable expansion or secret resolution. Supports wildcards: *, ?, [seq]. (default: None) |
None
|
logger
|
Logger | None
|
Optional logger for diagnostic messages. If provided, overrides the global default logger set by set_logger(). |
None
|
Returns:
| Type | Description |
|---|---|
dict[str, str]
|
Dictionary of resolved values |
Raises:
| Type | Description |
|---|---|
EnvironmentVariableResolutionError
|
If a variable resolution error occurs (wraps VariableNotFoundError or SecretResolutionError with context) |
MutuallyExclusiveArgumentsError
|
If both keys and prefix are specified |
URIParseError
|
If the URI format is invalid |
CircularReferenceError
|
If a circular variable reference is detected |
Examples:
>>> import envresolve
>>> import os
>>> envresolve.register_azure_kv_provider()
>>> # Resolve all environment variables
>>> resolved = envresolve.resolve_os_environ()
>>> # Resolve specific keys only
>>> resolved = envresolve.resolve_os_environ(keys=["API_KEY"])
>>> # Skip expansion for system variables
>>> resolved = envresolve.resolve_os_environ(
... ignore_keys=["PS1"]
... )