Skip to main content

Easy file-based and environment configuration.

Project description

EasyConf

Easy file-based and environment configuration.

Usage

First create a Config object:

config = easyconf.Config('config.yaml')

Then reference a configuration variable as an attribute of this object:

some_variable = config.SOME_VARIABLE()

At runtime, EasyConf will try to each variable from the following sources in order:

  1. A matching environment variable

  2. A matching variable within the configuration file

  3. The default attribute (if no attribute is provided, an easyconf.config.RequiredConfigVarMissing exception will be raised)

Config file generation

If the Config file doesn’t exist yet and the file location is writable, EasyConf will generate it using the initial values and commented default values defined in each configuration variable.

If the configuration file is within a git repository it will also be added to the .gitignore file.

Don’t hide configuration variables within conditional statements, load them at the top of a module so they can be generated regardless.

If you don’t want file generation for non-existant configuration, use generate=False when instantiating the Config object.

Example Django configuration

import easyconf

config = easyconf.Config('myproject.yaml')

DEBUG = config.DEBUG(default=False)
DATABASES = {'default': config.DATABASE(
    default='postgres:///myproject',
    cast=easyconf.dict_or_url,
)}
SECRET_KEY = config.SECRET_KEY(initial=easyconf.random_text_generator(60))

Configuration Variable options

default

The default value for the variable if it is not provided in the environment or in the configuration file.

initial

A value or callable used to set the initial value of a variable in the configuration file.

help

Help text to use in the configuration file for this variable.

cast

A callable to convert an incoming value (from the environment or default value) to the correct Python type.

Set the cast_from_config attribute of this callable to True if it is safe to cast from the configuration file as well.

Config Object options

default_files

A file or list of files to attempt to read configuration variables from. If multiple files are provided, only the first one found will be used.

If none of the provided files are found, the first one in a writable location will be created and populated automatically.

file_env_var

An environment variable which could be used to specify the configuration file path.

If provided and an environment variable matches, this overrides any default_files specified.

generate

Whether to generate a new config file if none of the default_files can be loaded. Defaults to True.

Helper Modules

easyconf.dict_or_url

Expand url strings defined in django-environ into dictionaries. For example:

>>> import easyconf
>>> easyconf.dict_or_url('mysql:///abc')
{'NAME': 'abc', 'USER': '', 'PASSWORD': '', 'HOST': '', 'PORT': '', 'ENGINE': 'django.db.backends.mysql'}

This will also cast url strings coming from the configuration file.

easyconf.random_text_generator

Creates a function that can be called to securely generate a random text string of a predefined length (containing base64 characters).

Useful for the initial configuration variable option.

>>> import easyconf
>>> gen = easyconf.random_text_generator(20)
>>> gen()
'sYw0D/7xjXqxfCyUdHqr'
>>> gen()
'ig1Z1n+mFLt+qYNOmD6I'

Change Log

1.3.1 (2019-08-26)

  • Don’t require git executable to be installed.

1.3 (2019-08-02)

  • Use environment values for automatic configuration generation.

1.2.2 (2019-08-02)

  • Correctly lock down ruamel.yaml this time (it’s v0.16, not v16).

1.2.1 (2019-08-01)

  • Avoid new version of ruamel.yaml which changed loading format.

1.2 (2019-05-21)

  • When generating a config that is within a git repository, add it to .gitignore.

  • If a cast callable has a cast_from_config attribute set to True, cast values coming from the configuration file. This is set to True for the dict_or_url helper method.

1.1.1 (2019-05-16)

  • Fix issue with generating multiple comments (for multiple default values) at the end of the YAML config.

  • Cast default values when writing to config.

1.1 (2019-05-15)

  • Allow explicit disabling of automatic config generation.

  • Cast default values if a cast argument is provided.

1.0 (2019-05-15)

  • Initial Release

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

easyconf-1.3.1.tar.gz (9.8 kB view details)

Uploaded Source

Built Distribution

easyconf-1.3.1-py2.py3-none-any.whl (8.6 kB view details)

Uploaded Python 2 Python 3

File details

Details for the file easyconf-1.3.1.tar.gz.

File metadata

  • Download URL: easyconf-1.3.1.tar.gz
  • Upload date:
  • Size: 9.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.21.0 setuptools/41.0.1 requests-toolbelt/0.9.1 tqdm/4.32.1 CPython/3.7.3

File hashes

Hashes for easyconf-1.3.1.tar.gz
Algorithm Hash digest
SHA256 d7127c93e5e1720b308a8f238876b49ffd40bc4b9ccfc215c03c3c93ac2aa10e
MD5 fa46569099433b6cf474fac8852a7828
BLAKE2b-256 345d10cbbcb491740228ca4bf621f2816547c1b6ecd6f440ab6ed4e5e4d098f8

See more details on using hashes here.

File details

Details for the file easyconf-1.3.1-py2.py3-none-any.whl.

File metadata

  • Download URL: easyconf-1.3.1-py2.py3-none-any.whl
  • Upload date:
  • Size: 8.6 kB
  • Tags: Python 2, Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.21.0 setuptools/41.0.1 requests-toolbelt/0.9.1 tqdm/4.32.1 CPython/3.7.3

File hashes

Hashes for easyconf-1.3.1-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 9c72ae0702625a60792120e030b28ae3542b88dc7c7b9dd6eae3a2a845d1edaa
MD5 09da7522f54bdd2862b7e05bf65c9fe2
BLAKE2b-256 aa84b229d0084684882e7a335c6cb054ec5a5e50111a26dac6c743d75fb8dbdc

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page