pytilities.event.dispatcherswitch

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

# Source Code for Module pytilities.event.dispatcherswitch

      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 pytilities.delegation import (
     22      delegator_factory, delegated)
     23  from . import UnsupportedEventError
     24
     25  @delegator_factory()
     26 -class DispatcherSwitch(object):
     27
     28      """
     29      Provides a single interface to multiple event dispatchers.
     30
     31      Events are switched to the the first event dispatcher in a list of
     32      dispatchers that supports the event, the other dispatchers are ignored for
     33      that particular event.
     34
     35      This is also how remove_handlers will look for
     36      handlers, it will only remove the handlers of each event's first found
     37      dispatcher; the other ones are 'hidden' by this one, for this event.
     38
     39      Check `Dispatcher` for the documentation of the methods that are left
     40      undocumented here.
     41
     42      Instance methods:
     43
     44          - `append_dispatchers`: Add dispatchers to the list of dispatchers
     45          - ...
     46      """
     47      # TODO: find a way to copy those bits of doc to our methods here, but
     48      # first, wait for that reST python reader
     49
     50      # I wonder, it's really more of a router, isn't it
     51
     52      @staticmethod
     53 -    def __init_delegation_profiles(profiles):
     54          profiles['default'] |= profiles['public']
     55
     56 -    def __init__(self):
     57          self.__dispatchers = []
     58
     59 -    def append_dispatchers(self, *dispatchers):
     60          """
     61          Add dispatchers to the end of the list of dispatchers.
     62
     63          Parameters:
     64
     65              dispatchers :: (Dispatcher...)
     66                  sequence of dispatchers to append
     67          """
     68          self.__dispatchers.extend(dispatchers)
     69
     70 -    def __get_dispatcher_for_event(self, event_name):
     71          """
     72          Get the dispatcher for the given event according to our switching
     73          rules
     74          """
     75          for dispatcher in self.__dispatchers:
     76              if dispatcher.has_event(event_name):
     77                  return dispatcher
     78          else:
     79              raise UnsupportedEventError(event_name)
     80
     81      @delegated("public")
     82 -    def add_handler(self, event_name, handler, owner = None):
     83          dispatcher = self.__get_dispatcher_for_event(event_name)
     84          dispatcher.add_handler(event_name, handler, owner)
     85
     86      @delegated("public")
     87 -    def remove_handlers(self, event_name=None, owner=None):
     88          # Run through our supported events, remove all handlers for each
     89          # supported event on the first dispatcher that supports it
     90          if event_name:
     91              dispatcher = self.__get_dispatcher_for_event(event_name)
     92              dispatcher.remove_handlers(event_name, owner)
     93          else:
     94              for event in self.events:
     95                  dispatcher = self.__get_dispatcher_for_event(event)
     96                  dispatcher.remove_handlers(event, owner)
     97
     98      @delegated("public")
     99 -    def remove_handler(self, event_name, handler, owner = None):
    100          dispatcher = self.__get_dispatcher_for_event(event_name)
    101          dispatcher.remove_handler(event_name, handler, owner)
    102
    103      @delegated("public")
    104 -    def event(self, event_name, owner=None):
    105          def decorator(handler):
    106              self.add_handler(event_name, handler, owner)
    107              return handler
    108
    109          return decorator
    110
    111      @delegated("public")
    112 -    def has_event(self, event_name):
    113          for dispatcher in self.__dispatchers:
    114              if dispatcher.has_event(event_name):
    115                  return True
    116          else:
    117              return False
    118
    119      @delegated("public")
    120      @property
    121 -    def events(self):
    122          events = set()
    123          events.update(*(d.events for d in self.__dispatchers))
    124          return events
    125
    126 -    def dispatch(self, event_name, *args, **keyword_args):
    127          dispatcher = self.__get_dispatcher_for_event(event_name)
    128          dispatcher.dispatch(event_name, *args, **keyword_args)
    129