From 07fecd7ad2f8cb6e1c294fdf3a8ff7fd975a41c0 Mon Sep 17 00:00:00 2001 From: Daniel Hahler Date: Wed, 20 Jan 2016 02:10:30 +0100 Subject: [PATCH] Add configurations.autosetup This will automatically import and call `setup()`. It is useful to keep the boilerplate with using django-configurations to a minimum, and also play nice with helpers like "isort" that would tear the "import" and calling of "configurations.setup()" apart - which is understandable. The downside obviously is that importing this module _will_ have side-effects, which is considered to be bad in general [1]. So this is open for discussion/feedback. I could imagine doing the automatic call to `setup()` only when Django is detected for example. 1: http://chrismorgan.info/blog/say-no-to-import-side-effects-in-python.html --- configurations/autosetup.py | 4 ++++ docs/cookbook.rst | 8 +++----- 2 files changed, 7 insertions(+), 5 deletions(-) create mode 100644 configurations/autosetup.py diff --git a/configurations/autosetup.py b/configurations/autosetup.py new file mode 100644 index 0000000..fbac409 --- /dev/null +++ b/configurations/autosetup.py @@ -0,0 +1,4 @@ +"""A module to automatically import and call :func:`setup()`.""" +from . import setup + +setup() diff --git a/docs/cookbook.rst b/docs/cookbook.rst index 8fd2173..0d860ba 100644 --- a/docs/cookbook.rst +++ b/docs/cookbook.rst @@ -125,8 +125,7 @@ probably just add the following to the **beginning** of your settings module: .. code-block:: python - import configurations - configurations.setup() + from configurations import autosetup That has the same effect as using the ``manage.py`` or ``wsgi.py`` utilities. This will also call ``django.setup()`` on Django >= 1.7. @@ -155,8 +154,7 @@ Celery's documentation`_: os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'mysite.settings') os.environ.setdefault('DJANGO_CONFIGURATION', 'MySiteConfiguration') - import configurations - configurations.setup() + from configurations import autosetup app = Celery('mysite') app.config_from_object('django.conf:settings') @@ -267,4 +265,4 @@ the environment variable accordingly: 'configurations', ] - # ... \ No newline at end of file + # ...