Package: django-flexi-settings
django-flexi-settingsPackage that allows flexible settings configuration for Django projects.InstallationInstall directly from GitHub:pipinstallgit+https://github.com/cedadev/django-flexi-settings.gitUsageUsing the include functionsdjango-flexi-settingsprovides two functions that can be used to include settings
from other files in yoursettings.py:# my_site/settings.pyINSTALLED_APPS=['app1','...']# ... Other settings ...# Use flexible settings utilities to include settings from other filesfromflexible_settingsimportinclude,include_dir# Include a file with one of the supported extensions# Python files DO NOT have to be on the PYTHONPATHinclude('/path/to/pythonfile.py')include('/path/to/pythonfile.conf')# This is also treated as pythoninclude('/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.pyINSTALLED_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.yamlSETTING_1:setting1DICT_SETTING:KEY1:value1KEY2:value2# /path/to/yamlfile2.yamldictSetting:key1:overriddenkey3:value3the resulting settings would be:SETTING_1="setting1"DICT_SETTING={"KEY1":"overridden","KEY2":"value2","KEY3":"value3"}Using the settings moduleIn the case where your settings are defined in a file that isn't on thePYTHONPATH,django-flexi-settingsprovides a module that can be used as theDJANGO_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.exportDJANGO_FLEXI_SETTINGS_ROOT="/etc/myapp/settings.py"exportDJANGO_SETTINGS_MODULE="flexi_settings.settings"If the file specified inDJANGO_FLEXI_SETTINGS_ROOTis a Python file, it can then use theincludeandinclude_dirfunctions to include other settings if desired. For example,
the following file could be used asDJANGO_FLEXI_SETTINGS_ROOTto allow drop-in changes to
settings by placing additional files in asettings.ddirectory:frompathlibimportPathfromflexi_settingsimportinclude_dirinclude_dir(Path(__file__).resolve().parent/'settings.d')This makes for a very flexible configuration system for Django that is not application-specific.Adding additional loadersdjango-flexi-settingsmakes adding new loaders for additional file types very easy.A loader indjango-flexi-settingsis 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 anextensionsproperty
to the loader function:defload_ini(path,settings):# ... Do something with path and settings ...load_ini.extensions={'.ini'}To register the loader withdjango-flexi-settings, use the entry point:# mypackage/setup.pyfromsetuptoolsimportsetupif__name__=="__main__":setup(# ... Other setup, e.g. name, requiresentry_points={'flexi_settings.loaders':['ini = mypackage.flexi_settings:load_ini',]})