diff options
| author | Aymeric Augustin <aymeric.augustin@m4x.org> | 2013-12-19 15:57:23 +0100 |
|---|---|---|
| committer | Aymeric Augustin <aymeric.augustin@m4x.org> | 2013-12-22 11:39:18 +0100 |
| commit | 65cd74be8e99d06c7861edc5050e34d6444e4d56 (patch) | |
| tree | 72431d2a70af3d235543781fadce8e647c746959 /django | |
| parent | d4733b6df07b1f8a869eed8eac86869d1d14472c (diff) | |
Stopped iterating on INSTALLED_APPS.
Used the app cache's get_app_configs() method instead.
Diffstat (limited to 'django')
| -rw-r--r-- | django/contrib/staticfiles/finders.py | 6 | ||||
| -rw-r--r-- | django/core/management/__init__.py | 20 | ||||
| -rw-r--r-- | django/core/management/commands/flush.py | 5 | ||||
| -rw-r--r-- | django/core/management/commands/migrate.py | 7 | ||||
| -rw-r--r-- | django/template/base.py | 8 | ||||
| -rw-r--r-- | django/template/loaders/app_directories.py | 11 | ||||
| -rw-r--r-- | django/template/loaders/eggs.py | 7 | ||||
| -rw-r--r-- | django/utils/autoreload.py | 9 | ||||
| -rw-r--r-- | django/utils/module_loading.py | 9 | ||||
| -rw-r--r-- | django/utils/translation/trans_real.py | 8 | ||||
| -rw-r--r-- | django/views/i18n.py | 6 |
11 files changed, 51 insertions, 45 deletions
diff --git a/django/contrib/staticfiles/finders.py b/django/contrib/staticfiles/finders.py index cf5eb4f85b..cadc8bffa7 100644 --- a/django/contrib/staticfiles/finders.py +++ b/django/contrib/staticfiles/finders.py @@ -2,6 +2,7 @@ from collections import OrderedDict import os from django.conf import settings +from django.core.apps import app_cache from django.core.exceptions import ImproperlyConfigured from django.core.files.storage import default_storage, Storage, FileSystemStorage from django.utils.functional import empty, LazyObject @@ -116,10 +117,11 @@ class AppDirectoriesFinder(BaseFinder): def __init__(self, apps=None, *args, **kwargs): # The list of apps that are handled self.apps = [] - # Mapping of app module paths to storage instances + # Mapping of app names to storage instances self.storages = OrderedDict() if apps is None: - apps = settings.INSTALLED_APPS + app_configs = app_cache.get_app_configs() + apps = [app_config.name for app_config in app_configs] for app in apps: app_storage = self.storage_class(app) if os.path.isdir(app_storage.location): diff --git a/django/core/management/__init__.py b/django/core/management/__init__.py index 0b47e179db..a7d074e149 100644 --- a/django/core/management/__init__.py +++ b/django/core/management/__init__.py @@ -5,6 +5,7 @@ from optparse import OptionParser, NO_DEFAULT import os import sys +from django.conf import settings from django.core.exceptions import ImproperlyConfigured from django.core.management.base import BaseCommand, CommandError, handle_default_options from django.core.management.color import color_style @@ -106,13 +107,19 @@ def get_commands(): _commands = dict((name, 'django.core') for name in find_commands(__path__[0])) # Find the installed apps - from django.conf import settings try: - apps = settings.INSTALLED_APPS + settings.INSTALLED_APPS except ImproperlyConfigured: - # Still useful for commands that do not require functional settings, - # like startproject or help + # Still useful for commands that do not require functional + # settings, like startproject or help. apps = [] + else: + # Populate the app cache outside of the try/except block to avoid + # catching ImproperlyConfigured errors that aren't caused by the + # absence of a settings module. + from django.core.apps import app_cache + app_configs = app_cache.get_app_configs() + apps = [app_config.name for app_config in app_configs] # Find and load the management module for each installed app. for app_name in apps: @@ -339,9 +346,10 @@ class ManagementUtility(object): elif cwords[0] in ('dumpdata', 'sql', 'sqlall', 'sqlclear', 'sqlcustom', 'sqlindexes', 'sqlsequencereset', 'test'): try: - from django.conf import settings + from django.core.apps import app_cache + app_configs = app_cache.get_app_configs() # Get the last part of the dotted path as the app name. - options += [(a.split('.')[-1], 0) for a in settings.INSTALLED_APPS] + options += [(app_config.label, 0) for app_config in app_configs] except ImportError: # Fail silently if DJANGO_SETTINGS_MODULE isn't set. The # user will find out once they execute the command. diff --git a/django/core/management/commands/flush.py b/django/core/management/commands/flush.py index 562147e403..f13e948ae5 100644 --- a/django/core/management/commands/flush.py +++ b/django/core/management/commands/flush.py @@ -2,7 +2,6 @@ import sys from importlib import import_module from optparse import make_option -from django.conf import settings from django.core.apps import app_cache from django.db import connections, router, transaction, DEFAULT_DB_ALIAS from django.core.management import call_command @@ -42,9 +41,9 @@ class Command(NoArgsCommand): # Import the 'management' module within each installed app, to register # dispatcher events. - for app_name in settings.INSTALLED_APPS: + for app_config in app_cache.get_app_configs(): try: - import_module('.management', app_name) + import_module('.management', app_config.name) except ImportError: pass diff --git a/django/core/management/commands/migrate.py b/django/core/management/commands/migrate.py index b607e8ee43..c99f26aa0c 100644 --- a/django/core/management/commands/migrate.py +++ b/django/core/management/commands/migrate.py @@ -6,7 +6,6 @@ from importlib import import_module import itertools import traceback -from django.conf import settings from django.core.apps import app_cache from django.core.management import call_command from django.core.management.base import BaseCommand, CommandError @@ -47,9 +46,9 @@ class Command(BaseCommand): # Import the 'management' module within each installed app, to register # dispatcher events. - for app_name in settings.INSTALLED_APPS: - if module_has_submodule(import_module(app_name), "management"): - import_module('.management', app_name) + for app_config in app_cache.get_app_configs(): + if module_has_submodule(app_config.app_module, "management"): + import_module('.management', app_config.name) # Get the database we're operating from db = options.get('database') diff --git a/django/template/base.py b/django/template/base.py index c314637c42..7098ea60f8 100644 --- a/django/template/base.py +++ b/django/template/base.py @@ -6,6 +6,7 @@ from importlib import import_module from inspect import getargspec, getcallargs from django.conf import settings +from django.core.apps import app_cache from django.template.context import (BaseContext, Context, RequestContext, # NOQA: imported for backwards compatibility ContextPopException) from django.utils.itercompat import is_iterable @@ -1302,9 +1303,12 @@ def get_templatetags_modules(): # Populate list once per process. Mutate the local list first, and # then assign it to the global name to ensure there are no cases where # two threads try to populate it simultaneously. - for app_module in ['django'] + list(settings.INSTALLED_APPS): + + templatetags_modules_candidates = ['django.templatetags'] + templatetags_modules_candidates += ['%s.templatetags' % app_config.name + for app_config in app_cache.get_app_configs()] + for templatetag_module in templatetags_modules_candidates: try: - templatetag_module = '%s.templatetags' % app_module import_module(templatetag_module) _templatetags_modules.append(templatetag_module) except ImportError: diff --git a/django/template/loaders/app_directories.py b/django/template/loaders/app_directories.py index 4f8ddfccac..90baa34c7a 100644 --- a/django/template/loaders/app_directories.py +++ b/django/template/loaders/app_directories.py @@ -3,12 +3,11 @@ Wrapper for loading templates from "templates" directories in INSTALLED_APPS packages. """ -from importlib import import_module import os import sys from django.conf import settings -from django.core.exceptions import ImproperlyConfigured +from django.core.apps import app_cache from django.template.base import TemplateDoesNotExist from django.template.loader import BaseLoader from django.utils._os import safe_join @@ -18,12 +17,8 @@ from django.utils import six if six.PY2: fs_encoding = sys.getfilesystemencoding() or sys.getdefaultencoding() app_template_dirs = [] -for app in settings.INSTALLED_APPS: - try: - mod = import_module(app) - except ImportError as e: - raise ImproperlyConfigured('ImportError %s: %s' % (app, e.args[0])) - template_dir = os.path.join(os.path.dirname(mod.__file__), 'templates') +for app_config in app_cache.get_app_configs(): + template_dir = os.path.join(app_config.path, 'templates') if os.path.isdir(template_dir): if six.PY2: template_dir = template_dir.decode(fs_encoding) diff --git a/django/template/loaders/eggs.py b/django/template/loaders/eggs.py index 04e4f73b7b..e9b46b15de 100644 --- a/django/template/loaders/eggs.py +++ b/django/template/loaders/eggs.py @@ -7,6 +7,7 @@ except ImportError: resource_string = None from django.conf import settings +from django.core.apps import app_cache from django.template.base import TemplateDoesNotExist from django.template.loader import BaseLoader from django.utils import six @@ -23,12 +24,12 @@ class Loader(BaseLoader): """ if resource_string is not None: pkg_name = 'templates/' + template_name - for app in settings.INSTALLED_APPS: + for app_config in app_cache.get_app_configs(): try: - resource = resource_string(app, pkg_name) + resource = resource_string(app_config.name, pkg_name) except Exception: continue if six.PY2: resource = resource.decode(settings.FILE_CHARSET) - return (resource, 'egg:%s:%s' % (app, pkg_name)) + return (resource, 'egg:%s:%s' % (app_config.name, pkg_name)) raise TemplateDoesNotExist(template_name) diff --git a/django/utils/autoreload.py b/django/utils/autoreload.py index 1913317822..05ffb12f74 100644 --- a/django/utils/autoreload.py +++ b/django/utils/autoreload.py @@ -37,9 +37,8 @@ import time import traceback from django.conf import settings +from django.core.apps import app_cache from django.core.signals import request_finished -from django.utils._os import upath -from importlib import import_module try: from django.utils.six.moves import _thread as thread except ImportError: @@ -91,10 +90,8 @@ def gen_filenames(): basedirs = [os.path.join(os.path.dirname(os.path.dirname(__file__)), 'conf', 'locale'), 'locale'] - for appname in reversed(settings.INSTALLED_APPS): - app = import_module(appname) - basedirs.append(os.path.join(os.path.dirname(upath(app.__file__)), - 'locale')) + for app_config in reversed(list(app_cache.get_app_configs())): + basedirs.append(os.path.join(app_config.path, 'locale')) basedirs.extend(settings.LOCALE_PATHS) basedirs = [os.path.abspath(basedir) for basedir in basedirs if os.path.isdir(basedir)] diff --git a/django/utils/module_loading.py b/django/utils/module_loading.py index 0c6bad2a7b..8d95fc00a3 100644 --- a/django/utils/module_loading.py +++ b/django/utils/module_loading.py @@ -58,18 +58,17 @@ def autodiscover_modules(*args, **kwargs): registry. This register_to object must have a _registry instance variable to access it. """ - from django.conf import settings + from django.core.apps import app_cache register_to = kwargs.get('register_to') - for app in settings.INSTALLED_APPS: - mod = import_module(app) + for app_config in app_cache.get_app_configs(): # Attempt to import the app's module. try: if register_to: before_import_registry = copy.copy(register_to._registry) for module_to_search in args: - import_module('%s.%s' % (app, module_to_search)) + import_module('%s.%s' % (app_config.name, module_to_search)) except: # Reset the model registry to the state before the last import as # this import will have to reoccur on the next request and this @@ -81,7 +80,7 @@ def autodiscover_modules(*args, **kwargs): # Decide whether to bubble up this error. If the app just # doesn't have an admin module, we can ignore the error # attempting to import it, otherwise we want it to bubble up. - if module_has_submodule(mod, module_to_search): + if module_has_submodule(app_config.app_module, module_to_search): raise diff --git a/django/utils/translation/trans_real.py b/django/utils/translation/trans_real.py index e547800b92..3ed6371966 100644 --- a/django/utils/translation/trans_real.py +++ b/django/utils/translation/trans_real.py @@ -7,10 +7,10 @@ import os import re import sys import gettext as gettext_module -from importlib import import_module from threading import local import warnings +from django.core.apps import app_cache from django.dispatch import receiver from django.test.signals import setting_changed from django.utils.encoding import force_str, force_text @@ -179,10 +179,8 @@ def translation(language): res.merge(t) return res - for appname in reversed(settings.INSTALLED_APPS): - app = import_module(appname) - apppath = os.path.join(os.path.dirname(upath(app.__file__)), 'locale') - + for app_config in reversed(list(app_cache.get_app_configs())): + apppath = os.path.join(app_config.path, 'locale') if os.path.isdir(apppath): res = _merge(apppath) diff --git a/django/views/i18n.py b/django/views/i18n.py index 751905efaf..2f0c89a037 100644 --- a/django/views/i18n.py +++ b/django/views/i18n.py @@ -5,6 +5,7 @@ import gettext as gettext_module from django import http from django.conf import settings +from django.core.apps import app_cache from django.template import Context, Template from django.utils.translation import check_for_language, to_locale, get_language from django.utils.encoding import smart_text @@ -187,7 +188,10 @@ def render_javascript_catalog(catalog=None, plural=None): def get_javascript_catalog(locale, domain, packages): default_locale = to_locale(settings.LANGUAGE_CODE) - packages = [p for p in packages if p == 'django.conf' or p in settings.INSTALLED_APPS] + app_configs = app_cache.get_app_configs() + allowable_packages = set(app_config.name for app_config in app_configs) + allowable_packages.add('django.conf') + packages = [p for p in packages if p in allowable_packages] t = {} paths = [] en_selected = locale.startswith('en') |
