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:
A matching environment variable
A matching variable within the configuration file
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.
Don’t hide configuration variables within conditional statements, load them at the top of a module so they can be generated regardless.
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 configuration file) to the correct Python type.
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.
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'}
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.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
Built Distribution
Hashes for easyconf-1.0-py2.py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | f116aa2ed29f9367840e61191d37c5891ab5806e9822654b7cf2ab003cc5230a |
|
MD5 | f9604804ff0d44c02e0996f15618dd58 |
|
BLAKE2b-256 | f2b0394fa4c7a4a0622c00cef612d614d765d42d949c952a8a33ff9f9f220ab3 |