Deprecated get_app_package, get_app_path and get_app_paths.

This commit is contained in:
Aymeric Augustin 2013-12-13 19:28:42 +01:00
parent da36d03fe6
commit 259cd3cd41
6 changed files with 52 additions and 40 deletions

View File

@ -5,6 +5,7 @@ import imp
from importlib import import_module from importlib import import_module
import os import os
import sys import sys
import warnings
from django.conf import settings from django.conf import settings
from django.core.exceptions import ImproperlyConfigured from django.core.exceptions import ImproperlyConfigured
@ -194,36 +195,6 @@ class BaseAppCache(object):
""" """
return [app_config.models_module for app_config in self.get_app_configs()] return [app_config.models_module for app_config in self.get_app_configs()]
def _get_app_package(self, app):
return '.'.join(app.__name__.split('.')[:-1])
def get_app_package(self, app_label):
return self._get_app_package(self.get_app(app_label))
def _get_app_path(self, app):
if hasattr(app, '__path__'): # models/__init__.py package
app_path = app.__path__[0]
else: # models.py module
app_path = app.__file__
return os.path.dirname(upath(app_path))
def get_app_path(self, app_label):
return self._get_app_path(self.get_app(app_label))
def get_app_paths(self):
"""
Returns a list of paths to all installed apps.
Useful for discovering files at conventional locations inside apps
(static files, templates, etc.)
"""
self._populate()
app_paths = []
for app in self.get_apps():
app_paths.append(self._get_app_path(app))
return app_paths
def get_app(self, app_label): def get_app(self, app_label):
""" """
Returns the module containing the models for the given app_label. Returns the module containing the models for the given app_label.
@ -363,6 +334,48 @@ class BaseAppCache(object):
def unset_available_apps(self): def unset_available_apps(self):
self.available_apps = None self.available_apps = None
### DEPRECATED METHODS GO BELOW THIS LINE ###
def _get_app_package(self, app):
return '.'.join(app.__name__.split('.')[:-1])
def get_app_package(self, app_label):
warnings.warn(
"get_app_config(label).name supersedes get_app_package(label).",
PendingDeprecationWarning, stacklevel=2)
return self._get_app_package(self.get_app(app_label))
def _get_app_path(self, app):
if hasattr(app, '__path__'): # models/__init__.py package
app_path = app.__path__[0]
else: # models.py module
app_path = app.__file__
return os.path.dirname(upath(app_path))
def get_app_path(self, app_label):
warnings.warn(
"get_app_config(label).path supersedes get_app_path(label).",
PendingDeprecationWarning, stacklevel=2)
return self._get_app_path(self.get_app(app_label))
def get_app_paths(self):
"""
Returns a list of paths to all installed apps.
Useful for discovering files at conventional locations inside apps
(static files, templates, etc.)
"""
warnings.warn(
"[a.path for a in get_app_configs()] supersedes get_app_paths().",
PendingDeprecationWarning, stacklevel=2)
self._populate()
app_paths = []
for app in self.get_apps():
app_paths.append(self._get_app_path(app))
return app_paths
class AppCache(BaseAppCache): class AppCache(BaseAppCache):
""" """

View File

@ -230,8 +230,8 @@ class Command(BaseCommand):
current directory. current directory.
""" """
dirs = [] dirs = []
for path in app_cache.get_app_paths(): for app_config in app_cache.get_app_configs():
d = os.path.join(path, 'fixtures') d = os.path.join(app_config.path, 'fixtures')
if os.path.isdir(d): if os.path.isdir(d):
dirs.append(d) dirs.append(d)
dirs.extend(list(settings.FIXTURE_DIRS)) dirs.extend(list(settings.FIXTURE_DIRS))

View File

@ -169,7 +169,7 @@ def _split_statements(content):
def custom_sql_for_model(model, style, connection): def custom_sql_for_model(model, style, connection):
opts = model._meta opts = model._meta
app_dirs = [] app_dirs = []
app_dir = app_cache.get_app_path(model._meta.app_label) app_dir = app_cache.get_app_config(model._meta.app_label).path
app_dirs.append(os.path.normpath(os.path.join(app_dir, 'sql'))) app_dirs.append(os.path.normpath(os.path.join(app_dir, 'sql')))
# Deprecated location -- remove in Django 1.9 # Deprecated location -- remove in Django 1.9

View File

@ -46,7 +46,7 @@ class MigrationLoader(object):
if app_label in settings.MIGRATION_MODULES: if app_label in settings.MIGRATION_MODULES:
return settings.MIGRATION_MODULES[app_label] return settings.MIGRATION_MODULES[app_label]
else: else:
return '%s.migrations' % app_cache.get_app_package(app_label) return '%s.migrations' % app_cache.get_app_config(app_label).name
def load_disk(self): def load_disk(self):
""" """

View File

@ -69,14 +69,12 @@ class MigrationWriter(object):
migrations_module = import_module(migrations_package_name) migrations_module = import_module(migrations_package_name)
basedir = os.path.dirname(migrations_module.__file__) basedir = os.path.dirname(migrations_module.__file__)
except ImportError: except ImportError:
app = app_cache.get_app(self.migration.app_label) app_config = app_cache.get_app_config(self.migration.app_label)
app_path = app_cache._get_app_path(app)
app_package_name = app_cache._get_app_package(app)
migrations_package_basename = migrations_package_name.split(".")[-1] migrations_package_basename = migrations_package_name.split(".")[-1]
# Alright, see if it's a direct submodule of the app # Alright, see if it's a direct submodule of the app
if '%s.%s' % (app_package_name, migrations_package_basename) == migrations_package_name: if '%s.%s' % (app_config.name, migrations_package_basename) == migrations_package_name:
basedir = os.path.join(app_path, migrations_package_basename) basedir = os.path.join(app_config.path, migrations_package_basename)
else: else:
raise ImportError("Cannot open migrations module %s for app %s" % (migrations_package_name, self.migration.app_label)) raise ImportError("Cannot open migrations module %s for app %s" % (migrations_package_name, self.migration.app_label))
return os.path.join(basedir, self.filename) return os.path.join(basedir, self.filename)

View File

@ -223,7 +223,8 @@ these changes.
to :setting:`CACHES` and use :data:`django.core.cache.caches` instead. to :setting:`CACHES` and use :data:`django.core.cache.caches` instead.
* ``django.db.models.loading`` will be removed. Use the new application * ``django.db.models.loading`` will be removed. Use the new application
loading APIs instead. loading APIs instead. Several undocumented methods of the ``AppCache`` class
will also be removed.
2.0 2.0
--- ---