Loadersο
Configuration Loadersο
Configuration loaders for Confii.
This module provides loader classes for loading configuration from various sources including files (YAML, JSON, TOML, INI), environment variables, and remote sources (HTTP, cloud storage, Git).
Available Loadersο
- File Loaders:
YamlLoader: Load from YAML files (.yaml, .yml)
JsonLoader: Load from JSON files (.json)
TomlLoader: Load from TOML files (.toml)
IniLoader: Load from INI files (.ini, .cfg)
- Environment Loaders:
EnvironmentLoader: Load from system environment variables
- Remote Loaders:
HTTPLoader: Load from HTTP/HTTPS URLs
S3Loader: Load from AWS S3
SSMLoader: Load from AWS SSM Parameter Store
AzureBlobLoader: Load from Azure Blob Storage
GCPStorageLoader: Load from Google Cloud Storage
IBMCloudObjectStorageLoader: Load from IBM Cloud Object Storage
GitLoader: Load from Git repositories
Example
>>> from confii.loaders import YamlLoader, EnvironmentLoader
>>> from confii import Config
>>>
>>> config = Config(loaders=[YamlLoader("config.yaml"), EnvironmentLoader("APP")])
- class confii.loaders.AzureBlobLoader(container_url: str, blob_name: str, account_name: str | None = None, account_key: str | None = None, sas_token: str | None = None, connection_string: str | None = None)[source]ο
Bases:
RemoteLoaderLoad configuration from Azure Blob Storage.
AzureBlobLoader downloads configuration files from Azure Blob Storage containers. It supports multiple authentication methods: connection strings, account key, SAS tokens, and Azure Managed Identity via
DefaultAzureCredential.- urlο
Synthetic URI in the form
azure://<container>/<blob>.
- container_urlο
The Azure container URL or container name.
- blob_nameο
The blob (file) name within the container.
- account_nameο
Azure Storage account name.
- account_keyο
Azure Storage account key (may be None).
- sas_tokenο
Shared Access Signature token (may be None).
- connection_stringο
Full Azure Storage connection string (may be None).
- configο
The loaded configuration dictionary.
Example
>>> from confii.loaders.remote_loader import AzureBlobLoader >>> loader = AzureBlobLoader( ... container_url="my-container", ... blob_name="config/app.yaml", ... account_name="mystorageaccount", ... account_key="base64key==", ... ) >>> config_dict = loader.load()
Note
Requires the
azure-storage-blobpackage. Install it with:pip install azure-storage-blob
Environment variable fallbacks:
AZURE_STORAGE_ACCOUNT,AZURE_STORAGE_KEY,AZURE_SAS_TOKEN,AZURE_STORAGE_CONNECTION_STRING.- __init__(container_url: str, blob_name: str, account_name: str | None = None, account_key: str | None = None, sas_token: str | None = None, connection_string: str | None = None)[source]ο
Initialize Azure Blob loader.
- Parameters:
container_url β Azure container URL or plain container name.
blob_name β Name of the blob (file) to load.
account_name β Azure storage account name. Falls back to
AZURE_STORAGE_ACCOUNTenvironment variable.account_key β Azure storage account key. Falls back to
AZURE_STORAGE_KEYenvironment variable.sas_token β Shared Access Signature token. Falls back to
AZURE_SAS_TOKENenvironment variable.connection_string β Full connection string (alternative to individual credentials). Falls back to
AZURE_STORAGE_CONNECTION_STRINGenvironment variable.
- load() Dict[str, Any][source]ο
Load configuration from Azure Blob Storage.
Downloads the blob content and parses it based on the blob nameβs file extension (e.g.,
.yaml,.json,.toml).- Returns:
Dictionary containing the loaded configuration.
- Raises:
ImportError β If
azure-storage-blobis not installed.ValueError β If
account_nameis required but not provided.ConfigLoadError β If the Azure request fails or the content cannot be parsed.
Example
>>> loader = AzureBlobLoader( ... "my-container", "config.yaml", account_name="myaccount" ... ) >>> config = loader.load()
- class confii.loaders.EnvFileLoader(source: str = '.env')[source]ο
Bases:
LoaderLoader for .env files with support for nested configurations.
EnvFileLoader parses
.envfiles in the standardKEY=VALUEformat used by tools such as Docker Compose, Heroku, and direnv. It supports quoted values, inline comments, escape sequences in double-quoted strings, and nested key structures via dot notation.- sourceο
Path to the
.envconfiguration file.
- configο
Loaded configuration dictionary.
Example
>>> from confii.loaders import EnvFileLoader >>> from confii import Config >>> >>> loader = EnvFileLoader(".env") >>> config = Config(loaders=[loader]) >>> print(config.DATABASE_URL)
Note
The
.envformat supports the following features:Comments: lines starting with
#are ignored.Quoting: values wrapped in single or double quotes have the quotes stripped. Single-quoted values are treated literally; double-quoted values process
\nand\tescapes.Inline comments:
VALUE # commentis supported for unquoted values (the# commentpart is stripped).Nested keys: dot-separated keys like
database.host=localhostare expanded into nested dictionaries.Type coercion: values are automatically converted to int, float, bool, or None where appropriate.
Missing files return None instead of raising an exception.
- __init__(source: str = '.env')[source]ο
Initialize the .env file loader.
- Parameters:
source β Path to the .env file. Defaults to
".env"in the current working directory.
- load() Dict[str, Any] | None[source]ο
Load configuration from a .env file.
Parses each
KEY=VALUEline, strips quotes, handles escape sequences, expands dot-notation keys into nested dictionaries, and coerces scalar values to appropriate Python types.- Returns:
Dictionary containing the loaded configuration, or None if the file does not exist.
- Raises:
PermissionError β If the file exists but is not readable.
UnicodeDecodeError β If the file contains non-UTF-8 content.
Example
>>> loader = EnvFileLoader(".env.production") >>> config_dict = loader.load() >>> if config_dict: ... print(config_dict["database"]["host"])
- class confii.loaders.EnvironmentLoader(prefix: str, separator: str = '__')[source]ο
Bases:
LoaderLoader for environment variables with prefix support.
This loader reads environment variables with a specific prefix and converts them into a nested configuration structure.
- class confii.loaders.GCPStorageLoader(bucket_name: str, blob_name: str, project_id: str | None = None, credentials_path: str | None = None)[source]ο
Bases:
RemoteLoaderLoad configuration from Google Cloud Storage.
GCPStorageLoader downloads configuration files from GCS buckets. Authentication is handled via explicit service account credentials or Application Default Credentials (ADC).
- urlο
Synthetic URI in the form
gs://<bucket>/<blob>.
- bucket_nameο
The GCS bucket name.
- blob_nameο
The blob (file) name within the bucket.
- project_idο
GCP project ID.
- credentials_pathο
Path to a service account JSON key file.
- configο
The loaded configuration dictionary.
Example
>>> from confii.loaders.remote_loader import GCPStorageLoader >>> loader = GCPStorageLoader( ... bucket_name="my-config-bucket", ... blob_name="services/app.yaml", ... project_id="my-gcp-project", ... ) >>> config_dict = loader.load()
Note
Requires the
google-cloud-storagepackage. Install it with:pip install google-cloud-storage
Environment variable fallbacks:
GCP_PROJECT_ID,GOOGLE_APPLICATION_CREDENTIALS.- __init__(bucket_name: str, blob_name: str, project_id: str | None = None, credentials_path: str | None = None)[source]ο
Initialize GCP Storage loader.
- Parameters:
bucket_name β GCS bucket name.
blob_name β Name of the blob (file) to load.
project_id β GCP project ID. Falls back to the
GCP_PROJECT_IDenvironment variable.credentials_path β Path to a service account JSON key file. Falls back to the
GOOGLE_APPLICATION_CREDENTIALSenvironment variable.
- load() Dict[str, Any][source]ο
Load configuration from Google Cloud Storage.
Downloads the blob content as text and parses it based on the blob nameβs file extension.
- Returns:
Dictionary containing the loaded configuration.
- Raises:
ImportError β If
google-cloud-storageis not installed.ConfigLoadError β If the GCS request fails or the content cannot be parsed.
Example
>>> loader = GCPStorageLoader("my-bucket", "config.yaml") >>> config = loader.load()
- class confii.loaders.GitLoader(repo_url: str, file_path: str, branch: str = 'main', token: str | None = None)[source]ο
Bases:
RemoteLoaderLoad configuration from a Git repository hosted on GitHub or GitLab.
GitLoader constructs a raw-content URL for the specified file and branch, then delegates to HTTPLoader to fetch and parse it. Private repositories are supported via personal access tokens.
- urlο
The Git repository URL (e.g.,
https://github.com/org/repo).
- file_pathο
Path to the configuration file within the repository.
- branchο
The Git branch to read from.
- tokenο
Access token for private repositories (may be None).
- configο
The loaded configuration dictionary.
Example
>>> from confii.loaders.remote_loader import GitLoader >>> loader = GitLoader( ... repo_url="https://github.com/myorg/myrepo", ... file_path="config/production.yaml", ... branch="main", ... token="ghp_xxxxxxxxxxxx", ... ) >>> config_dict = loader.load()
Note
Currently supports GitHub and GitLab. The
GIT_TOKENenvironment variable is used as a fallback whentokenis not provided.- __init__(repo_url: str, file_path: str, branch: str = 'main', token: str | None = None)[source]ο
Initialize Git loader.
- Parameters:
repo_url β Git repository URL (GitHub or GitLab).
file_path β Path to the configuration file within the repository.
branch β Git branch to read from. Defaults to
"main".token β Access token for private repositories. Falls back to the
GIT_TOKENenvironment variable.
- load() Dict[str, Any][source]ο
Load configuration from a Git repository.
Converts the repository URL to a raw-content URL based on the hosting provider (GitHub or GitLab), then fetches the file via HTTPLoader.
- Returns:
Dictionary containing the loaded configuration.
- Raises:
ValueError β If the Git provider is not supported (neither GitHub nor GitLab).
ConfigLoadError β If the HTTP request or parsing fails.
Example
>>> loader = GitLoader("https://github.com/org/repo", "config.yaml") >>> config = loader.load()
- class confii.loaders.HTTPLoader(url: str, timeout: int = 30, headers: Dict[str, str] | None = None, auth: Tuple[str, str] | None = None)[source]ο
Bases:
RemoteLoaderLoad configuration from HTTP/HTTPS endpoints.
HTTPLoader fetches configuration files from any HTTP or HTTPS URL. The response content-type header is used to determine the configuration format; if the content-type is ambiguous, the URLβs file extension is used as a fallback. Supports JSON, YAML, and TOML formats.
- urlο
The HTTP/HTTPS URL of the configuration resource.
- timeoutο
Request timeout in seconds.
- headersο
HTTP headers sent with the request.
- authο
Optional basic-auth credentials as a
(username, password)tuple.
- configο
The loaded configuration dictionary.
Example
>>> from confii.loaders.remote_loader import HTTPLoader >>> loader = HTTPLoader( ... "https://config-server.example.com/app/config.yaml", ... headers={"X-API-Key": "secret"}, ... timeout=10, ... ) >>> config_dict = loader.load() >>> print(config_dict["database"]["host"])
Note
Requires the
requestspackage. Install it with:pip install requests
- __init__(url: str, timeout: int = 30, headers: Dict[str, str] | None = None, auth: Tuple[str, str] | None = None)[source]ο
Initialize HTTP loader.
- Parameters:
url β HTTP/HTTPS URL to fetch configuration from.
timeout β Request timeout in seconds. Defaults to 30.
headers β Optional HTTP headers (e.g., API keys).
auth β Optional
(username, password)tuple for HTTP Basic Auth.
- Raises:
ImportError β If the
requestspackage is not installed.
- load() Dict[str, Any][source]ο
Load configuration from an HTTP/HTTPS endpoint.
Sends a GET request to the configured URL, detects the response format from the content-type header or URL extension, and parses the content into a dictionary.
- Returns:
Dictionary containing the loaded configuration.
- Raises:
ConfigLoadError β If the HTTP request fails or the response content cannot be parsed.
Example
>>> loader = HTTPLoader("https://example.com/config.json") >>> config = loader.load()
- class confii.loaders.IBMCloudObjectStorageLoader(bucket_name: str, object_key: str, api_key: str | None = None, service_instance_id: str | None = None, endpoint_url: str | None = None, region: str = 'us-south')[source]ο
Bases:
RemoteLoaderLoad configuration from IBM Cloud Object Storage.
IBMCloudObjectStorageLoader fetches configuration files stored in IBM Cloud Object Storage (COS) buckets using the S3-compatible API provided by the
ibm-cos-sdkpackage. Authentication uses IBM IAM OAuth via an API key and service instance ID.- urlο
Synthetic URI in the form
ibmcos://<bucket>/<key>.
- bucket_nameο
The IBM COS bucket name.
- object_keyο
The object key (path) within the bucket.
- api_keyο
IBM Cloud API key.
- service_instance_idο
IBM COS service instance ID.
- regionο
IBM Cloud region (e.g.,
"us-south").
- endpoint_urlο
The S3-compatible endpoint URL.
- configο
The loaded configuration dictionary.
Example
>>> from confii.loaders.remote_loader import IBMCloudObjectStorageLoader >>> loader = IBMCloudObjectStorageLoader( ... bucket_name="my-config-bucket", ... object_key="app/config.yaml", ... api_key="ibm-api-key", ... service_instance_id="crn:v1:...", ... ) >>> config_dict = loader.load()
Note
Requires the
ibm-cos-sdkpackage. Install it with:pip install ibm-cos-sdk
Environment variable fallbacks:
IBM_API_KEY,IBM_SERVICE_INSTANCE_ID.- __init__(bucket_name: str, object_key: str, api_key: str | None = None, service_instance_id: str | None = None, endpoint_url: str | None = None, region: str = 'us-south')[source]ο
Initialize IBM Cloud Object Storage loader.
- Parameters:
bucket_name β IBM COS bucket name.
object_key β Key (path) of the object to load.
api_key β IBM Cloud API key. Falls back to the
IBM_API_KEYenvironment variable.service_instance_id β IBM COS service instance ID. Falls back to the
IBM_SERVICE_INSTANCE_IDenvironment variable.endpoint_url β Custom S3-compatible endpoint URL. Defaults to the public endpoint for the given region.
region β IBM Cloud region. Defaults to
"us-south".
- load() Dict[str, Any][source]ο
Load configuration from IBM Cloud Object Storage.
Downloads the object content and parses it based on the object keyβs file extension.
- Returns:
Dictionary containing the loaded configuration.
- Raises:
ImportError β If
ibm-cos-sdkis not installed.ConfigLoadError β If the IBM COS request fails or the content cannot be parsed.
Example
>>> loader = IBMCloudObjectStorageLoader("my-bucket", "config.yaml") >>> config = loader.load()
- class confii.loaders.IniLoader(source: str)[source]ο
Bases:
LoaderLoader for INI configuration files.
IniLoader loads configuration data from INI (.ini or .cfg) files using Pythonβs
configparser.RawConfigParser. The raw parser is used to avoid interpolation of%characters that may appear in configuration values such as connection strings or format patterns.Each INI section becomes a top-level key in the resulting dictionary, with the sectionβs key-value pairs nested underneath. Scalar values are automatically coerced to appropriate Python types (int, float, bool, None) via
parse_scalar_value.- sourceο
Path to the INI configuration file.
- configο
Loaded configuration dictionary.
Example
>>> from confii.loaders import IniLoader >>> from confii import Config >>> >>> loader = IniLoader("database.ini") >>> config = Config(loaders=[loader]) >>> print(config.database.host)
Note
Missing files are handled gracefully and return None instead of raising an exception. This allows for optional configuration files. The
DEFAULTsection in INI files is not included as a separate key; its values are inherited by other sections per standard configparser behavior.- __init__(source: str)[source]ο
Initialize the INI loader.
- Parameters:
source β Path to the INI file.
- load() Dict[str, Any] | None[source]ο
Load configuration from an INI file.
Reads the INI file, parses each section into a nested dictionary, and coerces scalar values to their appropriate Python types.
- Returns:
Dictionary containing the loaded configuration keyed by section name, or None if the file does not exist or is unreadable.
- Raises:
configparser.MissingSectionHeaderError β If the file is not valid INI format (missing section headers).
Example
>>> loader = IniLoader("app.ini") >>> config_dict = loader.load() >>> if config_dict: ... print(config_dict["database"]["host"])
- class confii.loaders.JsonLoader(source: str)[source]ο
Bases:
LoaderLoader for JSON configuration files.
JsonLoader loads configuration data from JSON (.json) files. It uses Pythonβs built-in json module to parse the configuration.
- sourceο
Path to the JSON configuration file
- configο
Loaded configuration dictionary
Example
>>> from confii.loaders import JsonLoader >>> from confii import Config >>> >>> loader = JsonLoader("config.json") >>> config = Config(loaders=[loader]) >>> print(config.database.host)
Note
Missing files are handled gracefully and return None instead of raising an exception. This allows for optional configuration files.
- load() Dict[str, Any] | None[source]ο
Load configuration from JSON file.
This method reads the JSON file specified in the source attribute, parses it using Pythonβs json.loads, and returns the configuration as a dictionary.
- Returns:
Dictionary containing the loaded configuration, or None if the file doesnβt exist (allows for optional configuration files).
- Raises:
ConfigFormatError β If the JSON file contains invalid syntax or cannot be parsed.
ConfigLoadError β If thereβs an error reading the file (other than file not found).
Example
>>> loader = JsonLoader("config.json") >>> config_dict = loader.load() >>> if config_dict: ... print(f"Loaded {len(config_dict)} keys")
- class confii.loaders.Loader(source: str)[source]ο
Bases:
ABCAbstract base class for configuration loaders.
All configuration loaders must inherit from this class and implement the load() method. This provides a consistent interface for loading configuration from various sources (files, remote URLs, environment, etc.).
- sourceο
The source identifier (file path, URL, prefix, etc.)
- configο
The loaded configuration dictionary (may be empty if not loaded yet)
- __init__(source: str) None[source]ο
Initialize the loader with a source.
- Parameters:
source β The source identifier (file path, URL, prefix, etc.)
- abstractmethod load() Dict[str, Any] | None[source]ο
Load configuration from the source.
This method must be implemented by subclasses to load configuration from their specific source type.
- Returns:
Dictionary containing the loaded configuration, or None if the source doesnβt exist or couldnβt be loaded (depending on loader behavior).
- Raises:
ConfigLoadError β If loading fails due to an error (not just missing source)
ConfigFormatError β If the configuration format is invalid
- class confii.loaders.RemoteLoader(url: str, timeout: int = 30, headers: Dict[str, str] | None = None)[source]ο
Bases:
objectBase class for remote configuration loading.
RemoteLoader provides the shared interface and common attributes for all remote configuration loaders. Subclasses implement the
load()method to fetch configuration from specific remote backends (HTTP, S3, Azure Blob Storage, GCP Storage, Git repositories, IBM COS).The loaded content is automatically parsed into a Python dictionary based on the file extension or content-type header using the
format_parserutility.- urlο
The remote URL or URI identifying the configuration source.
- timeoutο
Request timeout in seconds.
- headersο
HTTP headers sent with the request (where applicable).
- sourceο
Alias for
url, used for source tracking.
- configο
The loaded configuration dictionary (populated after
load()).
Example
>>> # RemoteLoader is abstract; use a concrete subclass: >>> loader = HTTPLoader("https://example.com/config.json") >>> config_dict = loader.load()
- class confii.loaders.S3Loader(s3_url: str, aws_access_key: str | None = None, aws_secret_key: str | None = None, region: str = 'us-east-1')[source]ο
Bases:
RemoteLoaderLoad configuration from AWS S3.
S3Loader fetches configuration files stored in Amazon S3 buckets. Authentication can be provided explicitly via access keys, or implicitly through IAM roles, environment variables (
AWS_ACCESS_KEY_ID,AWS_SECRET_ACCESS_KEY), or the default boto3 credential chain.- urlο
The original
s3://URL.
- bucketο
The S3 bucket name (parsed from the URL).
- keyο
The object key / path within the bucket.
- aws_access_keyο
AWS access key ID (may be None for IAM).
- aws_secret_keyο
AWS secret access key (may be None for IAM).
- regionο
AWS region name.
- configο
The loaded configuration dictionary.
Example
>>> from confii.loaders.remote_loader import S3Loader >>> loader = S3Loader("s3://my-bucket/configs/app.yaml", region="eu-west-1") >>> config_dict = loader.load()
Note
Requires the
boto3package. Install it with:pip install boto3
- __init__(s3_url: str, aws_access_key: str | None = None, aws_secret_key: str | None = None, region: str = 'us-east-1')[source]ο
Initialize S3 loader.
- Parameters:
s3_url β S3 URL in the form
s3://bucket/path/to/config.json.aws_access_key β AWS access key ID. Falls back to the
AWS_ACCESS_KEY_IDenvironment variable or IAM role.aws_secret_key β AWS secret access key. Falls back to the
AWS_SECRET_ACCESS_KEYenvironment variable or IAM role.region β AWS region name. Defaults to
"us-east-1".
- Raises:
ValueError β If the URL scheme is not
s3.
- load() Dict[str, Any][source]ο
Load configuration from an AWS S3 bucket.
Downloads the object specified by
bucketandkey, then parses the content based on the file extension of the key.- Returns:
Dictionary containing the loaded configuration.
- Raises:
ImportError β If
boto3is not installed.ConfigLoadError β If the S3 request fails or the content cannot be parsed.
Example
>>> loader = S3Loader("s3://my-bucket/config.yaml") >>> config = loader.load()
- class confii.loaders.SSMLoader(path_prefix: str, decrypt: bool = True, aws_region: str | None = None, aws_access_key_id: str | None = None, aws_secret_access_key: str | None = None)[source]ο
Bases:
LoaderLoad configuration from AWS Systems Manager Parameter Store.
SSMLoader fetches parameters stored under a given path prefix in AWS SSM Parameter Store and converts them into a nested Python dictionary. For example, parameters stored at
/myapp/production/database/hostand/myapp/production/database/portwith apath_prefixof/myapp/production/produce:{"database": {"host": "db.example.com", "port": 5432}}
Authentication can be provided explicitly via constructor arguments, or implicitly through IAM roles, environment variables (
AWS_ACCESS_KEY_ID,AWS_SECRET_ACCESS_KEY,AWS_DEFAULT_REGION), or the default boto3 credential chain.String values are automatically coerced to appropriate Python types (int, float, bool) via
parse_scalar_value.- sourceο
The SSM path prefix used as the configuration source.
- path_prefixο
The SSM path prefix to fetch parameters from.
- decryptο
Whether to decrypt SecureString parameters.
- aws_regionο
AWS region name.
- aws_access_key_idο
AWS access key ID (may be None for IAM).
- aws_secret_access_keyο
AWS secret access key (may be None for IAM).
- configο
The loaded configuration dictionary.
Example
>>> from confii.loaders.ssm_loader import SSMLoader >>> loader = SSMLoader( ... path_prefix="/myapp/production/", ... decrypt=True, ... aws_region="us-west-2", ... ) >>> config_dict = loader.load() >>> print(config_dict["database"]["host"])
Note
Requires the
boto3package. Install it with:pip install boto3
- __init__(path_prefix: str, decrypt: bool = True, aws_region: str | None = None, aws_access_key_id: str | None = None, aws_secret_access_key: str | None = None) None[source]ο
Initialize SSM Parameter Store loader.
- Parameters:
path_prefix β SSM path prefix to fetch parameters from (e.g.,
/myapp/production/). A trailing slash is added automatically if not present.decrypt β Whether to decrypt SecureString parameters. Defaults to
True.aws_region β AWS region name. Falls back to the
AWS_DEFAULT_REGIONenvironment variable, then"us-east-1".aws_access_key_id β AWS access key ID. Falls back to the
AWS_ACCESS_KEY_IDenvironment variable or IAM role.aws_secret_access_key β AWS secret access key. Falls back to the
AWS_SECRET_ACCESS_KEYenvironment variable or IAM role.
- load() Dict[str, Any] | None[source]ο
Load configuration from AWS SSM Parameter Store.
Fetches all parameters under
path_prefixusing the SSMget_parameters_by_pathAPI with automatic pagination. Each parameterβs path (relative to the prefix) is split into nested dictionary keys, and string values are coerced to native Python types where possible.- Returns:
Dictionary containing the loaded configuration, or
Noneif no parameters were found under the prefix.- Raises:
ImportError β If
boto3is not installed.ConfigLoadError β If the SSM API call fails.
Example
>>> loader = SSMLoader("/myapp/production/") >>> config = loader.load()
- class confii.loaders.TomlLoader(source: str)[source]ο
Bases:
LoaderLoader for TOML configuration files.
TomlLoader loads configuration data from TOML (.toml) files. It uses the toml library to parse the configuration.
- sourceο
Path to the TOML configuration file
- configο
Loaded configuration dictionary
Example
>>> from confii.loaders import TomlLoader >>> from confii import Config >>> >>> loader = TomlLoader("config.toml") >>> config = Config(loaders=[loader]) >>> print(config.database.host)
Note
Missing files raise ConfigLoadError. Ensure the file exists or handle the exception appropriately.
- load() Dict[str, Any] | None[source]ο
Load configuration from TOML file.
This method reads the TOML file specified in the source attribute, parses it using the toml library, and returns the configuration as a dictionary.
- Returns:
Dictionary containing the loaded configuration, or None if the file doesnβt exist (allows for optional configuration files).
- Raises:
ConfigFormatError β If the TOML file contains invalid syntax or cannot be parsed.
ConfigLoadError β If thereβs an error reading the file (other than file not found).
Example
>>> loader = TomlLoader("config.toml") >>> config_dict = loader.load() >>> if config_dict: ... print(f"Loaded {len(config_dict)} keys")
- class confii.loaders.YamlLoader(source: str)[source]ο
Bases:
LoaderLoader for YAML configuration files.
YamlLoader loads configuration data from YAML (.yaml or .yml) files. It uses PyYAMLβs safe_load to parse the configuration, which prevents arbitrary code execution.
- sourceο
Path to the YAML configuration file
- configο
Loaded configuration dictionary
Example
>>> from confii.loaders import YamlLoader >>> from confii import Config >>> >>> loader = YamlLoader("config.yaml") >>> config = Config(loaders=[loader]) >>> print(config.database.host)
Note
Missing files are handled gracefully and return None instead of raising an exception. This allows for optional configuration files.
- load() Dict[str, Any] | None[source]ο
Load configuration from YAML file.
This method reads the YAML file specified in the source attribute, parses it using PyYAMLβs safe_load, and returns the configuration as a dictionary.
- Returns:
Dictionary containing the loaded configuration, or None if the file doesnβt exist (allows for optional configuration files).
- Raises:
ConfigFormatError β If the YAML file contains invalid syntax or cannot be parsed.
ConfigLoadError β If thereβs an error reading the file (other than file not found).
Example
>>> loader = YamlLoader("config.yaml") >>> config_dict = loader.load() >>> if config_dict: ... print(f"Loaded {len(config_dict)} keys")
Base Loaderο
- class confii.loaders.loader.Loader(source: str)[source]ο
Bases:
ABCAbstract base class for configuration loaders.
All configuration loaders must inherit from this class and implement the load() method. This provides a consistent interface for loading configuration from various sources (files, remote URLs, environment, etc.).
- sourceο
The source identifier (file path, URL, prefix, etc.)
- configο
The loaded configuration dictionary (may be empty if not loaded yet)
- __init__(source: str) None[source]ο
Initialize the loader with a source.
- Parameters:
source β The source identifier (file path, URL, prefix, etc.)
- abstractmethod load() Dict[str, Any] | None[source]ο
Load configuration from the source.
This method must be implemented by subclasses to load configuration from their specific source type.
- Returns:
Dictionary containing the loaded configuration, or None if the source doesnβt exist or couldnβt be loaded (depending on loader behavior).
- Raises:
ConfigLoadError β If loading fails due to an error (not just missing source)
ConfigFormatError β If the configuration format is invalid
YAML Loaderο
YAML configuration loader for Confii.
This module provides the YamlLoader class for loading configuration from YAML files.
- class confii.loaders.yaml_loader.YamlLoader(source: str)[source]ο
Bases:
LoaderLoader for YAML configuration files.
YamlLoader loads configuration data from YAML (.yaml or .yml) files. It uses PyYAMLβs safe_load to parse the configuration, which prevents arbitrary code execution.
- sourceο
Path to the YAML configuration file
- configο
Loaded configuration dictionary
Example
>>> from confii.loaders import YamlLoader >>> from confii import Config >>> >>> loader = YamlLoader("config.yaml") >>> config = Config(loaders=[loader]) >>> print(config.database.host)
Note
Missing files are handled gracefully and return None instead of raising an exception. This allows for optional configuration files.
- load() Dict[str, Any] | None[source]ο
Load configuration from YAML file.
This method reads the YAML file specified in the source attribute, parses it using PyYAMLβs safe_load, and returns the configuration as a dictionary.
- Returns:
Dictionary containing the loaded configuration, or None if the file doesnβt exist (allows for optional configuration files).
- Raises:
ConfigFormatError β If the YAML file contains invalid syntax or cannot be parsed.
ConfigLoadError β If thereβs an error reading the file (other than file not found).
Example
>>> loader = YamlLoader("config.yaml") >>> config_dict = loader.load() >>> if config_dict: ... print(f"Loaded {len(config_dict)} keys")
JSON Loaderο
JSON configuration loader for Confii.
This module provides the JsonLoader class for loading configuration from JSON files.
- class confii.loaders.json_loader.JsonLoader(source: str)[source]ο
Bases:
LoaderLoader for JSON configuration files.
JsonLoader loads configuration data from JSON (.json) files. It uses Pythonβs built-in json module to parse the configuration.
- sourceο
Path to the JSON configuration file
- configο
Loaded configuration dictionary
Example
>>> from confii.loaders import JsonLoader >>> from confii import Config >>> >>> loader = JsonLoader("config.json") >>> config = Config(loaders=[loader]) >>> print(config.database.host)
Note
Missing files are handled gracefully and return None instead of raising an exception. This allows for optional configuration files.
- load() Dict[str, Any] | None[source]ο
Load configuration from JSON file.
This method reads the JSON file specified in the source attribute, parses it using Pythonβs json.loads, and returns the configuration as a dictionary.
- Returns:
Dictionary containing the loaded configuration, or None if the file doesnβt exist (allows for optional configuration files).
- Raises:
ConfigFormatError β If the JSON file contains invalid syntax or cannot be parsed.
ConfigLoadError β If thereβs an error reading the file (other than file not found).
Example
>>> loader = JsonLoader("config.json") >>> config_dict = loader.load() >>> if config_dict: ... print(f"Loaded {len(config_dict)} keys")
TOML Loaderο
TOML configuration loader for Confii.
This module provides the TomlLoader class for loading configuration from TOML files.
- class confii.loaders.toml_loader.TomlLoader(source: str)[source]ο
Bases:
LoaderLoader for TOML configuration files.
TomlLoader loads configuration data from TOML (.toml) files. It uses the toml library to parse the configuration.
- sourceο
Path to the TOML configuration file
- configο
Loaded configuration dictionary
Example
>>> from confii.loaders import TomlLoader >>> from confii import Config >>> >>> loader = TomlLoader("config.toml") >>> config = Config(loaders=[loader]) >>> print(config.database.host)
Note
Missing files raise ConfigLoadError. Ensure the file exists or handle the exception appropriately.
- load() Dict[str, Any] | None[source]ο
Load configuration from TOML file.
This method reads the TOML file specified in the source attribute, parses it using the toml library, and returns the configuration as a dictionary.
- Returns:
Dictionary containing the loaded configuration, or None if the file doesnβt exist (allows for optional configuration files).
- Raises:
ConfigFormatError β If the TOML file contains invalid syntax or cannot be parsed.
ConfigLoadError β If thereβs an error reading the file (other than file not found).
Example
>>> loader = TomlLoader("config.toml") >>> config_dict = loader.load() >>> if config_dict: ... print(f"Loaded {len(config_dict)} keys")
Environment Loaderο
- class confii.loaders.environment_loader.EnvironmentLoader(prefix: str, separator: str = '__')[source]ο
Bases:
LoaderLoader for environment variables with prefix support.
This loader reads environment variables with a specific prefix and converts them into a nested configuration structure.
Remote Loadersο
Remote configuration loaders for Confii.
- class confii.loaders.remote_loader.AzureBlobLoader(container_url: str, blob_name: str, account_name: str | None = None, account_key: str | None = None, sas_token: str | None = None, connection_string: str | None = None)[source]ο
Bases:
RemoteLoaderLoad configuration from Azure Blob Storage.
AzureBlobLoader downloads configuration files from Azure Blob Storage containers. It supports multiple authentication methods: connection strings, account key, SAS tokens, and Azure Managed Identity via
DefaultAzureCredential.- urlο
Synthetic URI in the form
azure://<container>/<blob>.
- container_urlο
The Azure container URL or container name.
- blob_nameο
The blob (file) name within the container.
- account_nameο
Azure Storage account name.
- account_keyο
Azure Storage account key (may be None).
- sas_tokenο
Shared Access Signature token (may be None).
- connection_stringο
Full Azure Storage connection string (may be None).
- configο
The loaded configuration dictionary.
Example
>>> from confii.loaders.remote_loader import AzureBlobLoader >>> loader = AzureBlobLoader( ... container_url="my-container", ... blob_name="config/app.yaml", ... account_name="mystorageaccount", ... account_key="base64key==", ... ) >>> config_dict = loader.load()
Note
Requires the
azure-storage-blobpackage. Install it with:pip install azure-storage-blob
Environment variable fallbacks:
AZURE_STORAGE_ACCOUNT,AZURE_STORAGE_KEY,AZURE_SAS_TOKEN,AZURE_STORAGE_CONNECTION_STRING.- __init__(container_url: str, blob_name: str, account_name: str | None = None, account_key: str | None = None, sas_token: str | None = None, connection_string: str | None = None)[source]ο
Initialize Azure Blob loader.
- Parameters:
container_url β Azure container URL or plain container name.
blob_name β Name of the blob (file) to load.
account_name β Azure storage account name. Falls back to
AZURE_STORAGE_ACCOUNTenvironment variable.account_key β Azure storage account key. Falls back to
AZURE_STORAGE_KEYenvironment variable.sas_token β Shared Access Signature token. Falls back to
AZURE_SAS_TOKENenvironment variable.connection_string β Full connection string (alternative to individual credentials). Falls back to
AZURE_STORAGE_CONNECTION_STRINGenvironment variable.
- load() Dict[str, Any][source]ο
Load configuration from Azure Blob Storage.
Downloads the blob content and parses it based on the blob nameβs file extension (e.g.,
.yaml,.json,.toml).- Returns:
Dictionary containing the loaded configuration.
- Raises:
ImportError β If
azure-storage-blobis not installed.ValueError β If
account_nameis required but not provided.ConfigLoadError β If the Azure request fails or the content cannot be parsed.
Example
>>> loader = AzureBlobLoader( ... "my-container", "config.yaml", account_name="myaccount" ... ) >>> config = loader.load()
- class confii.loaders.remote_loader.GCPStorageLoader(bucket_name: str, blob_name: str, project_id: str | None = None, credentials_path: str | None = None)[source]ο
Bases:
RemoteLoaderLoad configuration from Google Cloud Storage.
GCPStorageLoader downloads configuration files from GCS buckets. Authentication is handled via explicit service account credentials or Application Default Credentials (ADC).
- urlο
Synthetic URI in the form
gs://<bucket>/<blob>.
- bucket_nameο
The GCS bucket name.
- blob_nameο
The blob (file) name within the bucket.
- project_idο
GCP project ID.
- credentials_pathο
Path to a service account JSON key file.
- configο
The loaded configuration dictionary.
Example
>>> from confii.loaders.remote_loader import GCPStorageLoader >>> loader = GCPStorageLoader( ... bucket_name="my-config-bucket", ... blob_name="services/app.yaml", ... project_id="my-gcp-project", ... ) >>> config_dict = loader.load()
Note
Requires the
google-cloud-storagepackage. Install it with:pip install google-cloud-storage
Environment variable fallbacks:
GCP_PROJECT_ID,GOOGLE_APPLICATION_CREDENTIALS.- __init__(bucket_name: str, blob_name: str, project_id: str | None = None, credentials_path: str | None = None)[source]ο
Initialize GCP Storage loader.
- Parameters:
bucket_name β GCS bucket name.
blob_name β Name of the blob (file) to load.
project_id β GCP project ID. Falls back to the
GCP_PROJECT_IDenvironment variable.credentials_path β Path to a service account JSON key file. Falls back to the
GOOGLE_APPLICATION_CREDENTIALSenvironment variable.
- load() Dict[str, Any][source]ο
Load configuration from Google Cloud Storage.
Downloads the blob content as text and parses it based on the blob nameβs file extension.
- Returns:
Dictionary containing the loaded configuration.
- Raises:
ImportError β If
google-cloud-storageis not installed.ConfigLoadError β If the GCS request fails or the content cannot be parsed.
Example
>>> loader = GCPStorageLoader("my-bucket", "config.yaml") >>> config = loader.load()
- class confii.loaders.remote_loader.GitLoader(repo_url: str, file_path: str, branch: str = 'main', token: str | None = None)[source]ο
Bases:
RemoteLoaderLoad configuration from a Git repository hosted on GitHub or GitLab.
GitLoader constructs a raw-content URL for the specified file and branch, then delegates to HTTPLoader to fetch and parse it. Private repositories are supported via personal access tokens.
- urlο
The Git repository URL (e.g.,
https://github.com/org/repo).
- file_pathο
Path to the configuration file within the repository.
- branchο
The Git branch to read from.
- tokenο
Access token for private repositories (may be None).
- configο
The loaded configuration dictionary.
Example
>>> from confii.loaders.remote_loader import GitLoader >>> loader = GitLoader( ... repo_url="https://github.com/myorg/myrepo", ... file_path="config/production.yaml", ... branch="main", ... token="ghp_xxxxxxxxxxxx", ... ) >>> config_dict = loader.load()
Note
Currently supports GitHub and GitLab. The
GIT_TOKENenvironment variable is used as a fallback whentokenis not provided.- __init__(repo_url: str, file_path: str, branch: str = 'main', token: str | None = None)[source]ο
Initialize Git loader.
- Parameters:
repo_url β Git repository URL (GitHub or GitLab).
file_path β Path to the configuration file within the repository.
branch β Git branch to read from. Defaults to
"main".token β Access token for private repositories. Falls back to the
GIT_TOKENenvironment variable.
- load() Dict[str, Any][source]ο
Load configuration from a Git repository.
Converts the repository URL to a raw-content URL based on the hosting provider (GitHub or GitLab), then fetches the file via HTTPLoader.
- Returns:
Dictionary containing the loaded configuration.
- Raises:
ValueError β If the Git provider is not supported (neither GitHub nor GitLab).
ConfigLoadError β If the HTTP request or parsing fails.
Example
>>> loader = GitLoader("https://github.com/org/repo", "config.yaml") >>> config = loader.load()
- class confii.loaders.remote_loader.HTTPLoader(url: str, timeout: int = 30, headers: Dict[str, str] | None = None, auth: Tuple[str, str] | None = None)[source]ο
Bases:
RemoteLoaderLoad configuration from HTTP/HTTPS endpoints.
HTTPLoader fetches configuration files from any HTTP or HTTPS URL. The response content-type header is used to determine the configuration format; if the content-type is ambiguous, the URLβs file extension is used as a fallback. Supports JSON, YAML, and TOML formats.
- urlο
The HTTP/HTTPS URL of the configuration resource.
- timeoutο
Request timeout in seconds.
- headersο
HTTP headers sent with the request.
- authο
Optional basic-auth credentials as a
(username, password)tuple.
- configο
The loaded configuration dictionary.
Example
>>> from confii.loaders.remote_loader import HTTPLoader >>> loader = HTTPLoader( ... "https://config-server.example.com/app/config.yaml", ... headers={"X-API-Key": "secret"}, ... timeout=10, ... ) >>> config_dict = loader.load() >>> print(config_dict["database"]["host"])
Note
Requires the
requestspackage. Install it with:pip install requests
- __init__(url: str, timeout: int = 30, headers: Dict[str, str] | None = None, auth: Tuple[str, str] | None = None)[source]ο
Initialize HTTP loader.
- Parameters:
url β HTTP/HTTPS URL to fetch configuration from.
timeout β Request timeout in seconds. Defaults to 30.
headers β Optional HTTP headers (e.g., API keys).
auth β Optional
(username, password)tuple for HTTP Basic Auth.
- Raises:
ImportError β If the
requestspackage is not installed.
- load() Dict[str, Any][source]ο
Load configuration from an HTTP/HTTPS endpoint.
Sends a GET request to the configured URL, detects the response format from the content-type header or URL extension, and parses the content into a dictionary.
- Returns:
Dictionary containing the loaded configuration.
- Raises:
ConfigLoadError β If the HTTP request fails or the response content cannot be parsed.
Example
>>> loader = HTTPLoader("https://example.com/config.json") >>> config = loader.load()
- class confii.loaders.remote_loader.IBMCloudObjectStorageLoader(bucket_name: str, object_key: str, api_key: str | None = None, service_instance_id: str | None = None, endpoint_url: str | None = None, region: str = 'us-south')[source]ο
Bases:
RemoteLoaderLoad configuration from IBM Cloud Object Storage.
IBMCloudObjectStorageLoader fetches configuration files stored in IBM Cloud Object Storage (COS) buckets using the S3-compatible API provided by the
ibm-cos-sdkpackage. Authentication uses IBM IAM OAuth via an API key and service instance ID.- urlο
Synthetic URI in the form
ibmcos://<bucket>/<key>.
- bucket_nameο
The IBM COS bucket name.
- object_keyο
The object key (path) within the bucket.
- api_keyο
IBM Cloud API key.
- service_instance_idο
IBM COS service instance ID.
- regionο
IBM Cloud region (e.g.,
"us-south").
- endpoint_urlο
The S3-compatible endpoint URL.
- configο
The loaded configuration dictionary.
Example
>>> from confii.loaders.remote_loader import IBMCloudObjectStorageLoader >>> loader = IBMCloudObjectStorageLoader( ... bucket_name="my-config-bucket", ... object_key="app/config.yaml", ... api_key="ibm-api-key", ... service_instance_id="crn:v1:...", ... ) >>> config_dict = loader.load()
Note
Requires the
ibm-cos-sdkpackage. Install it with:pip install ibm-cos-sdk
Environment variable fallbacks:
IBM_API_KEY,IBM_SERVICE_INSTANCE_ID.- __init__(bucket_name: str, object_key: str, api_key: str | None = None, service_instance_id: str | None = None, endpoint_url: str | None = None, region: str = 'us-south')[source]ο
Initialize IBM Cloud Object Storage loader.
- Parameters:
bucket_name β IBM COS bucket name.
object_key β Key (path) of the object to load.
api_key β IBM Cloud API key. Falls back to the
IBM_API_KEYenvironment variable.service_instance_id β IBM COS service instance ID. Falls back to the
IBM_SERVICE_INSTANCE_IDenvironment variable.endpoint_url β Custom S3-compatible endpoint URL. Defaults to the public endpoint for the given region.
region β IBM Cloud region. Defaults to
"us-south".
- load() Dict[str, Any][source]ο
Load configuration from IBM Cloud Object Storage.
Downloads the object content and parses it based on the object keyβs file extension.
- Returns:
Dictionary containing the loaded configuration.
- Raises:
ImportError β If
ibm-cos-sdkis not installed.ConfigLoadError β If the IBM COS request fails or the content cannot be parsed.
Example
>>> loader = IBMCloudObjectStorageLoader("my-bucket", "config.yaml") >>> config = loader.load()
- class confii.loaders.remote_loader.RemoteLoader(url: str, timeout: int = 30, headers: Dict[str, str] | None = None)[source]ο
Bases:
objectBase class for remote configuration loading.
RemoteLoader provides the shared interface and common attributes for all remote configuration loaders. Subclasses implement the
load()method to fetch configuration from specific remote backends (HTTP, S3, Azure Blob Storage, GCP Storage, Git repositories, IBM COS).The loaded content is automatically parsed into a Python dictionary based on the file extension or content-type header using the
format_parserutility.- urlο
The remote URL or URI identifying the configuration source.
- timeoutο
Request timeout in seconds.
- headersο
HTTP headers sent with the request (where applicable).
- sourceο
Alias for
url, used for source tracking.
- configο
The loaded configuration dictionary (populated after
load()).
Example
>>> # RemoteLoader is abstract; use a concrete subclass: >>> loader = HTTPLoader("https://example.com/config.json") >>> config_dict = loader.load()
- class confii.loaders.remote_loader.S3Loader(s3_url: str, aws_access_key: str | None = None, aws_secret_key: str | None = None, region: str = 'us-east-1')[source]ο
Bases:
RemoteLoaderLoad configuration from AWS S3.
S3Loader fetches configuration files stored in Amazon S3 buckets. Authentication can be provided explicitly via access keys, or implicitly through IAM roles, environment variables (
AWS_ACCESS_KEY_ID,AWS_SECRET_ACCESS_KEY), or the default boto3 credential chain.- urlο
The original
s3://URL.
- bucketο
The S3 bucket name (parsed from the URL).
- keyο
The object key / path within the bucket.
- aws_access_keyο
AWS access key ID (may be None for IAM).
- aws_secret_keyο
AWS secret access key (may be None for IAM).
- regionο
AWS region name.
- configο
The loaded configuration dictionary.
Example
>>> from confii.loaders.remote_loader import S3Loader >>> loader = S3Loader("s3://my-bucket/configs/app.yaml", region="eu-west-1") >>> config_dict = loader.load()
Note
Requires the
boto3package. Install it with:pip install boto3
- __init__(s3_url: str, aws_access_key: str | None = None, aws_secret_key: str | None = None, region: str = 'us-east-1')[source]ο
Initialize S3 loader.
- Parameters:
s3_url β S3 URL in the form
s3://bucket/path/to/config.json.aws_access_key β AWS access key ID. Falls back to the
AWS_ACCESS_KEY_IDenvironment variable or IAM role.aws_secret_key β AWS secret access key. Falls back to the
AWS_SECRET_ACCESS_KEYenvironment variable or IAM role.region β AWS region name. Defaults to
"us-east-1".
- Raises:
ValueError β If the URL scheme is not
s3.
- load() Dict[str, Any][source]ο
Load configuration from an AWS S3 bucket.
Downloads the object specified by
bucketandkey, then parses the content based on the file extension of the key.- Returns:
Dictionary containing the loaded configuration.
- Raises:
ImportError β If
boto3is not installed.ConfigLoadError β If the S3 request fails or the content cannot be parsed.
Example
>>> loader = S3Loader("s3://my-bucket/config.yaml") >>> config = loader.load()