Configparser with environment variable and direct override
Project description
ConfigParser Override
This library provides a utility class ConfigParserOverride
that extends the
functionality of configparser.ConfigParser
to allow overriding configuration
values with environment variables and directly assigned key-value arguments.
NOTE: This library only depends on the Python standard library!
Features
- Read configuration from one or more files.
- Override configuration values with environment variables.
- Override configuration values with directly assigned arguments.
- Support for optional environment variable prefix.
Install
pip install configparser-override
Usage
Example of how to use ConfigParserOverride
:
Example config.ini
File
[DEFAULT]
default_key1 = default_value1
default_key2 = default_value2
[section1]
key1 = value1
key2 = value2
[section2]
key3 = value3
key4 = value4
Python Code
import os
from configparser_override import ConfigParserOverride
# Optionally set environment variables for overriding
os.environ['MYAPP_KEY1'] = 'overridden_default_value1'
os.environ['MYAPP__SECTION1_KEY1'] = 'overridden_value1'
os.environ['MYAPP__SECTION2_KEY3'] = 'overridden_value3'
# Initialize the parser with an optional environment variable prefix
parser = ConfigParserOverride(env_prefix='MYAPP', section2__key4="direct_override_value4")
# Read configuration from a file
config = parser.read(filenames='config.ini')
# Access the configuration
print(config.defaults()['default_key1']) # Output: overridden_default_value1
print(config.defaults()['default_key2']) # Output: default_value2
print(config['section1']['key1']) # Output: overridden_value1
print(config['section1']['key2']) # Output: value2
print(config['section2']['key3']) # Output: overridden_value3
print(config['section2']['key4']) # Output: direct_override_value4
Note
Configuration values can be overridden in three ways, in order of precedence:
- Directly passed arguments during initialization of the class.
- Environment variables.
- Configuration files.
The format for environment variable names is as follows:
- When no prefix is set: The format is
[KEY]
. - When a prefix is set: The format is
[PREFIX]_[KEY]
. - Sections are denoted with double underscores (
__
).- The format is
[PREFIX]__[SECTION]_[KEY]
or[SECTION]_[KEY]
if no prefix is set.
- The format is
For example, to override key1
in section1
with a prefix MYAPP
, the
environment variable would be MYAPP__SECTION1_KEY1
.
Development
To list available commands for your convenience:
make help
Local environment setup
python3 -m venv ./venv
source ./venv/bin/activate # Linux and MacOS
venv\Scripts\activate # Windows
pip install --editable .[dev]
Run tests
make pytest # Run pytest
make style # Run lint formatting and type check
make test-all # Run all tests with tox
make auto-fix # Auto-fix possible style issues
Pre-commit hooks
To install optional pre-commit hooks; after environment set-up run:
pre-commit install
Project maintenance
Intended for project maintainers
Release
Bump my version is used to bump the semantic version of the project.
For details see:
bump-my-version bump --help
Bump my version is configured to create a new commit
and tag
it with the
new version when a version is bumped.
When a new tag is pushed to github the publish-pypi workflow is triggered and will build and publish the new version to PyPi.
Documentation
Sphinx is used to create documentation for the project. To generate:
cd docs
make apidocs # Generates API reference documentation for the code of the project
make html # Generates HTML that can be viwed in the browser
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
Hashes for configparser_override-0.1.0.tar.gz
Algorithm | Hash digest | |
---|---|---|
SHA256 | 88ec5ea57af440e5b7e1d5360ad2fad524800192b51d0df64b8d17add263d0f4 |
|
MD5 | d8e802b7cb1bda0cca06e0e148541285 |
|
BLAKE2b-256 | 33319a0db2063f0ed4a3074d7aa2810969c2827b7e8ae64eaf090ba013c78848 |
Hashes for configparser_override-0.1.0-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 00d30bb285702949811575c5794a014acbffe87b93ca6fcf1c21f6dc9c53d2b4 |
|
MD5 | 038620025ee2abe6e6997b2060941f39 |
|
BLAKE2b-256 | f0164af9ae80367433a7034ffbafee320f6b81f4a4cca386abcfdb61a75550be |