Add catch-all '*' to event handlers
This commit is contained in:
parent
ef56620b6e
commit
8d9b6f19fd
@ -135,7 +135,10 @@ class Emitter(object):
|
|||||||
for cls in order:
|
for cls in order:
|
||||||
if not hasattr(cls, '__handlers__'):
|
if not hasattr(cls, '__handlers__'):
|
||||||
continue
|
continue
|
||||||
for func in sorted(cls.__handlers__[event],
|
handlers = cls.__handlers__[event]
|
||||||
|
if '*' in cls.__handlers__:
|
||||||
|
handlers = cls.__handlers__['*'] | handlers
|
||||||
|
for func in sorted(handlers,
|
||||||
key=(lambda handler: hasattr(handler, 'ha_bound')),
|
key=(lambda handler: hasattr(handler, 'ha_bound')),
|
||||||
reverse=True):
|
reverse=True):
|
||||||
effect = func(self, event, *args, **kwargs)
|
effect = func(self, event, *args, **kwargs)
|
||||||
|
@ -82,3 +82,27 @@ class TC_00_Emitter(qubes.tests.QubesTestCase):
|
|||||||
|
|
||||||
self.assertItemsEqual(effect,
|
self.assertItemsEqual(effect,
|
||||||
('testvalue1', 'testvalue2', 'testvalue3', 'testvalue4'))
|
('testvalue1', 'testvalue2', 'testvalue3', 'testvalue4'))
|
||||||
|
|
||||||
|
def test_004_catch_all(self):
|
||||||
|
# need something mutable
|
||||||
|
testevent_fired = [0]
|
||||||
|
|
||||||
|
def on_all(subject, event, *args, **kwargs):
|
||||||
|
# pylint: disable=unused-argument
|
||||||
|
testevent_fired[0] += 1
|
||||||
|
|
||||||
|
def on_foo(subject, event, *args, **kwargs):
|
||||||
|
# pylint: disable=unused-argument
|
||||||
|
testevent_fired[0] += 1
|
||||||
|
|
||||||
|
emitter = qubes.events.Emitter()
|
||||||
|
emitter.add_handler('*', on_all)
|
||||||
|
emitter.add_handler('foo', on_foo)
|
||||||
|
emitter.events_enabled = True
|
||||||
|
emitter.fire_event('testevent')
|
||||||
|
self.assertEqual(testevent_fired[0], 1)
|
||||||
|
emitter.fire_event('foo')
|
||||||
|
# now catch-all and foo should be executed
|
||||||
|
self.assertEqual(testevent_fired[0], 3)
|
||||||
|
emitter.fire_event('bar')
|
||||||
|
self.assertEqual(testevent_fired[0], 4)
|
||||||
|
Loading…
Reference in New Issue
Block a user