1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 __docformat__ = 'reStructuredText'
20
21 from . import UnsupportedEventError
24
25 """
26 `Dispatcher` wrapper that filters the list of supported events.
27
28 Check `Dispatcher` for the documentation of the methods that are left
29 undocumented here.
30 """
31
32 - def __init__(self, dispatcher, allow = None, disallow = None):
33 """
34 Construct a restricted dispatcher.
35
36 `disallow` and `allow` are mutually exclusive.
37
38 Parameters:
39
40 dispatcher :: Dispatcher
41 the dispatcher to wrap around
42
43 allow :: iter(string...):
44 events that are allowed through
45
46 disallow :: iter(string...)
47 events that are hidden/blocked
48 """
49
50 assert dispatcher, "dispatcher musn't be None"
51 assert not (allow and disallow), \
52 'disallow and allow are mutually exclusive'
53
54 self.__dispatcher = dispatcher
55 self.__allowed_events = set(allow or disallow or ())
56 self.__complement = not allow
57 '''True, if allowed_events contains the complement of allowed events'''
58
59 - def add_handler(self, event_name, handler, owner = None):
64
76
82
83 - def dispatch(self, event_name, *args, **keyword_args):
88
89 - def event(self, event_name, owner=None):
90 def decorator(handler):
91 self.add_handler(event_name, handler, owner)
92 return handler
93
94 return decorator
95
96 @property
98 if self.__complement:
99 return self.__dispatcher.events - self.__allowed_events
100 else:
101 return self.__dispatcher.events & self.__allowed_events
102
104 return (((event_name in self.__allowed_events) ^ self.__complement)
105 and self.__dispatcher.has_event(event_name))
106