plugin_service.py 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. from collections.abc import Generator
  2. from mimetypes import guess_type
  3. from core.plugin.entities.plugin import PluginEntity
  4. from core.plugin.entities.plugin_daemon import InstallPluginMessage, PluginDaemonInnerError
  5. from core.plugin.manager.asset import PluginAssetManager
  6. from core.plugin.manager.debugging import PluginDebuggingManager
  7. from core.plugin.manager.plugin import PluginInstallationManager
  8. class PluginService:
  9. @staticmethod
  10. def get_plugin_debugging_key(tenant_id: str) -> str:
  11. manager = PluginDebuggingManager()
  12. return manager.get_debugging_key(tenant_id)
  13. @staticmethod
  14. def list_plugins(tenant_id: str) -> list[PluginEntity]:
  15. manager = PluginInstallationManager()
  16. return manager.list_plugins(tenant_id)
  17. @staticmethod
  18. def get_asset(tenant_id: str, asset_file: str) -> tuple[bytes, str]:
  19. manager = PluginAssetManager()
  20. # guess mime type
  21. mime_type, _ = guess_type(asset_file)
  22. return manager.fetch_asset(tenant_id, asset_file), mime_type or "application/octet-stream"
  23. @staticmethod
  24. def check_plugin_unique_identifier(tenant_id: str, plugin_unique_identifier: str) -> bool:
  25. manager = PluginInstallationManager()
  26. return manager.fetch_plugin_by_identifier(tenant_id, plugin_unique_identifier)
  27. @staticmethod
  28. def install_plugin_from_unique_identifier(tenant_id: str, plugin_unique_identifier: str) -> bool:
  29. manager = PluginInstallationManager()
  30. return manager.install_from_identifier(tenant_id, plugin_unique_identifier)
  31. @staticmethod
  32. def install_plugin_from_pkg(tenant_id: str, pkg: bytes) -> Generator[InstallPluginMessage, None, None]:
  33. manager = PluginInstallationManager()
  34. try:
  35. yield from manager.install_from_pkg(tenant_id, pkg)
  36. except PluginDaemonInnerError as e:
  37. yield InstallPluginMessage(event=InstallPluginMessage.Event.Error, data=str(e.message))