__init__.py 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. """Comm package.
  2. Copyright (c) IPython Development Team.
  3. Distributed under the terms of the Modified BSD License.
  4. This package provides a way to register a Kernel Comm implementation, as per
  5. the Jupyter kernel protocol.
  6. It also provides a base Comm implementation and a default CommManager for the IPython case.
  7. """
  8. from __future__ import annotations
  9. from typing import Any
  10. from .base_comm import BaseComm, BuffersType, CommManager, MaybeDict
  11. __version__ = "0.2.3"
  12. __all__ = [
  13. "__version__",
  14. "create_comm",
  15. "get_comm_manager",
  16. ]
  17. _comm_manager = None
  18. class DummyComm(BaseComm):
  19. def publish_msg(
  20. self,
  21. msg_type: str,
  22. data: MaybeDict = None,
  23. metadata: MaybeDict = None,
  24. buffers: BuffersType = None,
  25. **keys: Any,
  26. ) -> None:
  27. pass
  28. def _create_comm(*args: Any, **kwargs: Any) -> BaseComm:
  29. """Create a Comm.
  30. This method is intended to be replaced, so that it returns your Comm instance.
  31. """
  32. return DummyComm(*args, **kwargs)
  33. def _get_comm_manager() -> CommManager:
  34. """Get the current Comm manager, creates one if there is none.
  35. This method is intended to be replaced if needed (if you want to manage multiple CommManagers).
  36. """
  37. global _comm_manager # noqa: PLW0603
  38. if _comm_manager is None:
  39. _comm_manager = CommManager()
  40. return _comm_manager
  41. create_comm = _create_comm
  42. get_comm_manager = _get_comm_manager