__init__.py 1.1 KB

12345678910111213141516171819202122232425262728293031
  1. import os
  2. from loguru import logger
  3. APHRODITE_PLUGINS = None if "APHRODITE_PLUGINS" not in os.environ else \
  4. os.environ["APHRODITE_PLUGINS"].split(",")
  5. def load_general_plugins():
  6. """WARNING: plugins can be loaded for multiple times in different
  7. processes. They should be designed in a way that they can be loaded
  8. multiple times without causing issues.
  9. """
  10. import sys
  11. if sys.version_info < (3, 10):
  12. from importlib_metadata import entry_points
  13. else:
  14. from importlib.metadata import entry_points
  15. allowed_plugins = APHRODITE_PLUGINS
  16. discovered_plugins = entry_points(group='aphrodite.general_plugins')
  17. for plugin in discovered_plugins:
  18. logger.info(f"Found general plugin: {plugin.name}")
  19. if allowed_plugins is None or plugin.name in allowed_plugins:
  20. try:
  21. func = plugin.load()
  22. func()
  23. logger.info(f"Loaded general plugin: {plugin.name}")
  24. except Exception:
  25. logger.exception("Failed to load general plugin: "
  26. f"{plugin.name}")