您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

module_loading.py 1.6KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. """
  2. Module loading utilities similar to Django's module_loading.
  3. Reference implementation from Django:
  4. https://github.com/django/django/blob/main/django/utils/module_loading.py
  5. """
  6. import sys
  7. from importlib import import_module
  8. from typing import Any
  9. def cached_import(module_path: str, class_name: str) -> Any:
  10. """
  11. Import a module and return the named attribute/class from it, with caching.
  12. Args:
  13. module_path: The module path to import from
  14. class_name: The attribute/class name to retrieve
  15. Returns:
  16. The imported attribute/class
  17. """
  18. if not (
  19. (module := sys.modules.get(module_path))
  20. and (spec := getattr(module, "__spec__", None))
  21. and getattr(spec, "_initializing", False) is False
  22. ):
  23. module = import_module(module_path)
  24. return getattr(module, class_name)
  25. def import_string(dotted_path: str) -> Any:
  26. """
  27. Import a dotted module path and return the attribute/class designated by
  28. the last name in the path. Raise ImportError if the import failed.
  29. Args:
  30. dotted_path: Full module path to the class (e.g., 'module.submodule.ClassName')
  31. Returns:
  32. The imported class or attribute
  33. Raises:
  34. ImportError: If the module or attribute cannot be imported
  35. """
  36. try:
  37. module_path, class_name = dotted_path.rsplit(".", 1)
  38. except ValueError as err:
  39. raise ImportError(f"{dotted_path} doesn't look like a module path") from err
  40. try:
  41. return cached_import(module_path, class_name)
  42. except AttributeError as err:
  43. raise ImportError(f'Module "{module_path}" does not define a "{class_name}" attribute/class') from err