You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

module_loading.py 3.6KB

5 years ago
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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 (ImportError, AttributeError):
  63. # When module_name is an invalid dotted path, Python raises ImportError
  64. # (or ModuleNotFoundError in Python 3.6+). AttributeError may be raised
  65. # if the penultimate part of the path is not a package.
  66. # (https://bugs.python.org/issue30436)
  67. return False
  68. def module_dir(module):
  69. """
  70. Find the name of the directory that contains a module, if possible.
  71. Raise ValueError otherwise, e.g. for namespace packages that are split
  72. over several directories.
  73. """
  74. # Convert to list because _NamespacePath does not support indexing on 3.3.
  75. paths = list(getattr(module, '__path__', []))
  76. if len(paths) == 1:
  77. return paths[0]
  78. else:
  79. filename = getattr(module, '__file__', None)
  80. if filename is not None:
  81. return os.path.dirname(filename)
  82. raise ValueError("Cannot determine directory containing %s" % module)