Funktionierender Prototyp des Serious Games zur Vermittlung von Wissen zu Software-Engineering-Arbeitsmodellen.
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.7KB

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