module_loading.py 3.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. import copy
  2. import os
  3. from importlib import import_module
  4. from importlib.util import find_spec as importlib_find
  5. def import_string(dotted_path):
  6. """
  7. Import a dotted module path and return the attribute/class designated by the
  8. last name in the path. Raise ImportError if the import failed.
  9. """
  10. try:
  11. module_path, class_name = dotted_path.rsplit('.', 1)
  12. except ValueError as err:
  13. raise ImportError("%s doesn't look like a module path" % dotted_path) from err
  14. module = import_module(module_path)
  15. try:
  16. return getattr(module, class_name)
  17. except AttributeError as err:
  18. raise ImportError('Module "%s" does not define a "%s" attribute/class' % (
  19. module_path, class_name)
  20. ) from err
  21. def autodiscover_modules(*args, **kwargs):
  22. """
  23. Auto-discover INSTALLED_APPS modules and fail silently when
  24. not present. This forces an import on them to register any admin bits they
  25. may want.
  26. You may provide a register_to keyword parameter as a way to access a
  27. registry. This register_to object must have a _registry instance variable
  28. to access it.
  29. """
  30. from django.apps import apps
  31. register_to = kwargs.get('register_to')
  32. for app_config in apps.get_app_configs():
  33. for module_to_search in args:
  34. # Attempt to import the app's module.
  35. try:
  36. if register_to:
  37. before_import_registry = copy.copy(register_to._registry)
  38. import_module('%s.%s' % (app_config.name, module_to_search))
  39. except Exception:
  40. # Reset the registry to the state before the last import
  41. # as this import will have to reoccur on the next request and
  42. # this could raise NotRegistered and AlreadyRegistered
  43. # exceptions (see #8245).
  44. if register_to:
  45. register_to._registry = before_import_registry
  46. # Decide whether to bubble up this error. If the app just
  47. # doesn't have the module in question, we can ignore the error
  48. # attempting to import it, otherwise we want it to bubble up.
  49. if module_has_submodule(app_config.module, module_to_search):
  50. raise
  51. def module_has_submodule(package, module_name):
  52. """See if 'module' is in 'package'."""
  53. try:
  54. package_name = package.__name__
  55. package_path = package.__path__
  56. except AttributeError:
  57. # package isn't a package.
  58. return False
  59. full_module_name = package_name + '.' + module_name
  60. try:
  61. return importlib_find(full_module_name, package_path) is not None
  62. except (ModuleNotFoundError, AttributeError):
  63. # When module_name is an invalid dotted path, Python raises
  64. # ModuleNotFoundError. AttributeError is raised on PY36 (fixed in PY37)
  65. # if the penultimate part of the path is not a package.
  66. return False
  67. def module_dir(module):
  68. """
  69. Find the name of the directory that contains a module, if possible.
  70. Raise ValueError otherwise, e.g. for namespace packages that are split
  71. over several directories.
  72. """
  73. # Convert to list because _NamespacePath does not support indexing.
  74. paths = list(getattr(module, '__path__', []))
  75. if len(paths) == 1:
  76. return paths[0]
  77. else:
  78. filename = getattr(module, '__file__', None)
  79. if filename is not None:
  80. return os.path.dirname(filename)
  81. raise ValueError("Cannot determine directory containing %s" % module)