Load configuration variables from a file or environment
Project description
A thin wrapper over Pydantic’s settings management. Allows you to define configuration variables and load them from environment or JSON/YAML file. Also generates initial configuration files and documentation for your defined configuration.
Installation
pip install goodconf or pip install goodconf[yaml] / pip install goodconf[toml] if parsing/generating YAML/TOML files is required.
Quick Start
Let’s use configurable Django settings as an example.
First, create a conf.py file in your project’s directory, next to settings.py:
import base64
import os
from goodconf import GoodConf, Field
from pydantic import PostgresDsn
class AppConfig(GoodConf):
"Configuration for My App"
DEBUG: bool
DATABASE_URL: PostgresDsn = "postgres://localhost:5432/mydb"
SECRET_KEY: str = Field(
initial=lambda: base64.b64encode(os.urandom(60)).decode(),
description="Used for cryptographic signing. "
"https://docs.djangoproject.com/en/2.0/ref/settings/#secret-key")
class Config:
default_files = ["/etc/myproject/myproject.yaml", "myproject.yaml"]
config = AppConfig()
Next, use the config in your settings.py file:
import dj_database_url
from .conf import config
config.load()
DEBUG = config.DEBUG
SECRET_KEY = config.SECRET_KEY
DATABASES = {"default": dj_database_url.parse(config.DATABASE_URL)}
In your initial developer installation instructions, give some advice such as:
python -c "import myproject; print(myproject.conf.config.generate_yaml(DEBUG=True))" > myproject.yaml
Better yet, make it a function and entry point so you can install your project and run something like generate-config > myproject.yaml.
Usage
GoodConf
Your subclassed GoodConf object can include a Config class with the following attributes:
- file_env_var
The name of an environment variable which can be used for the name of the configuration file to load.
- default_files
If no file is passed to the load method, try to load a configuration from these files in order.
It also has one method:
- load
Trigger the load method during instantiation. Defaults to False.
Use plain-text docstring for use as a header when generating a configuration file.
Environment variables always take precedence over variables in the configuration files.
See Pydantic’s docs for examples of loading:
Fields
Declare configuration values by subclassing GoodConf and defining class attributes which are standard Python type definitions or Pydantic FieldInfo instances generated by the Field function.
Goodconf can use one extra argument provided to the Field to define an function which can generate an initial value for the field:
- initial
Callable to use for initial value when generating a config
Django Usage
A helper is provided which monkey-patches Django’s management commands to accept a --config argument. Replace your manage.py with the following:
# Define your GoodConf in `myproject/conf.py`
from myproject.conf import config
if __name__ == '__main__':
config.django_manage()
Why?
I took inspiration from logan (used by Sentry) and derpconf (used by Thumbor). Both, however used Python files for configuration. I wanted a safer format and one that was easier to serialize data into from a configuration management system.
Environment Variables
I don’t like working with environment variables. First, there are potential security issues:
Accidental leaks via logging or error reporting services.
Child process inheritance (see ImageTragick for an idea why this could be bad).
Second, in practice on deployment environments, environment variables end up getting written to a number of files (cron, bash profile, service definitions, web server config, etc.). Not only is it cumbersome, but also increases the possibility of leaks via incorrect file permissions.
I prefer a single structured file which is explicitly read by the application. I also want it to be easy to run my applications on services like Heroku where environment variables are the preferred configuration method.
This module let’s me do things the way I prefer in environments I control, but still run them with environment variables on environments I don’t control with minimal fuss.
Contribute
Create virtual environment and install package and dependencies.
pip install -e ".[tests]"
Run tests
pytest
Releasing a new version to PyPI:
export VERSION=X.Y.Z
git tag -s v$VERSION -m v$VERSION
git push --tags
rm -rf ./dist
hatch build
hatch publish
gh release create v$VERSION dist/goodconf-$VERSION* --generate-notes --verify-tag
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
File details
Details for the file goodconf-4.0.0.tar.gz
.
File metadata
- Download URL: goodconf-4.0.0.tar.gz
- Upload date:
- Size: 25.2 MB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: python-httpx/0.23.3
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | af3c6dce410821841ed64c9d36f7aa911da55cd4b2a19db84f85923cf5665e88 |
|
MD5 | 1591b6740d9d69cf6e7e2bac8a2939c7 |
|
BLAKE2b-256 | 132bf1937527d6aab185721f2fb7f233037ea7374da8e6850d8c250e49021ccc |
Provenance
File details
Details for the file goodconf-4.0.0-py3-none-any.whl
.
File metadata
- Download URL: goodconf-4.0.0-py3-none-any.whl
- Upload date:
- Size: 10.0 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: python-httpx/0.23.3
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | b194e3e094923c23d59299cb2628d602c6aac792ab7f3a8c8b727b1fbeb31b35 |
|
MD5 | 0add2bd35151da152b80699f3a645f74 |
|
BLAKE2b-256 | 12dd375a574f5b70536aa32fade801a164da5aa727da3f3bab9aef59e2275d05 |