registry.py 970 B

12345678910111213141516171819202122232425262728293031323334353637383940
  1. """Plugin registry for zhuce6 platforms."""
  2. from __future__ import annotations
  3. import importlib
  4. import pkgutil
  5. from typing import Type
  6. from .base_platform import BasePlatform
  7. _registry: dict[str, Type[BasePlatform]] = {}
  8. def register(cls: Type[BasePlatform]) -> Type[BasePlatform]:
  9. _registry[cls.name] = cls
  10. return cls
  11. def load_all() -> None:
  12. import platforms
  13. for _, name, _ in pkgutil.iter_modules(platforms.__path__, platforms.__name__ + "."):
  14. try:
  15. importlib.import_module(f"{name}.plugin")
  16. except ModuleNotFoundError:
  17. continue
  18. def get(name: str) -> Type[BasePlatform]:
  19. if name not in _registry:
  20. raise KeyError(f"Unknown platform: {name}. Registered: {list(_registry)}")
  21. return _registry[name]
  22. def list_platforms() -> list[dict[str, str]]:
  23. return [
  24. {"name": cls.name, "display_name": cls.display_name, "version": cls.version}
  25. for cls in _registry.values()
  26. ]