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 Filter rules (in the specified order):
29
30 - If it matches a disallow rule, it is not let through
31 - If there are no allow rules, all the rest is let through
32 - If there are allow rules, it must match an allow rule
33
34 Check `Dispatcher` for the documentation of the methods that are left
35 undocumented here.
36 """
37
38 - def __init__(self, dispatcher, allow = None, disallow = None):
39 """
40 Construct a restricted dispatcher.
41
42 `disallow` takes precedence over `allow_events`.
43
44 Parameters:
45
46 dispatcher :: Dispatcher
47 the dispatcher to wrap around
48
49 allow :: (string...):
50 a list of events that are allowed. If `None`, all are allowed,
51 unless they are in `disallow_events`
52
53 disallow :: (string...)
54 disallowed events
55 """
56 assert dispatcher, "dispatcher musn't be None"
57 self.__dispatcher = dispatcher
58 self.__allowed_events = set(allow or ())
59 self.__disallowed_events = set(disallow or ())
60
61 - def add_handler(self, event_name, handler, owner = None):
66
78
84
85 - def dispatch(self, event_name, *args, **keyword_args):
90
91 - def event(self, event_name, owner=None):
92 def decorator(handler):
93 self.add_handler(event_name, handler, owner)
94 return handler
95
96 return decorator
97
98 @property
100 return (self.__dispatcher.events - self.__disallowed_events) \
101 & self.__allowed_events
102
104 return (
105 not (self.__disallowed_events and
106 event_name in self.__disallowed_events) and
107 (not self.__allowed_events or
108 event_name in self.__allowed_events) and
109 self.__dispatcher.has_event(event_name)
110 )
111