Show More
@@ -13,8 +13,6 b' events and the arguments which will be passed to them.' | |||||
13 | This API is experimental in IPython 2.0, and may be revised in future versions. |
|
13 | This API is experimental in IPython 2.0, and may be revised in future versions. | |
14 | """ |
|
14 | """ | |
15 |
|
15 | |||
16 | from backcall import callback_prototype |
|
|||
17 |
|
||||
18 |
|
16 | |||
19 | class EventManager(object): |
|
17 | class EventManager(object): | |
20 | """Manage a collection of events and a sequence of callbacks for each. |
|
18 | """Manage a collection of events and a sequence of callbacks for each. | |
@@ -63,23 +61,14 b' class EventManager(object):' | |||||
63 | """ |
|
61 | """ | |
64 | if not callable(function): |
|
62 | if not callable(function): | |
65 | raise TypeError('Need a callable, got %r' % function) |
|
63 | raise TypeError('Need a callable, got %r' % function) | |
66 | callback_proto = available_events.get(event) |
|
|||
67 | if function not in self.callbacks[event]: |
|
64 | if function not in self.callbacks[event]: | |
68 |
self.callbacks[event].append( |
|
65 | self.callbacks[event].append(function) | |
69 |
|
66 | |||
70 | def unregister(self, event, function): |
|
67 | def unregister(self, event, function): | |
71 | """Remove a callback from the given event.""" |
|
68 | """Remove a callback from the given event.""" | |
72 | if function in self.callbacks[event]: |
|
69 | if function in self.callbacks[event]: | |
73 | return self.callbacks[event].remove(function) |
|
70 | return self.callbacks[event].remove(function) | |
74 |
|
71 | |||
75 | # Remove callback in case ``function`` was adapted by `backcall`. |
|
|||
76 | for callback in self.callbacks[event]: |
|
|||
77 | try: |
|
|||
78 | if callback.__wrapped__ is function: |
|
|||
79 | return self.callbacks[event].remove(callback) |
|
|||
80 | except AttributeError: |
|
|||
81 | pass |
|
|||
82 |
|
||||
83 | raise ValueError('Function {!r} is not registered as a {} callback'.format(function, event)) |
|
72 | raise ValueError('Function {!r} is not registered as a {} callback'.format(function, event)) | |
84 |
|
73 | |||
85 | def trigger(self, event, *args, **kwargs): |
|
74 | def trigger(self, event, *args, **kwargs): | |
@@ -100,9 +89,8 b' class EventManager(object):' | |||||
100 | available_events = {} |
|
89 | available_events = {} | |
101 |
|
90 | |||
102 | def _define_event(callback_function): |
|
91 | def _define_event(callback_function): | |
103 | callback_proto = callback_prototype(callback_function) |
|
92 | available_events[callback_function.__name__] = callback_function | |
104 | available_events[callback_function.__name__] = callback_proto |
|
93 | return callback_function | |
105 | return callback_proto |
|
|||
106 |
|
94 | |||
107 | # ------------------------------------------------------------------------------ |
|
95 | # ------------------------------------------------------------------------------ | |
108 | # Callback prototypes |
|
96 | # Callback prototypes |
@@ -76,16 +76,3 b' class CallbackTests(unittest.TestCase):' | |||||
76 | self.em.trigger('ping_received') |
|
76 | self.em.trigger('ping_received') | |
77 | self.assertEqual([True, True, False], invoked) |
|
77 | self.assertEqual([True, True, False], invoked) | |
78 | self.assertEqual([func3], self.em.callbacks['ping_received']) |
|
78 | self.assertEqual([func3], self.em.callbacks['ping_received']) | |
79 |
|
||||
80 | def test_ignore_event_arguments_if_no_argument_required(self): |
|
|||
81 | call_count = [0] |
|
|||
82 | def event_with_no_argument(): |
|
|||
83 | call_count[0] += 1 |
|
|||
84 |
|
||||
85 | self.em.register('event_with_argument', event_with_no_argument) |
|
|||
86 | self.em.trigger('event_with_argument', 'the argument') |
|
|||
87 | self.assertEqual(call_count[0], 1) |
|
|||
88 |
|
||||
89 | self.em.unregister('event_with_argument', event_with_no_argument) |
|
|||
90 | self.em.trigger('ping_received') |
|
|||
91 | self.assertEqual(call_count[0], 1) |
|
@@ -30,7 +30,6 b' python_requires = >=3.9' | |||||
30 | zip_safe = False |
|
30 | zip_safe = False | |
31 | install_requires = |
|
31 | install_requires = | |
32 | appnope; sys_platform == "darwin" |
|
32 | appnope; sys_platform == "darwin" | |
33 | backcall |
|
|||
34 | colorama; sys_platform == "win32" |
|
33 | colorama; sys_platform == "win32" | |
35 | decorator |
|
34 | decorator | |
36 | exceptiongroup; python_version<'3.11' |
|
35 | exceptiongroup; python_version<'3.11' |
General Comments 0
You need to be logged in to leave comments.
Login now