module_import_helper.py 2.1 KB

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