pytilities.event.restricteddispatcher

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      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):
     60          if not self.has_event(event_name):
     61              raise UnsupportedEventError(event_name)
     62
     63          self.__dispatcher.add_handler(event_name, handler, owner)
     64
     65 -    def remove_handlers(self, event_name=None, owner=None):
     66          # pass on the call, making sure we don't remove handlers for events we
     67          # don't support
     68          if event_name:
     69              if not self.has_event(event_name):
     70                  raise UnsupportedEventError(event_name)
     71
     72              self.__dispatcher.remove_handlers(event_name, owner)
     73          else:
     74              for event in self.events:
     75                  self.__dispatcher.remove_handlers(event, owner)
     76
     77 -    def remove_handler(self, event_name, handler, owner = None):
     78          if not self.has_event(event_name):
     79              raise UnsupportedEventError(event_name)
     80
     81          self.__dispatcher.remove_handler(event_name, handler, owner)
     82
     83 -    def dispatch(self, event_name, *args, **keyword_args):
     84          if not self.has_event(event_name):
     85              raise UnsupportedEventError(event_name)
     86
     87          self.__dispatcher.dispatch(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
     97 -    def events(self):
     98          if self.__complement:
     99              return self.__dispatcher.events - self.__allowed_events
    100          else:
    101              return self.__dispatcher.events & self.__allowed_events
    102
    103 -    def has_event(self, event_name):
    104          return (((event_name in self.__allowed_events) ^ self.__complement)
    105                  and self.__dispatcher.has_event(event_name))
    106