Index
provide.foundation.utils.environment
¶
TODO: Add module docstring.
Classes¶
EnvPrefix
¶
Environment variable reader with prefix support.
Provides convenient access to environment variables with a common prefix, useful for application-specific configuration namespacing.
Uses caching to improve performance for repeated name lookups.
Examples:
>>> app_env = EnvPrefix('MYAPP')
>>> app_env.get_bool('DEBUG') # Reads MYAPP_DEBUG
>>> app_env['database_url'] # Reads MYAPP_DATABASE_URL
Initialize with prefix.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
prefix
|
str
|
Prefix for all environment variables |
required |
separator
|
str
|
Separator between prefix and variable name |
'_'
|
Source code in provide/foundation/utils/environment/prefix.py
Functions¶
__contains__
¶
__getitem__
¶
all_with_prefix
¶
Get all environment variables with this prefix.
Returns:
| Type | Description |
|---|---|
dict[str, str]
|
Dictionary of variable names (without prefix) to values |
Source code in provide/foundation/utils/environment/prefix.py
get_bool
¶
get_dict
¶
get_dict(
name: str,
default: dict[str, str] | None = None,
item_separator: str = ",",
key_value_separator: str = "=",
) -> dict[str, str]
Get dictionary with prefix.
Source code in provide/foundation/utils/environment/prefix.py
get_float
¶
get_int
¶
get_list
¶
get_path
¶
get_str
¶
require
¶
Functions¶
get_bool
¶
Get boolean environment variable.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name
|
str
|
Environment variable name |
required |
default
|
bool | None
|
Default value if not set |
None
|
Returns:
| Type | Description |
|---|---|
bool | None
|
Boolean value, None (if set but empty), or default (if unset) |
Note
Empty string is treated as ambiguous and returns None with a warning. Unset variable returns the default value.
Examples:
Source code in provide/foundation/utils/environment/getters.py
get_dict
¶
get_dict(
name: str,
default: dict[str, str] | None = None,
item_separator: str = ",",
key_value_separator: str = "=",
) -> dict[str, str]
Get dictionary from environment variable.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name
|
str
|
Environment variable name |
required |
default
|
dict[str, str] | None
|
Default dict if not set |
None
|
item_separator
|
str
|
Separator between items |
','
|
key_value_separator
|
str
|
Separator between key and value |
'='
|
Returns:
| Type | Description |
|---|---|
dict[str, str]
|
Dictionary of string key-value pairs |
Examples:
>>> os.environ['CONFIG'] = 'key1=val1,key2=val2'
>>> get_dict('CONFIG')
{'key1': 'val1', 'key2': 'val2'}
Source code in provide/foundation/utils/environment/getters.py
get_float
¶
Get float environment variable.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name
|
str
|
Environment variable name |
required |
default
|
float | None
|
Default value if not set |
None
|
Returns:
| Type | Description |
|---|---|
float | None
|
Float value or default |
Raises:
| Type | Description |
|---|---|
ValidationError
|
If value cannot be parsed as float |
Source code in provide/foundation/utils/environment/getters.py
get_int
¶
Get integer environment variable.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name
|
str
|
Environment variable name |
required |
default
|
int | None
|
Default value if not set |
None
|
Returns:
| Type | Description |
|---|---|
int | None
|
Integer value or default |
Raises:
| Type | Description |
|---|---|
ValidationError
|
If value cannot be parsed as integer |
Source code in provide/foundation/utils/environment/getters.py
get_list
¶
Get list from environment variable.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name
|
str
|
Environment variable name |
required |
default
|
list[str] | None
|
Default list if not set |
None
|
separator
|
str
|
String separator (default: comma) |
','
|
Returns:
| Type | Description |
|---|---|
list[str]
|
List of strings |
Examples:
Source code in provide/foundation/utils/environment/getters.py
get_path
¶
Get path environment variable.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name
|
str
|
Environment variable name |
required |
default
|
Path | str | None
|
Default path if not set |
None
|
Returns:
| Type | Description |
|---|---|
Path | None
|
Path object or None |
Source code in provide/foundation/utils/environment/getters.py
get_str
¶
parse_duration
¶
Parse duration string to seconds.
Supports formats like: 30s, 5m, 2h, 1d, 1h30m, etc.
Results are cached for performance on repeated calls.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
value
|
str
|
Duration string |
required |
Returns:
| Type | Description |
|---|---|
int
|
Duration in seconds |
Examples:
Source code in provide/foundation/utils/environment/parsers.py
parse_size
¶
Parse size string to bytes.
Supports formats like: 1024, 1KB, 10MB, 1.5GB, etc.
Results are cached for performance on repeated calls.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
value
|
str
|
Size string |
required |
Returns:
| Type | Description |
|---|---|
int
|
Size in bytes |
Examples:
Source code in provide/foundation/utils/environment/parsers.py
require
¶
Require an environment variable to be set.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name
|
str
|
Environment variable name |
required |
type_hint
|
type[T] | None
|
Optional type hint for parsing |
None
|
Returns:
| Type | Description |
|---|---|
Any
|
Parsed value |
Raises:
| Type | Description |
|---|---|
ValidationError
|
If variable is not set |