Home > django-configurator

django-configurator

Django-configurator is a project mainly written in Python, it's free.

Modular configuration for Django applications

This application provides a drop-in replacement for django settings. It allows you to have different places where you put configuration bits. By default, it supposes you will have conf/ directory, where you'll put project-wide settings, and files like my_app/conf.py, where applications will set their settings.

Let's look further into details.

Options can be global or belong to applications:

from configurator import options options.KEY = 1 options.apps['myapp'].KEY = 2 type(options) <class 'configurator.Config'> type(options.apps) <class 'configurator.AppList'> type(options.apps['myapp']) <class 'configurator.Config'>

If the key is missing, DictList is provided:

from configurator import options

KEY = options.SOME_KEY type(KEY) <class 'configurator.containers.DictList'> id(KEY) == id(options.SOME_KEY) True

Next concept is DictList. DictList is options holder and it allows one to set a global option in advance, providing data from one module to another, not having to deal with applications order in INSTALLED_APPS.

Can be used as an ordered set:

KEY += 'xxx' KEY ['xxx'] KEY -= 'yyy' KEY += 'yyy' KEY ['xxx', 'yyy'] KEY -= 'xxx' ['yyy']

In Django, this can be used for AUTHENTICATION_BACKENDS, TEMPLATE_CONTEXT_PROCESSORS, MIDDLEWARE_CLASSES, ADMINS, LANGUAGES and other settings.

Or as an ordered dict:

D = DictList() D['abc'] = 'def' D['key'] = 'value' D.default = '(default)' D {default=(default), 'abc': 'def', 'key': 'value'} In Django, this can be used for DATABASES setup, and, i hope, in 1.3, for LOGGING setup and APP_MEDIA setup.

Currently Django's SortedDict is not used for DictList to not depend on Django.

The next concept is autodiscover. It allows you to have different configuration bits for every application or for different computers. For basic use in django applications, you can put this call in the end of the settings.py:

autodiscover(locals())

It does the following: 1) puts all written in caps settings from settings.py into options, wrapping lists, tuples and dicts into DictList 2) iterates over options.DISCOVERY_ORDER, defined by default as the following:

options.DISCOVERY_ORDER = [ ListFiles(options.lazy.CONF, 'global'),# conf/global.py ListFiles(options.lazy.CONF, options.lazy.SITE), # conf/.py ListFiles(options.lazy.INSTALLED_APPS, 'conf'), # app1/conf.py ListFiles(options.lazy.APP_CONF, options.lazy.INSTALLED_APPS), # conf/app1.py ListFiles(options.lazy.CONF, 'global', '_overrides'), # conf/global_overrides.py ListFiles(options.lazy.CONF, options.lazy.SITE, '_overrides'), # conf/_overrides.py ] You can set or update any option in those files, you can also modify the order they are iterated.

Please note, that this DISCOVERY_ORDER is lazy, and you can set options.CONF to your own folder in settings.py, or do any other thing before DISCOVERY_ORDER will be evaluated.

3) writes updated settings back to settings.py

If you don't want to change anything in settings.py, you can do autodiscover(), update_options() and update_back_settings() manually at any moment of time.

Exceptions, raised in configuration modules, are isolated. If one of your conf files failed to load, console message will appear, or, in addition to the message, application will fail with traceback if console is not a tty.

Known issues: since global django settings are read and set independently from settings.py, they don't know anything about each other. So if you do AUTHENTICATION_BACKENDS += 'logins.MyBackend', no django.contrib.auth.backends.ModelBackend will be added.

Previous:graph