module_import_helper.py 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. import importlib.util
  2. import logging
  3. import sys
  4. from types import ModuleType
  5. from typing import AnyStr
  6. def import_module_from_source(*, module_name: str, py_file_path: AnyStr, use_lazy_loader: bool = False) -> ModuleType:
  7. """
  8. Importing a module from the source file directly
  9. """
  10. try:
  11. existed_spec = importlib.util.find_spec(module_name)
  12. if existed_spec:
  13. spec = existed_spec
  14. if not spec.loader:
  15. raise Exception(f"Failed to load module {module_name} from {py_file_path!r}")
  16. else:
  17. # Refer to: https://docs.python.org/3/library/importlib.html#importing-a-source-file-directly
  18. # FIXME: mypy does not support the type of spec.loader
  19. spec = importlib.util.spec_from_file_location(module_name, py_file_path) # type: ignore
  20. if not spec or not spec.loader:
  21. raise Exception(f"Failed to load module {module_name} from {py_file_path!r}")
  22. if use_lazy_loader:
  23. # Refer to: https://docs.python.org/3/library/importlib.html#implementing-lazy-imports
  24. spec.loader = importlib.util.LazyLoader(spec.loader)
  25. module = importlib.util.module_from_spec(spec)
  26. if not existed_spec:
  27. sys.modules[module_name] = module
  28. spec.loader.exec_module(module)
  29. return module
  30. except Exception as e:
  31. logging.exception(f"Failed to load module {module_name} from script file '{py_file_path!r}'")
  32. raise e
  33. def get_subclasses_from_module(mod: ModuleType, parent_type: type) -> list[type]:
  34. """
  35. Get all the subclasses of the parent type from the module
  36. """
  37. classes = [
  38. x for _, x in vars(mod).items() if isinstance(x, type) and x != parent_type and issubclass(x, parent_type)
  39. ]
  40. return classes
  41. def load_single_subclass_from_source(
  42. *, module_name: str, script_path: AnyStr, parent_type: type, use_lazy_loader: bool = False
  43. ) -> type:
  44. """
  45. Load a single subclass from the source
  46. """
  47. module = import_module_from_source(
  48. module_name=module_name, py_file_path=script_path, use_lazy_loader=use_lazy_loader
  49. )
  50. subclasses = get_subclasses_from_module(module, parent_type)
  51. match len(subclasses):
  52. case 1:
  53. return subclasses[0]
  54. case 0:
  55. raise Exception(f"Missing subclass of {parent_type.__name__} in {script_path!r}")
  56. case _:
  57. raise Exception(f"Multiple subclasses of {parent_type.__name__} in {script_path!r}")