Skip to content

Commit

Permalink
Merge pull request #27 from mikebryant/better-autoconfig-module-detec…
Browse files Browse the repository at this point in the history
…tion

Try to detect the autoconfig module without importing the app. Fixes #23
  • Loading branch information
mikebryant committed Jan 8, 2016
2 parents e93b0b6 + 1678de9 commit d890924
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 21 deletions.
26 changes: 14 additions & 12 deletions django_autoconfig/autoconfig.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
from django.conf.urls import include, patterns, url
from django.utils.functional import Promise
from django.utils.module_loading import module_has_submodule
import imp
import importlib
import operator

Expand All @@ -15,13 +16,6 @@

MAX_ITERATIONS = 1000

SETTINGS = {
'AUTOCONFIG_DISABLED_APPS': [
'django.contrib.admin',
'django.contrib.auth',
],
}

class OrderingRelationship(object):
'''
This class defines a relationship between an element in a setting
Expand Down Expand Up @@ -144,6 +138,18 @@ def merge_dictionaries(current, new, only_defaults=False, template_special_case=
changes += 1
return changes

def autoconfig_module_exists(app_name):
names = app_name.split('.')
path = None
for name in names:
_, path, _ = imp.find_module(name, path)
path = [path]
try:
imp.find_module('autoconfig', path)
return True
except ImportError:
return False

def configure_settings(settings, environment_settings=True):
'''
Given a settings object, run automatic configuration of all
Expand All @@ -158,12 +164,8 @@ def configure_settings(settings, environment_settings=True):
if environment_settings:
app_names.append('django_autoconfig.environment_settings')
for app_name in app_names:
if app_name not in settings.get('AUTOCONFIG_DISABLED_APPS', ()):
app_module = importlib.import_module(app_name)
else:
app_module = None
import django_autoconfig.contrib
if app_module and module_has_submodule(app_module, 'autoconfig'):
if autoconfig_module_exists(app_name):
module = importlib.import_module("%s.autoconfig" % (app_name,))
elif app_name in django_autoconfig.contrib.CONTRIB_CONFIGS:
module = django_autoconfig.contrib.CONTRIB_CONFIGS[app_name]
Expand Down
9 changes: 0 additions & 9 deletions django_autoconfig/tests/test_autoconfig.py
Original file line number Diff line number Diff line change
Expand Up @@ -145,15 +145,6 @@ def test_importerror_from_import_error(self):
autoconfig.configure_settings(self.settings_dict)
self.assertIn('flibble', str(exception_manager.exception))

def test_disabled_autoconfig(self):
'''
Test the ability to disable autoconfig for some apps.
'''
self.settings_dict['INSTALLED_APPS'] = ['django_autoconfig.tests.app_list']
self.settings_dict['AUTOCONFIG_DISABLED_APPS'] = ['django_autoconfig.tests.app_list']
autoconfig.configure_settings(self.settings_dict)
self.assertEqual(self.settings_dict['LIST_SETTING'], [1, 2])

def test_contrib_autoconfig(self):
'''
Test autoconfig provided by us instead of the app.
Expand Down

0 comments on commit d890924

Please sign in to comment.