Package pytilities :: Package event :: Module restricteddispatcher
[hide private]
[frames] | no frames]

Source Code for Module pytilities.event.restricteddispatcher

  1  # Copyright (C) 2010 Tim Diels <limyreth@users.sourceforge.net> 
  2  #  
  3  # This file is part of pytilities. 
  4  #  
  5  # pytilities is free software: you can redistribute it and/or modify 
  6  # it under the terms of the GNU General Public License as published by 
  7  # the Free Software Foundation, either version 3 of the License, or 
  8  # (at your option) any later version. 
  9  #  
 10  # pytilities is distributed in the hope that it will be useful, 
 11  # but WITHOUT ANY WARRANTY; without even the implied warranty of 
 12  # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the 
 13  # GNU General Public License for more details. 
 14  #  
 15  # You should have received a copy of the GNU General Public License 
 16  # along with pytilities.  If not, see <http://www.gnu.org/licenses/>. 
 17  # 
 18   
 19  __docformat__ = 'reStructuredText' 
 20   
 21  from . import UnsupportedEventError 
22 23 -class RestrictedDispatcher(object):
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):
62 if not self.has_event(event_name): 63 raise UnsupportedEventError(event_name) 64 65 self.__dispatcher.add_handler(event_name, handler, owner)
66
67 - def remove_handlers(self, event_name=None, owner=None):
68 # pass on the call, making sure we don't remove handlers for events we 69 # don't support 70 if event_name: 71 if not self.has_event(event_name): 72 raise UnsupportedEventError(event_name) 73 74 self.__dispatcher.remove_handlers(event_name, owner) 75 else: 76 for event in self.events: 77 self.__dispatcher.remove_handlers(event, owner)
78
79 - def remove_handler(self, event_name, handler, owner = None):
80 if not self.has_event(event_name): 81 raise UnsupportedEventError(event_name) 82 83 self.__dispatcher.remove_handler(event_name, handler, owner)
84
85 - def dispatch(self, event_name, *args, **keyword_args):
86 if not self.has_event(event_name): 87 raise UnsupportedEventError(event_name) 88 89 self.__dispatcher.dispatch(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
99 - def events(self):
100 return (self.__dispatcher.events - self.__disallowed_events) \ 101 & self.__allowed_events
102
103 - def has_event(self, event_name):
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