Manage a collection of events and a sequence of callbacks for each. This is attached to :class:`~IPython.core.interactiveshell.InteractiveShell` instances as an ``events`` attribute. .. note:: This API is experimental in IPython 2.0, and may be revised in future version
| 26 | |
| 27 | |
| 28 | class EventManager: |
| 29 | """Manage a collection of events and a sequence of callbacks for each. |
| 30 | |
| 31 | This is attached to :class:`~IPython.core.interactiveshell.InteractiveShell` |
| 32 | instances as an ``events`` attribute. |
| 33 | |
| 34 | .. note:: |
| 35 | |
| 36 | This API is experimental in IPython 2.0, and may be revised in future versions. |
| 37 | """ |
| 38 | |
| 39 | def __init__( |
| 40 | self, |
| 41 | shell: InteractiveShell, |
| 42 | available_events: Iterable[str], |
| 43 | print_on_error: bool = True, |
| 44 | ) -> None: |
| 45 | """Initialise the :class:`CallbackManager`. |
| 46 | |
| 47 | Parameters |
| 48 | ---------- |
| 49 | shell |
| 50 | The :class:`~IPython.core.interactiveshell.InteractiveShell` instance |
| 51 | available_events |
| 52 | An iterable of names for callback events. |
| 53 | print_on_error: |
| 54 | A boolean flag to set whether the EventManager will print a warning which a event errors. |
| 55 | """ |
| 56 | self.shell = shell |
| 57 | self.callbacks: dict[str, list[Callable[..., Any]]] = { |
| 58 | n: [] for n in available_events |
| 59 | } |
| 60 | self.print_on_error = print_on_error |
| 61 | |
| 62 | def register(self, event: str, function: Callable[..., Any]) -> None: |
| 63 | """Register a new event callback. |
| 64 | |
| 65 | Parameters |
| 66 | ---------- |
| 67 | event : str |
| 68 | The event for which to register this callback. |
| 69 | function : callable |
| 70 | A function to be called on the given event. It should take the same |
| 71 | parameters as the appropriate callback prototype. |
| 72 | |
| 73 | Raises |
| 74 | ------ |
| 75 | TypeError |
| 76 | If ``function`` is not callable. |
| 77 | KeyError |
| 78 | If ``event`` is not one of the known events. |
| 79 | """ |
| 80 | if not callable(function): |
| 81 | raise TypeError('Need a callable, got %r' % function) |
| 82 | if function not in self.callbacks[event]: |
| 83 | self.callbacks[event].append(function) |
| 84 | |
| 85 | def unregister(self, event: str, function: Callable[..., Any]) -> None: |
no outgoing calls
searching dependent graphs…