Some checks failed
API Endpoint Tests / test-api-endpoints (push) Successful in 17s
CLI Tests / test-cli (push) Failing after 3s
Cross-Node Transaction Testing / transaction-test (push) Successful in 3s
Deploy to Testnet / deploy-testnet (push) Has been cancelled
Multi-Node Stress Testing / stress-test (push) Has been cancelled
Node Failover Simulation / failover-test (push) Has been cancelled
Integration Tests / test-service-integration (push) Successful in 2m39s
Package Tests / Python package - aitbc-agent-sdk (push) Failing after 12s
Package Tests / Python package - aitbc-core (push) Successful in 12s
Package Tests / Python package - aitbc-crypto (push) Successful in 10s
Package Tests / Python package - aitbc-sdk (push) Failing after 7s
Package Tests / JavaScript package - aitbc-sdk-js (push) Successful in 6s
Package Tests / JavaScript package - aitbc-token (push) Successful in 14s
Python Tests / test-python (push) Failing after 9s
Security Scanning / security-scan (push) Successful in 15s
- Create aitbc/crypto/ subpackage (crypto.py, security.py) - Create aitbc/utils/ subpackage (validation, time_utils, json_utils, paths, env) - Create aitbc/network/ subpackage (http_client, web3_utils) - Update all import statements across codebase - Maintain backward compatibility with __init__.py exports - Improve code organization and modularity
115 lines
2.8 KiB
Python
115 lines
2.8 KiB
Python
"""
|
|
AITBC Environment Variable Helpers
|
|
Centralized utilities for loading and managing environment variables
|
|
"""
|
|
|
|
import os
|
|
from typing import Optional
|
|
from .exceptions import ConfigurationError
|
|
|
|
|
|
def get_env_var(key: str, default: str = "") -> str:
|
|
"""
|
|
Get an environment variable with a default value.
|
|
|
|
Args:
|
|
key: Environment variable name
|
|
default: Default value if not set
|
|
|
|
Returns:
|
|
Environment variable value or default
|
|
"""
|
|
return os.getenv(key, default)
|
|
|
|
|
|
def get_required_env_var(key: str) -> str:
|
|
"""
|
|
Get a required environment variable, raise error if not set.
|
|
|
|
Args:
|
|
key: Environment variable name
|
|
|
|
Returns:
|
|
Environment variable value
|
|
|
|
Raises:
|
|
ConfigurationError: If environment variable is not set
|
|
"""
|
|
value = os.getenv(key)
|
|
if value is None:
|
|
raise ConfigurationError(f"Required environment variable '{key}' is not set")
|
|
return value
|
|
|
|
|
|
def get_bool_env_var(key: str, default: bool = False) -> bool:
|
|
"""
|
|
Get a boolean environment variable.
|
|
|
|
Args:
|
|
key: Environment variable name
|
|
default: Default value if not set
|
|
|
|
Returns:
|
|
True if variable is set to 'true', '1', 'yes', or 'on' (case-insensitive)
|
|
False if variable is set to 'false', '0', 'no', or 'off' (case-insensitive)
|
|
Default value if not set
|
|
"""
|
|
value = os.getenv(key, "").lower()
|
|
if not value:
|
|
return default
|
|
return value in ("true", "1", "yes", "on")
|
|
|
|
|
|
def get_int_env_var(key: str, default: int = 0) -> int:
|
|
"""
|
|
Get an integer environment variable.
|
|
|
|
Args:
|
|
key: Environment variable name
|
|
default: Default value if not set or invalid
|
|
|
|
Returns:
|
|
Integer value or default
|
|
"""
|
|
try:
|
|
return int(os.getenv(key, str(default)))
|
|
except ValueError:
|
|
return default
|
|
|
|
|
|
def get_float_env_var(key: str, default: float = 0.0) -> float:
|
|
"""
|
|
Get a float environment variable.
|
|
|
|
Args:
|
|
key: Environment variable name
|
|
default: Default value if not set or invalid
|
|
|
|
Returns:
|
|
Float value or default
|
|
"""
|
|
try:
|
|
return float(os.getenv(key, str(default)))
|
|
except ValueError:
|
|
return default
|
|
|
|
|
|
def get_list_env_var(key: str, separator: str = ",", default: Optional[list] = None) -> list:
|
|
"""
|
|
Get a list environment variable.
|
|
|
|
Args:
|
|
key: Environment variable name
|
|
separator: Separator for list items
|
|
default: Default value if not set
|
|
|
|
Returns:
|
|
List of values or default
|
|
"""
|
|
if default is None:
|
|
default = []
|
|
value = os.getenv(key, "")
|
|
if not value:
|
|
return default
|
|
return [item.strip() for item in value.split(separator) if item.strip()]
|