1
0
mirror of https://github.com/django/django.git synced 2025-10-23 21:59:11 +00:00

Stopped iterating on INSTALLED_APPS.

Used the app cache's get_app_configs() method instead.
This commit is contained in:
Aymeric Augustin
2013-12-19 15:57:23 +01:00
parent d4733b6df0
commit 65cd74be8e
18 changed files with 116 additions and 82 deletions

View File

@@ -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):

View File

@@ -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.

View File

@@ -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

View File

@@ -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')

View File

@@ -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:

View File

@@ -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)

View File

@@ -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)

View File

@@ -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)]

View File

@@ -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

View File

@@ -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)

View File

@@ -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')