clEvent.py

00001 #!/usr/bin/env python
00002 # -*- coding: utf-8 -*-
00003 
00004 # ----------------------------------------------------------------------------
00005 # pyjama - python jamendo audioplayer
00006 # Copyright (c) 2008 Daniel Nögel
00007 #
00008 # This program is free software: you can redistribute it and/or modify
00009 # it under the terms of the GNU General Public License as published by
00010 # the Free Software Foundation, either version 3 of the License, or
00011 # (at your option) any later version.
00012 #
00013 # This program is distributed in the hope that it will be useful,
00014 # but WITHOUT ANY WARRANTY; without even the implied warranty of
00015 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00016 # GNU General Public License for more details.
00017 # You should have received a copy of the GNU General Public License
00018 # along with this program.  If not, see <http://www.gnu.org/licenses/>.
00019 # ----------------------------------------------------------------------------
00020 
00021 # This Script was found on:
00022 # http://www.valuedlessons.com/2008/04/events-in-python.html
00023 
00024 
00025 ## @package clEvent
00026 # Pyjama's custom event module
00027 
00028 ## The EventHandler class represents the Event itself.
00029 # A EventHandler is created for earch plugin created.
00030 # Usually you do not have to use this class.
00031 # Please have a look at clEvent.Events
00032 class EventHandler:
00033     def __init__(self):
00034         ## List of functions which will be called by this EventHandler
00035         self.handlers = [] #set()
00036 
00037     ## Adds an function to the handler list
00038     def handle(self, handler):
00039         #self.handlers.insert(0,handler)
00040         self.handlers.append(handler)
00041         return self
00042 
00043     ## Removes a function from the handler list
00044     def unhandle(self, handler):
00045         try:
00046             self.handlers.remove(handler)
00047         except:
00048             raise ValueError("Handler is not handling this event, so cannot unhandle it.")
00049         return self
00050 
00051     ## Calls each function in the handler list
00052     # @param *args Arguments
00053     # @param **kargs Keyword arguments
00054     def fire(self, *args, **kargs):
00055         for handler in self.handlers:
00056 #            if "alldone" in str(handler): print handler
00057             handler(*args, **kargs)
00058 
00059     ## Returns the number of functions connected to this event
00060     def getHandlerCount(self):
00061         return len(self.handlers)
00062 
00063     __iadd__ = handle
00064     __isub__ = unhandle
00065     __call__ = fire
00066     __len__  = getHandlerCount
00067 
00068 ## Pyjama's event class is the EventHandler class interface
00069 class Events:
00070     def __init__(self):
00071 #        self.pluginloaded = EventHandler()
00072 #        self.nowplaying = EventHandler()
00073 #        self.alldone = EventHandler()
00074         ## A dictionary with all events created
00075         self.dict = {}
00076 
00077     ## Creates a new event
00078     # @param self Object Pointer
00079     # @param eventname The new event's name
00080     # @return None
00081     def add_event(self, eventname):
00082         self.dict[eventname] = EventHandler()
00083 
00084     ## Connects to an existing event
00085     # @param self Object Pointer
00086     # @param eventname Name of the event to connect to
00087     # @param fkt The function to call when this event is raised
00088     # @return None
00089     def connect_event(self, eventname, fkt):
00090         self.dict[eventname] += fkt
00091 
00092     ## Disconnects a function from an event
00093     # @param self Object Pointer
00094     # @param eventname Name of the event to disconnect from
00095     # @param fkt The function to disconnect
00096     # @return None
00097     def disconnect_event(self, eventname, fkt, *argv):
00098         self.dict[eventname] -= fkt
00099 
00100     ## Raises an event
00101     # @param self Object Pointer
00102     # @param eventname Name of the event to raise
00103     # @param *argv Arguments to pass to the connected functions
00104     # @param *kargs Keyword arguments to pass to the connected functions
00105     # @return None
00106     def raise_event(self, eventname, *argv, **kargs):
00107         self.dict[eventname](*argv, **kargs)
00108 

Generated on Thu Jun 4 19:08:24 2009 for Pyjama by  doxygen 1.5.8