Converters
provide.foundation.config.converters
¶
TODO: Add module docstring.
Functions¶
parse_bool_extended
¶
Parse boolean from string with lenient/forgiving interpretation.
This is the lenient boolean parser - designed for user-facing configuration where we want to be forgiving of various inputs. Any unrecognized string defaults to False rather than raising an error.
Use Cases: - Environment variables set by end users - Feature flags that should default to "off" if misconfigured - Optional telemetry settings where failure should not break the system
Recognized True Values: true, yes, 1, on (case-insensitive) Recognized False Values: false, no, 0, off (case-insensitive) Default Behavior: Any other string → False (no error)
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
value
|
str | bool
|
Boolean string representation or actual bool |
required |
Returns:
| Type | Description |
|---|---|
bool
|
Boolean value (defaults to False for unrecognized strings) |
Examples:
>>> parse_bool_extended("yes") # True
>>> parse_bool_extended("FALSE") # False
>>> parse_bool_extended("invalid") # False (no error)
>>> parse_bool_extended(True) # True
Source code in provide/foundation/parsers/primitives.py
parse_bool_strict
¶
Parse boolean from string with strict validation and clear error messages.
This is the strict boolean parser - designed for internal APIs and critical configuration where invalid values should cause immediate failure with helpful error messages.
Use Cases: - Internal API parameters where precision matters - Critical system configurations where misconfiguration is dangerous - Programmatic configuration where clear validation errors help developers
Recognized True Values: true, yes, 1, on, enabled (case-insensitive), 1.0 Recognized False Values: false, no, 0, off, disabled (case-insensitive), 0.0 Error Behavior: Raises ValueError with helpful message for invalid values
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
value
|
str | bool | int | float
|
Boolean value as string, bool, int, or float |
required |
Returns:
| Type | Description |
|---|---|
bool
|
Boolean value (never defaults - raises on invalid input) |
Raises:
| Type | Description |
|---|---|
TypeError
|
If value is not a string, bool, int, or float |
ValueError
|
If value cannot be parsed as boolean |
Examples:
>>> parse_bool_strict("yes") # True
>>> parse_bool_strict("FALSE") # False
>>> parse_bool_strict(1) # True
>>> parse_bool_strict(0.0) # False
>>> parse_bool_strict("invalid") # ValueError with helpful message
>>> parse_bool_strict(42) # ValueError - only 0/1 valid for numbers
Source code in provide/foundation/parsers/primitives.py
parse_comma_list
¶
parse_console_formatter
¶
Parse and validate console formatter string.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
value
|
str
|
Formatter string (case-insensitive) |
required |
Returns:
| Type | Description |
|---|---|
ConsoleFormatterStr
|
Valid formatter string in lowercase |
Raises:
| Type | Description |
|---|---|
ValueError
|
If the formatter is invalid |
Source code in provide/foundation/parsers/telemetry.py
parse_float_with_validation
¶
parse_float_with_validation(
value: str,
min_val: float | None = None,
max_val: float | None = None,
) -> float
Parse float with optional range validation.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
value
|
str
|
String representation of float |
required |
min_val
|
float | None
|
Minimum allowed value (inclusive) |
None
|
max_val
|
float | None
|
Maximum allowed value (inclusive) |
None
|
Returns:
| Type | Description |
|---|---|
float
|
Parsed float value |
Raises:
| Type | Description |
|---|---|
ValueError
|
If value is not a valid float or out of range |
Source code in provide/foundation/parsers/primitives.py
parse_foundation_log_output
¶
Parse and validate foundation log output destination.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
value
|
str
|
Output destination string |
required |
Returns:
| Type | Description |
|---|---|
str
|
Valid output destination (stderr, stdout, main) |
Raises:
| Type | Description |
|---|---|
ValueError
|
If the value is invalid |
Source code in provide/foundation/parsers/telemetry.py
parse_headers
¶
Parse HTTP headers from string format.
Format Requirements: - Comma-separated key=value pairs: "key1=value1,key2=value2" - Header names and values are trimmed of whitespace - Empty header names are ignored - Each pair must contain exactly one '=' separator - Invalid pairs are silently skipped
Examples: >>> parse_headers("Authorization=Bearer token,Content-Type=application/json")
>>> parse_headers("X-API-Key=secret123") # Single header
{'X-API-Key': 'secret123'}
>>> parse_headers("valid=ok,invalid-no-equals,another=good") # Partial success
{'valid': 'ok', 'another': 'good'}
>>> parse_headers("empty-value=") # Empty values allowed
{'empty-value': ''}
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
value
|
str | dict[str, str]
|
Comma-separated key=value pairs for HTTP headers, or dict if already parsed |
required |
Returns:
| Type | Description |
|---|---|
dict[str, str]
|
Dictionary of header name-value pairs. |
dict[str, str]
|
Invalid entries are silently ignored. |
Note
This parser is lenient by design - invalid header pairs are skipped rather than raising errors to allow partial configuration success in production environments.
Source code in provide/foundation/parsers/structured.py
parse_json_dict
¶
Parse JSON string into dictionary.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
value
|
str
|
JSON string |
required |
Returns:
| Type | Description |
|---|---|
dict[str, Any]
|
Parsed dictionary |
Raises:
| Type | Description |
|---|---|
ValueError
|
If JSON is invalid |
Source code in provide/foundation/parsers/primitives.py
parse_json_list
¶
Parse JSON string into list.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
value
|
str
|
JSON string |
required |
Returns:
| Type | Description |
|---|---|
list[Any]
|
Parsed list |
Raises:
| Type | Description |
|---|---|
ValueError
|
If JSON is invalid |
Source code in provide/foundation/parsers/primitives.py
parse_log_level
¶
Parse and validate log level string.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
value
|
str
|
Log level string (case-insensitive) |
required |
Returns:
| Type | Description |
|---|---|
LogLevelStr
|
Valid log level string in uppercase |
Raises:
| Type | Description |
|---|---|
ValueError
|
If the log level is invalid |
Source code in provide/foundation/parsers/telemetry.py
parse_module_levels
¶
Parse module-specific log levels from string format.
Format Requirements: - String format: "module1:LEVEL,module2:LEVEL" (comma-separated pairs) - Dict format: Already parsed dictionary (validated and returned) - Log levels must be valid: TRACE, DEBUG, INFO, WARNING, ERROR, CRITICAL - Module names are trimmed of whitespace - Invalid log levels are silently ignored
Examples: >>> parse_module_levels("auth.service:DEBUG,database:ERROR")
>>> parse_module_levels("api:INFO") # Single module
{'api': 'INFO'}
>>> parse_module_levels({"web": "warning"}) # Dict input (case normalized)
{'web': 'WARNING'}
>>> parse_module_levels("api:INFO,bad:INVALID,db:ERROR") # Partial success
{'api': 'INFO', 'db': 'ERROR'}
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
value
|
str | dict[str, str]
|
Comma-separated module:level pairs or pre-parsed dict |
required |
Returns:
| Type | Description |
|---|---|
dict[str, LogLevelStr]
|
Dictionary mapping module names to validated log level strings. |
dict[str, LogLevelStr]
|
Invalid entries are silently ignored. |
Note
This parser is lenient by design - invalid log levels are skipped rather than raising errors to allow partial configuration success in production environments.
Source code in provide/foundation/parsers/structured.py
parse_rate_limits
¶
Parse per-logger rate limits from string format.
Format Requirements: - Comma-separated triplets: "logger1:rate:capacity,logger2:rate:capacity" - Rate and capacity must be valid float numbers - Logger names are trimmed of whitespace - Empty logger names are ignored - Invalid entries are silently skipped to allow partial success
Examples: >>> parse_rate_limits("api:10.0:100.0,worker:5.0:50.0")
>>> parse_rate_limits("db:1.5:25.0") # Single entry
{'db': (1.5, 25.0)}
>>> parse_rate_limits("api:10:100,invalid:bad,worker:5:50") # Partial success
{'api': (10.0, 100.0), 'worker': (5.0, 50.0)}
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
value
|
str
|
Comma-separated logger:rate:capacity triplets |
required |
Returns:
| Type | Description |
|---|---|
dict[str, tuple[float, float]]
|
Dictionary mapping logger names to (rate, capacity) tuples. |
dict[str, tuple[float, float]]
|
Invalid entries are silently ignored. |
Note
This parser is lenient by design - invalid entries are skipped rather than raising errors to allow partial configuration success in production environments.
Source code in provide/foundation/parsers/structured.py
parse_sample_rate
¶
Parse sampling rate (0.0 to 1.0).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
value
|
str
|
String representation of sampling rate |
required |
Returns:
| Type | Description |
|---|---|
float
|
Float between 0.0 and 1.0 |
Raises:
| Type | Description |
|---|---|
ValueError
|
If value is not valid or out of range |
Source code in provide/foundation/parsers/primitives.py
validate_choice
¶
Create a validator that ensures value is one of the given choices.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
choices
|
list[Any]
|
List of valid choices |
required |
Returns:
| Type | Description |
|---|---|
Callable[[Any, Any, Any], None]
|
Validator function for use with attrs |
Source code in provide/foundation/config/validators.py
validate_log_level
¶
Validate that a log level is valid.
Source code in provide/foundation/config/validators.py
validate_non_negative
¶
Validate that a value is non-negative.
Source code in provide/foundation/config/validators.py
validate_overflow_policy
¶
Validate rate limit overflow policy.
Source code in provide/foundation/config/validators.py
validate_port
¶
Validate that a port number is valid.
Source code in provide/foundation/config/validators.py
validate_positive
¶
Validate that a value is positive.
Source code in provide/foundation/config/validators.py
validate_range
¶
Create a validator that ensures value is within the given numeric range.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
min_val
|
float
|
Minimum allowed value (inclusive) |
required |
max_val
|
float
|
Maximum allowed value (inclusive) |
required |
Returns:
| Type | Description |
|---|---|
Callable[[Any, Any, Any], None]
|
Validator function for use with attrs |
Source code in provide/foundation/config/validators.py
validate_sample_rate
¶
Validate that a sample rate is between 0.0 and 1.0.