Add your description here
Project description
glean-config
A flexible, thread-safe Python configuration manager using TOML files with support for environment variables, base64 encoding, and fileless operation modes.
Features
- TOML-based configuration: Easy-to-read and write configuration files
- Singleton pattern: Ensures consistent configuration across your application
- Thread-safe: Built-in locking for concurrent access
- Environment variable integration: Auto-load environment variables by name or regex pattern
- Automatic base64 encoding: Prefix keys with
encoded_for automatic encoding/decoding - Fileless mode: Run without a config file for testing or ephemeral configurations
- Context manager support: Use with
withstatement for automatic saving
Installation
pip install glean-config
Or for development:
git clone <repository-url>
cd glean-config
pip install -e .
Quick Start
Basic Usage
from glean_config.config import Glean_config
# Load or create a config file
config = Glean_config.get_instance('config.toml')
# Set values
config['api_key'] = 'your-api-key'
config['debug'] = 'true'
# Get values
api_key = config['api_key']
# Save changes
config.save()
Using the CLI
# Create/view config with a specific file
python -m glean_config -f config.toml
# Add a key-value pair
python -m glean_config -f config.toml -a "database_url::postgresql://localhost/mydb"
# Show a specific key
python -m glean_config -f config.toml -k database_url
# Use environment variable for config file location
export GLEAN_CONFIG_FILE=/path/to/config.toml
python -m glean_config -k database_url
# Run in fileless mode (no file or env var needed)
python -m glean_config -a "temp_key::temp_value"
Configuration Modes
1. File-based Mode (Default)
config = Glean_config.get_instance('myconfig.toml')
Loads configuration from the specified TOML file. Creates the file if it doesn't exist.
2. Environment Variable Mode
# Set the environment variable
import os
os.environ['GLEAN_CONFIG_FILE'] = '/path/to/config.toml'
# No file argument needed
config = Glean_config.get_instance()
3. Fileless Mode
# No file, no environment variable
config = Glean_config.get_instance()
# Config exists only in memory
config['key'] = 'value'
Advanced Features
Automatic Base64 Encoding
Keys starting with encoded_ are automatically base64 encoded when set and decoded when retrieved:
config['encoded_password'] = 'my-secret-password'
# Stored as base64 in the file
password = config['encoded_password']
# Returns: 'my-secret-password' (decoded)
Environment Variable Integration
Configure your TOML file to automatically load environment variables:
config_name = 'myapp'
[environment]
# Regex pattern to match environment variables
env_rex = '^MYAPP_'
# Explicit list of environment variables to load
env = ['DATABASE_URL', 'SECRET_KEY']
[config_items]
# Your config items here
Now environment variables matching the pattern or in the list are automatically loaded:
import os
os.environ['MYAPP_DEBUG'] = 'true'
os.environ['DATABASE_URL'] = 'postgresql://localhost/db'
config = Glean_config.get_instance('config.toml')
print(config['MYAPP_DEBUG']) # 'true'
print(config['DATABASE_URL']) # 'postgresql://localhost/db'
Context Manager
with Glean_config.get_instance('config.toml') as config:
config['key'] = 'value'
config['another_key'] = 'another_value'
# Automatically saved on exit
Bulk Configuration
# Replace entire config
new_config = {
'api_key': 'new-key',
'timeout': '30',
'retries': '3'
}
config.set_config(new_config)
# Merge with existing config
config.set_config(new_config, merge=True)
Export Formats
# Export as TOML string
toml_str = config.get_toml()
# Export as JSON string
json_str = config.get_json()
TOML File Structure
config_name = 'my_application'
[environment]
# Regex pattern for environment variables to auto-load
env_rex = '^AP_'
# Explicit environment variables to load
env = ['DATABASE_URL', 'SECRET_KEY']
[config_items]
# Your application configuration
api_key = "your-api-key"
debug = "false"
timeout = "30"
encoded_password = "bXktc2VjcmV0LXBhc3N3b3Jk" # base64 encoded
API Reference
Glean_config.get_instance(toml_file=None)
Get or create the singleton config instance.
- toml_file (str, optional): Path to TOML file. If
None, usesGLEAN_CONFIG_FILEenv var or fileless mode. - Returns:
Glean_configinstance
Instance Methods
config[key] / config[key] = value
Get or set configuration values.
save(force=False)
Save configuration to file (no-op in fileless mode).
- force (bool): Save even if not modified
get_config()
Get the entire config_items dictionary.
- Returns: dict
set_config(config, merge=False)
Set the entire config_items dictionary.
- config (dict): New configuration
- merge (bool): Merge with existing config instead of replacing
get_toml()
Export configuration as TOML string.
- Returns: str
get_json()
Export configuration as JSON string.
- Returns: str
parameters()
Get list of top-level configuration keys.
- Returns: list[str]
close()
Explicitly save and close the configuration.
Testing
# Run all tests
pytest tests/
# Run with coverage
pytest tests/ --cov=glean_config
# Run specific test
pytest tests/test_config.py::TestGleanConfig::test_fileless_mode_no_args
Thread Safety
All operations are thread-safe using internal locks:
- Object-level lock for singleton instance creation and config updates
- File-level lock for concurrent file I/O operations
Error Handling
ConfigObjectError
Raised when trying to instantiate Glean_config directly instead of using get_instance().
from glean_config.config import Glean_config, ConfigObjectError
try:
config = Glean_config('config.toml') # Wrong!
except ConfigObjectError:
config = Glean_config.get_instance('config.toml') # Correct
KeyError
Raised when configuration structure is invalid (missing config_items key).
License
MIT
Contributing
Contributions are welcome! Please ensure:
- All tests pass:
pytest tests/ - Code follows PEP 8 style guidelines
- New features include tests
- Documentation is updated
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 glean_config-0.1.0.tar.gz.
File metadata
- Download URL: glean_config-0.1.0.tar.gz
- Upload date:
- Size: 5.1 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
e0827323d48ddc269dee8a08343f2e65a8ba34e506797d597795b9926c63b412
|
|
| MD5 |
14cf29e1cbe806e8623fe563dd46a734
|
|
| BLAKE2b-256 |
e84ffd6da9d110c03f58059861f978357d730f4500cdbc41107586c6c9441bbc
|
Provenance
The following attestation bundles were made for glean_config-0.1.0.tar.gz:
Publisher:
publish.yaml on Glean-llc/config
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
glean_config-0.1.0.tar.gz -
Subject digest:
e0827323d48ddc269dee8a08343f2e65a8ba34e506797d597795b9926c63b412 - Sigstore transparency entry: 660675382
- Sigstore integration time:
-
Permalink:
Glean-llc/config@ca7ee7fcdd4346d18ac462ac47d42013b1380f3f -
Branch / Tag:
refs/tags/0.1.0 - Owner: https://github.com/Glean-llc
-
Access:
private
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yaml@ca7ee7fcdd4346d18ac462ac47d42013b1380f3f -
Trigger Event:
release
-
Statement type:
File details
Details for the file glean_config-0.1.0-py3-none-any.whl.
File metadata
- Download URL: glean_config-0.1.0-py3-none-any.whl
- Upload date:
- Size: 6.4 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
cf43f7f14a131c847f307bccb6a2de9ef149881e0b2063e03462c561f09df3ff
|
|
| MD5 |
a859c5288edec833f826f831dd0cf93b
|
|
| BLAKE2b-256 |
afcf24113c7fbf499aa2239a4a76def611ae2d2f27721667a4b1fcf541855f43
|
Provenance
The following attestation bundles were made for glean_config-0.1.0-py3-none-any.whl:
Publisher:
publish.yaml on Glean-llc/config
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
glean_config-0.1.0-py3-none-any.whl -
Subject digest:
cf43f7f14a131c847f307bccb6a2de9ef149881e0b2063e03462c561f09df3ff - Sigstore transparency entry: 660675385
- Sigstore integration time:
-
Permalink:
Glean-llc/config@ca7ee7fcdd4346d18ac462ac47d42013b1380f3f -
Branch / Tag:
refs/tags/0.1.0 - Owner: https://github.com/Glean-llc
-
Access:
private
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yaml@ca7ee7fcdd4346d18ac462ac47d42013b1380f3f -
Trigger Event:
release
-
Statement type: