##// END OF EJS Templates
Add docstrings for callbacks
Thomas Kluyver -
Show More
@@ -1,25 +1,74 b''
1 """Infrastructure for registering and firing callbacks.
2
3 Unlike :mod:`IPython.core.hooks`, which lets end users set single functions to
4 be called at specific times, or a collection of alternative methods to try,
5 callbacks are designed to be used by extension authors. A number of callbacks
6 can be registered for the same event without needing to be aware of one another.
7
8 The functions defined in this module are no-ops indicating the names of available
9 events and the arguments which will be passed to them.
10 """
1 from __future__ import print_function
11 from __future__ import print_function
2
12
3 class CallbackManager(object):
13 class CallbackManager(object):
14 """Manage a collection of events and a sequence of callbacks for each.
15
16 This is attached to :class:`~IPython.core.interactiveshell.InteractiveShell`
17 instances as a ``callbacks`` attribute.
18 """
4 def __init__(self, shell, available_callbacks):
19 def __init__(self, shell, available_callbacks):
20 """Initialise the :class:`CallbackManager`.
21
22 Parameters
23 ----------
24 shell
25 The :class:`~IPython.core.interactiveshell.InteractiveShell` instance
26 available_callbacks
27 An iterable of names for callback events.
28 """
5 self.shell = shell
29 self.shell = shell
6 self.callbacks = {n:[] for n in available_callbacks}
30 self.callbacks = {n:[] for n in available_callbacks}
7
31
8 def register(self, name, function):
32 def register(self, name, function):
33 """Register a new callback
34
35 Parameters
36 ----------
37 name : str
38 The event for which to register this callback.
39 function : callable
40 A function to be called on the given event. It should take the same
41 parameters as the appropriate callback prototype.
42
43 Raises
44 ------
45 TypeError
46 If ``function`` is not callable.
47 KeyError
48 If ``name`` is not one of the known callback events.
49 """
9 if not callable(function):
50 if not callable(function):
10 raise TypeError('Need a callable, got %r' % function)
51 raise TypeError('Need a callable, got %r' % function)
11 self.callbacks[name].append(function)
52 self.callbacks[name].append(function)
12
53
13 def unregister(self, name, function):
54 def unregister(self, name, function):
55 """Remove a callback from the given event."""
14 self.callbacks[name].remove(function)
56 self.callbacks[name].remove(function)
15
57
16 def reset(self, name):
58 def reset(self, name):
59 """Clear all callbacks for the given event."""
17 self.callbacks[name] = []
60 self.callbacks[name] = []
18
61
19 def reset_all(self):
62 def reset_all(self):
63 """Clear all callbacks for all events."""
20 self.callbacks = {n:[] for n in self.callbacks}
64 self.callbacks = {n:[] for n in self.callbacks}
21
65
22 def fire(self, name, *args, **kwargs):
66 def fire(self, name, *args, **kwargs):
67 """Call callbacks for the event ``name``.
68
69 Any additional arguments are passed to all callbacks registered for this
70 event. Exceptions raised by callbacks are caught, and a message printed.
71 """
23 for func in self.callbacks[name]:
72 for func in self.callbacks[name]:
24 try:
73 try:
25 func(*args, **kwargs)
74 func(*args, **kwargs)
@@ -27,16 +76,25 b' class CallbackManager(object):'
27 print("Error in callback {} (for {}):".format(func, name))
76 print("Error in callback {} (for {}):".format(func, name))
28 self.shell.showtraceback()
77 self.shell.showtraceback()
29
78
79 # event_name -> prototype mapping
30 available_callbacks = {}
80 available_callbacks = {}
81
31 def _collect(callback_proto):
82 def _collect(callback_proto):
32 available_callbacks[callback_proto.__name__] = callback_proto
83 available_callbacks[callback_proto.__name__] = callback_proto
33 return callback_proto
84 return callback_proto
34
85
86 # ------------------------------------------------------------------------------
87 # Callback prototypes
88 #
89 # No-op functions which describe the names of available events and the
90 # signatures of callbacks for those events.
91 # ------------------------------------------------------------------------------
92
35 @_collect
93 @_collect
36 def pre_execute():
94 def pre_execute():
37 """Fires before code is executed in response to user/frontend action.
95 """Fires before code is executed in response to user/frontend action.
38
96
39 This includes comm and widget messages."""
97 This includes comm and widget messages as well as user code cells."""
40 pass
98 pass
41
99
42 @_collect
100 @_collect
@@ -48,7 +106,7 b' def pre_execute_explicit():'
48 def post_execute():
106 def post_execute():
49 """Fires after code is executed in response to user/frontend action.
107 """Fires after code is executed in response to user/frontend action.
50
108
51 This includes comm and widget messages."""
109 This includes comm and widget messages as well as user code cells."""
52 pass
110 pass
53
111
54 @_collect
112 @_collect
General Comments 0
You need to be logged in to leave comments. Login now