A Python package for loading, validating, configurations from various sources.
Project description
pyconfig-loader
pyconfig-loader is a simple Python library for loading configurations from different sources such as environment variables and configuration files (JSON/YAML). It supports flexible configurations with features like default values, required keys, and filtering based on key prefixes.
Installation
To install the package, you can use pip:
pip install pyconfig-loader
Alternatively, you can install it from the source using poetry:
poetry install
Usage
1. Loading Configuration from Environment Variables
You can load configuration directly from environment variables, optionally filtering by key prefix, providing default values, or marking certain variables as required.
from pyconfig_loader import ConfigLoader
# Example of loading configuration from environment variables
loader = ConfigLoader("env")
# Load with required keys and default values
config = loader.load_from_env(
required_keys=["DB_USERNAME", "DB_PASSWORD"],
default_values={"DB_PASSWORD": "default_pass"},
key_prefix="DB_"
)
print(config)
This would output a dictionary like:
{
'DB_USERNAME': 'admin', # from env
'DB_PASSWORD': 'default_pass' # default value as it's missing in the environment
}
2. Loading Configuration from a File (JSON or YAML)
You can also load configuration from a file, supporting both JSON and YAML formats.
JSON Example:
{
"DB_USERNAME": "admin",
"DB_PASSWORD": "secret"
}
YAML Example:
DB_USERNAME: admin
DB_PASSWORD: secret
from pyconfig_loader import ConfigLoader
# Example of loading configuration from a JSON or YAML file
loader = ConfigLoader("path/to/config.json") # Or "path/to/config.yaml"
config = loader.load()
print(config)
3. Error Handling
By default, if required environment variables are missing, a ValueError will be raised. You can modify this behavior with the raise_on_missing argument.
config = loader.load_from_env(
required_keys=["DB_USERNAME", "DB_PASSWORD"],
raise_on_missing=False # Will not raise an error if keys are missing
)
4. Advanced Configuration Features
- Prefix Filtering: You can filter environment variables based on a prefix (e.g., load only variables starting with
DB_). - Default Values: Specify default values for missing environment variables.
- Required Keys: Define which environment variables are required to exist in the configuration, and optionally set default values for them.
config = loader.load_from_env(
required_keys=["API_KEY", "DB_PASSWORD"],
default_values={"DB_PASSWORD": "default_pass"},
key_prefix="APP_"
)
Development
To contribute to pyconfig-loader, you can clone the repository and use poetry to manage dependencies and run tests.
git clone https://github.com/yourusername/pyconfig-loader.git
cd pyconfig-loader
poetry install
poetry run pytest # To run tests
License
This project is licensed under the MIT License - see the LICENSE file for details.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file pyconf_ezload-0.1.1.tar.gz.
File metadata
- Download URL: pyconf_ezload-0.1.1.tar.gz
- Upload date:
- Size: 3.8 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.12.9
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
5c89c0fb514309bab7d5d242e94f16cf3baca9aca78413b32627e8d129ddb2a8
|
|
| MD5 |
a5d30d08c1f78194d4df51a5bbb61be8
|
|
| BLAKE2b-256 |
935e6c4df730f09910db4f5417fb37e57acaa9a7d6085a895d9fa7352205f694
|
Provenance
The following attestation bundles were made for pyconf_ezload-0.1.1.tar.gz:
Publisher:
python-publish.yml on KartikeyaMalimath/pyconf-ezload
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
pyconf_ezload-0.1.1.tar.gz -
Subject digest:
5c89c0fb514309bab7d5d242e94f16cf3baca9aca78413b32627e8d129ddb2a8 - Sigstore transparency entry: 189680909
- Sigstore integration time:
-
Permalink:
KartikeyaMalimath/pyconf-ezload@9a826d28aec9784e850c197bb5b2983c21875beb -
Branch / Tag:
refs/tags/pyconf-ezload-0.1.1 - Owner: https://github.com/KartikeyaMalimath
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
python-publish.yml@9a826d28aec9784e850c197bb5b2983c21875beb -
Trigger Event:
release
-
Statement type:
File details
Details for the file pyconf_ezload-0.1.1-py3-none-any.whl.
File metadata
- Download URL: pyconf_ezload-0.1.1-py3-none-any.whl
- Upload date:
- Size: 4.6 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.12.9
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
364c57afcf449240ca77adc58e1672f7279a5fb063a23f8ae34a04cebf500c3b
|
|
| MD5 |
c919111c38f7d818d0a187d5d94b5894
|
|
| BLAKE2b-256 |
8f0de6ff8cb62ff3ba2273baadb8c04b53a2b79fcf9547fbf8524075576b22a2
|
Provenance
The following attestation bundles were made for pyconf_ezload-0.1.1-py3-none-any.whl:
Publisher:
python-publish.yml on KartikeyaMalimath/pyconf-ezload
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
pyconf_ezload-0.1.1-py3-none-any.whl -
Subject digest:
364c57afcf449240ca77adc58e1672f7279a5fb063a23f8ae34a04cebf500c3b - Sigstore transparency entry: 189680911
- Sigstore integration time:
-
Permalink:
KartikeyaMalimath/pyconf-ezload@9a826d28aec9784e850c197bb5b2983c21875beb -
Branch / Tag:
refs/tags/pyconf-ezload-0.1.1 - Owner: https://github.com/KartikeyaMalimath
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
python-publish.yml@9a826d28aec9784e850c197bb5b2983c21875beb -
Trigger Event:
release
-
Statement type: