2013-09-07 07:23:25 +08:00
|
|
|
import copy
|
2010-05-04 14:14:47 +08:00
|
|
|
import os
|
2021-08-31 16:14:04 +08:00
|
|
|
import sys
|
2015-01-28 20:35:27 +08:00
|
|
|
from importlib import import_module
|
2016-12-01 18:38:01 +08:00
|
|
|
from importlib.util import find_spec as importlib_find
|
2010-04-16 02:44:51 +08:00
|
|
|
|
2013-02-03 05:58:02 +08:00
|
|
|
|
2021-08-31 16:14:04 +08:00
|
|
|
def cached_import(module_path, class_name):
|
2021-10-04 13:47:49 +08:00
|
|
|
# Check whether module is loaded and fully initialized.
|
|
|
|
if not (
|
|
|
|
(module := sys.modules.get(module_path))
|
|
|
|
and (spec := getattr(module, "__spec__", None))
|
|
|
|
and getattr(spec, "_initializing", False) is False
|
2021-09-16 13:12:58 +08:00
|
|
|
):
|
2021-10-04 13:47:49 +08:00
|
|
|
module = import_module(module_path)
|
|
|
|
return getattr(module, class_name)
|
2021-08-31 16:14:04 +08:00
|
|
|
|
|
|
|
|
2014-01-21 04:15:14 +08:00
|
|
|
def import_string(dotted_path):
|
2013-02-03 05:58:02 +08:00
|
|
|
"""
|
|
|
|
Import a dotted module path and return the attribute/class designated by the
|
2014-01-21 04:15:14 +08:00
|
|
|
last name in the path. Raise ImportError if the import failed.
|
2013-02-03 05:58:02 +08:00
|
|
|
"""
|
|
|
|
try:
|
|
|
|
module_path, class_name = dotted_path.rsplit(".", 1)
|
2017-01-08 03:13:29 +08:00
|
|
|
except ValueError as err:
|
|
|
|
raise ImportError("%s doesn't look like a module path" % dotted_path) from err
|
2014-01-21 04:15:14 +08:00
|
|
|
|
2013-02-03 05:58:02 +08:00
|
|
|
try:
|
2021-08-31 16:14:04 +08:00
|
|
|
return cached_import(module_path, class_name)
|
2017-01-08 03:13:29 +08:00
|
|
|
except AttributeError as err:
|
|
|
|
raise ImportError(
|
|
|
|
'Module "%s" does not define a "%s" attribute/class'
|
2015-06-03 23:05:03 +08:00
|
|
|
% (module_path, class_name)
|
2017-01-08 03:13:29 +08:00
|
|
|
) from err
|
2014-01-21 04:15:14 +08:00
|
|
|
|
|
|
|
|
2013-09-07 07:23:25 +08:00
|
|
|
def autodiscover_modules(*args, **kwargs):
|
|
|
|
"""
|
|
|
|
Auto-discover INSTALLED_APPS modules and fail silently when
|
|
|
|
not present. This forces an import on them to register any admin bits they
|
|
|
|
may want.
|
|
|
|
|
|
|
|
You may provide a register_to keyword parameter as a way to access a
|
|
|
|
registry. This register_to object must have a _registry instance variable
|
|
|
|
to access it.
|
|
|
|
"""
|
2013-12-24 19:25:17 +08:00
|
|
|
from django.apps import apps
|
2013-09-07 07:23:25 +08:00
|
|
|
|
|
|
|
register_to = kwargs.get("register_to")
|
2013-12-24 19:25:17 +08:00
|
|
|
for app_config in apps.get_app_configs():
|
2014-10-31 17:22:15 +08:00
|
|
|
for module_to_search in args:
|
|
|
|
# Attempt to import the app's module.
|
|
|
|
try:
|
|
|
|
if register_to:
|
|
|
|
before_import_registry = copy.copy(register_to._registry)
|
2013-09-07 07:23:25 +08:00
|
|
|
|
2013-12-19 22:57:23 +08:00
|
|
|
import_module("%s.%s" % (app_config.name, module_to_search))
|
2015-11-16 02:52:47 +08:00
|
|
|
except Exception:
|
2014-10-31 17:22:15 +08:00
|
|
|
# Reset the registry to the state before the last import
|
|
|
|
# as this import will have to reoccur on the next request and
|
|
|
|
# this could raise NotRegistered and AlreadyRegistered
|
|
|
|
# exceptions (see #8245).
|
|
|
|
if register_to:
|
|
|
|
register_to._registry = before_import_registry
|
|
|
|
|
|
|
|
# Decide whether to bubble up this error. If the app just
|
|
|
|
# doesn't have the module in question, we can ignore the error
|
|
|
|
# attempting to import it, otherwise we want it to bubble up.
|
|
|
|
if module_has_submodule(app_config.module, module_to_search):
|
|
|
|
raise
|
2013-09-07 07:23:25 +08:00
|
|
|
|
|
|
|
|
2016-12-01 18:38:01 +08:00
|
|
|
def module_has_submodule(package, module_name):
|
|
|
|
"""See if 'module' is in 'package'."""
|
|
|
|
try:
|
|
|
|
package_name = package.__name__
|
|
|
|
package_path = package.__path__
|
|
|
|
except AttributeError:
|
|
|
|
# package isn't a package.
|
|
|
|
return False
|
|
|
|
|
|
|
|
full_module_name = package_name + "." + module_name
|
2017-06-09 02:34:20 +08:00
|
|
|
try:
|
|
|
|
return importlib_find(full_module_name, package_path) is not None
|
2021-01-19 15:35:16 +08:00
|
|
|
except ModuleNotFoundError:
|
2019-01-18 23:04:29 +08:00
|
|
|
# When module_name is an invalid dotted path, Python raises
|
2021-01-19 15:35:16 +08:00
|
|
|
# ModuleNotFoundError.
|
2017-06-09 02:34:20 +08:00
|
|
|
return False
|
2015-02-23 04:18:12 +08:00
|
|
|
|
|
|
|
|
|
|
|
def module_dir(module):
|
|
|
|
"""
|
|
|
|
Find the name of the directory that contains a module, if possible.
|
|
|
|
|
|
|
|
Raise ValueError otherwise, e.g. for namespace packages that are split
|
|
|
|
over several directories.
|
|
|
|
"""
|
2021-03-26 17:14:07 +08:00
|
|
|
# Convert to list because __path__ may not support indexing.
|
2015-02-23 04:18:12 +08:00
|
|
|
paths = list(getattr(module, "__path__", []))
|
|
|
|
if len(paths) == 1:
|
|
|
|
return paths[0]
|
|
|
|
else:
|
|
|
|
filename = getattr(module, "__file__", None)
|
|
|
|
if filename is not None:
|
|
|
|
return os.path.dirname(filename)
|
|
|
|
raise ValueError("Cannot determine directory containing %s" % module)
|