All Projects → lincolnloop → goodconf

lincolnloop / goodconf

Licence: MIT license
Transparently load variables from environment or JSON/YAML file.

Programming Languages

python
139335 projects - #7 most used programming language

Projects that are alternatives of or similar to goodconf

climatecontrol
Python library for loading settings and config data from files and environment variables
Stars: ✭ 20 (-75%)
Mutual labels:  config, yaml, configuration, environment-variables, pydantic
Node Convict
Featureful configuration management library for Node.js
Stars: ✭ 1,855 (+2218.75%)
Mutual labels:  config, configuration, environment-variables, env
Dynaconf
Configuration Management for Python ⚙
Stars: ✭ 2,082 (+2502.5%)
Mutual labels:  config, yaml, configuration, environment-variables
sitri
Sitri - powerful settings & configs for python
Stars: ✭ 20 (-75%)
Mutual labels:  config, configuration, environment-variables, pydantic
parse it
A python library for parsing multiple types of config files, envvars & command line arguments that takes the headache out of setting app configurations.
Stars: ✭ 86 (+7.5%)
Mutual labels:  config, yaml, configuration, environment-variables
env
A lightweight package for loading OS environment variables into structs for Go projects
Stars: ✭ 24 (-70%)
Mutual labels:  config, configuration, environment-variables, env
read-env
🔧 Transform environment variables into JSON object with sanitized values.
Stars: ✭ 60 (-25%)
Mutual labels:  config, configuration, environment-variables, env
exenv
Exenv makes loading environment variables from external sources easy.
Stars: ✭ 35 (-56.25%)
Mutual labels:  config, yaml, environment-variables, env
Simple Settings
A simple way to manage your project settings.
Stars: ✭ 165 (+106.25%)
Mutual labels:  config, yaml, configuration
Aconfig
Simple, useful and opinionated config loader.
Stars: ✭ 187 (+133.75%)
Mutual labels:  config, configuration, environment-variables
Environ Config
Python Application Configuration With Environment Variables
Stars: ✭ 210 (+162.5%)
Mutual labels:  config, configuration, environment-variables
Config Lite
A super simple & flexible & useful config module.
Stars: ✭ 78 (-2.5%)
Mutual labels:  config, yaml, configuration
Konf
A type-safe cascading configuration library for Kotlin/Java/Android, supporting most configuration formats
Stars: ✭ 225 (+181.25%)
Mutual labels:  config, yaml, configuration
ini
📝 Go INI config management. support multi file load, data override merge. parse ENV variable, parse variable reference. Dotenv file parse and loader. INI配置读取管理,支持多文件加载,数据覆盖合并, 解析ENV变量, 解析变量引用。DotEnv 解析加载
Stars: ✭ 72 (-10%)
Mutual labels:  config, environment-variables, env
Config
A lightweight yet powerful config package for Go projects
Stars: ✭ 126 (+57.5%)
Mutual labels:  config, configuration, environment-variables
Config
🛠 A configuration library for Go that parses environment variables, JSON files, and reloads automatically on SIGHUP
Stars: ✭ 203 (+153.75%)
Mutual labels:  config, configuration, environment-variables
superconfig
Access environment variables. Also includes presence validation, type coercion and default values.
Stars: ✭ 33 (-58.75%)
Mutual labels:  config, configuration, environment-variables
Mconfig
MCONFIG is a lightweight Golang library for integrating configs files like (json, yml, toml) and environment variables into one config struct.
Stars: ✭ 28 (-65%)
Mutual labels:  config, yaml, environment-variables
Hiyapyco
HiYaPyCo - A Hierarchical Yaml Python Config
Stars: ✭ 58 (-27.5%)
Mutual labels:  config, yaml, configuration
paerser
No description or website provided.
Stars: ✭ 38 (-52.5%)
Mutual labels:  yaml, configuration, environment-variables

Goodconf

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] if parsing/generating YAML 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:

  1. Accidental leaks via logging or error reporting services.
  2. 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
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].