Utilities for flexible configuration for Django.
Project description
django-flexi-settings
Package that allows flexible settings configuration for Django projects.
Installation
Install directly from GitHub:
pip install git+https://github.com/cedadev/django-flexi-settings.git
Usage
Using the include functions
django-flexi-settings
provides two functions that can be used to include settings
from other files in your settings.py
:
# my_site/settings.py
INSTALLED_APPS = ['app1', '...']
# ... Other settings ...
# Use flexible settings utilities to include settings from other files
from flexible_settings import include, include_dir
# Include a file with one of the supported extensions
# Python files DO NOT have to be on the PYTHONPATH
include('/path/to/pythonfile.py')
include('/path/to/pythonfile.conf') # This is also treated as python
include('/path/to/yamlfile.yaml')
include('/path/to/jsonfile.json')
# Or include a whole directory
# The files are included in lexicographical order, so to control ordering you
# might name the files 01-somesettings.py, 02-moresettings.yaml, etc.
include_dir('/path/to/config/directory')
If the included files are Python, they can modify existing variables:
# /path/to/pythonfile.py
INSTALLED_APPS += ['extra_app1', 'extra_app2']
For YAML and JSON files, keys are normalised and dictionaries are merged in the resulting settings, e.g. for these two YAML files:
# /path/to/yamlfile1.yaml
SETTING_1: setting1
DICT_SETTING:
KEY1: value1
KEY2: value2
# /path/to/yamlfile2.yaml
dictSetting:
key1: overridden
key3: value3
the resulting settings would be:
SETTING_1 = "setting1"
DICT_SETTING = {
"KEY1": "overridden",
"KEY2": "value2",
"KEY3": "value3"
}
Using the settings module
In the case where your settings are defined in a file that isn't on the PYTHONPATH
,
django-flexi-settings
provides a module that can be used as the DJANGO_SETTINGS_MODULE
.
This module respects the value of an environment variable, DJANGO_FLEXI_SETTINGS_ROOT
,
that determines the settings file to include. It defaults to /etc/django/settings.py
.
export DJANGO_FLEXI_SETTINGS_ROOT="/etc/myapp/settings.py"
export DJANGO_SETTINGS_MODULE="flexi_settings.settings"
If the file specified in DJANGO_FLEXI_SETTINGS_ROOT
is a Python file, it can then use the
include
and include_dir
functions to include other settings if desired. For example,
the following file could be used as DJANGO_FLEXI_SETTINGS_ROOT
to allow drop-in changes to
settings by placing additional files in a settings.d
directory:
from pathlib import Path
from flexi_settings import include_dir
include_dir(Path(__file__).resolve().parent / 'settings.d')
This makes for a very flexible configuration system for Django that is not application-specific.
Adding additional loaders
django-flexi-settings
makes adding new loaders for additional file types very easy.
A loader in django-flexi-settings
is just a function that takes a file path and a settings
dictionary and modifies the settings dictionary in a way consistent with the specified file.
Note that the existing dictionary is modified, not a new dictionary returned. Please refer
to the built-in loaders.
To declare the extensions for which the additional loader is valid, add an extensions
property
to the loader function:
def load_ini(path, settings):
# ... Do something with path and settings ...
load_ini.extensions = { '.ini' }
To register the loader with django-flexi-settings
, use the entry point:
# mypackage/setup.py
from setuptools import setup
if __name__ == "__main__":
setup(
# ... Other setup, e.g. name, requires
entry_points = {
'flexi_settings.loaders': [
'ini = mypackage.flexi_settings:load_ini',
]
}
)
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 django-flexi-settings-0.1.0.tar.gz
Algorithm | Hash digest | |
---|---|---|
SHA256 | d949ec131dee1a77984f725adebc4760b24c71151f20a0ff6beeabe53216e404 |
|
MD5 | e72879da41f112637e394b5c2a3cae2e |
|
BLAKE2b-256 | f3fc5101f338cd646acc891af1fae8fd6ae4bb269be1659df4f0fbe03735c4d1 |
Hashes for django_flexi_settings-0.1.0-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 375bc5ba4086e66239c91f11d9d78ebb39757b0b4b386c39a7c30ebaeb18e68b |
|
MD5 | 9ab2a02f27376c964ff8b294e1f61a63 |
|
BLAKE2b-256 | ccaf141d74ac63362890597b5b5b9ec4ffa909511140ed63e6e5fb6353ab6b47 |