[Top][All Lists]
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[gnue] r8094 - trunk/gnue-common/src/events
From: |
johannes |
Subject: |
[gnue] r8094 - trunk/gnue-common/src/events |
Date: |
Tue, 8 Nov 2005 04:46:19 -0600 (CST) |
Author: johannes
Date: 2005-11-08 04:46:18 -0600 (Tue, 08 Nov 2005)
New Revision: 8094
Modified:
trunk/gnue-common/src/events/Event.py
trunk/gnue-common/src/events/EventAware.py
trunk/gnue-common/src/events/EventController.py
trunk/gnue-common/src/events/__init__.py
Log:
Code clean-up (according to technote 15)
Modified: trunk/gnue-common/src/events/Event.py
===================================================================
--- trunk/gnue-common/src/events/Event.py 2005-11-04 02:37:33 UTC (rev
8093)
+++ trunk/gnue-common/src/events/Event.py 2005-11-08 10:46:18 UTC (rev
8094)
@@ -1,6 +1,9 @@
+# GNU Enterprise Common Library - Events framework - Events
#
-# This file is part of GNU Enterprise.
+# Copyright 2001-2005 Free Software Foundation
#
+# This file is part of GNU Enterprise
+#
# GNU Enterprise is free software; you can redistribute it
# and/or modify it under the terms of the GNU General Public
# License as published by the Free Software Foundation; either
@@ -16,23 +19,18 @@
# write to the Free Software Foundation, Inc., 59 Temple Place
# - Suite 330, Boston, MA 02111-1307, USA.
#
-# Copyright 2000-2005 Free Software Foundation
-#
-# FILE:
-# Event.py
-#
-# DESCRIPTION:
-#
-# NOTES:
-#
+# $Id$
-#
-# Basic event passed between items
-#
-# If event = Event('MYEVENT',x=1, y=2, object=MyObject), then
-# event.event == 'MYEVENT', event.x = 1,
-# event.y == 2, and event.object == MyObject
-#
+"""
+Implementation of an event.
+"""
+
+__all__ = ['Event']
+
+# =============================================================================
+# Implementation of an Event
+# =============================================================================
+
class Event:
"""
An Event is the actual event object passed back and forth between the event
@@ -53,88 +51,132 @@
2
"""
- def __init__(self, event, data=None, **parms):
+ # ---------------------------------------------------------------------------
+ # Constructor
+ # ---------------------------------------------------------------------------
+
+ def __init__ (self, event, data = None, **parms):
"""
- @param event: The name of the event. GNUe event listeners
- register the names of the events they wish to
- receive.
- @type event: string
+ @param event: The name of the event. GNUe event listeners register the
+ names of the events they wish to receive.
@param data: B{OBSOLETE: Do not use}
@param parms: Allows the event to accept any argument names you like.
- Examples would be things like
- - caller=self
- - x=15
- - vals={'name':'Bob', 'title':'Mr'}
+ Examples would be things like
+ - caller=self
+ - x=15
+ - vals = {'name': 'Bob', 'title': 'Mr'}
"""
- self.__dict__.update(parms)
+ self.__dict__.update (parms)
+
# TODO: Get rid of data=
- self.data = data
+ self.data = data
- self.__event__ = event
- self.__result__ = None
- self.__dropped__ = 0
- self.__error__ = 0
+ self.__event__ = event
+ self.__result__ = None
+ self.__dropped__ = 0
+ self.__error__ = 0
self.__errortext__ = ""
- self.__after__ = []
+ self.__after__ = []
- def getResult(self):
+
+ # ---------------------------------------------------------------------------
+ # Nice string representation
+ # ---------------------------------------------------------------------------
+
+ def __repr__ (self):
+
+ return "<Event %s at %s>" % (self.__event__, id (self))
+
+
+ # ---------------------------------------------------------------------------
+ # Get the result value stored in the event
+ # ---------------------------------------------------------------------------
+
+ def getResult (self):
"""
Returns the result value stored in the event by the L{setResult} function.
+
@return: The result value of the event.
- @rtype: Any
"""
+
return self.__result__
- def setResult(self, value):
+
+ # ---------------------------------------------------------------------------
+ # Set the result value for the event
+ # ---------------------------------------------------------------------------
+
+ def setResult (self, value):
"""
- Can be used by an event listener to assign a return value to an event.
- The result will be returned by the event controller's
- L{dispatchEvent<gnue.common.events.EventController.EventController>}
function.
- It can also be obtained by the L{getResult} function.
+ Can be used by an event listener to assign a return value to an event. The
+ result will be returned by the event controller's
+ L{dispatchEvent<gnue.common.events.EventController.EventController>}
+ function. It can also be obtained by the L{getResult} function.
- @param value: The result value to assign to the event. It can
- be anything that is concidered valid python.
+ @param value: The result value to assign to the event. It can be anything
+ that is concidered valid python.
"""
+
self.__result__ = value
- def getEvent(self):
+
+ # ---------------------------------------------------------------------------
+ # Get the name of the event
+ # ---------------------------------------------------------------------------
+
+ def getEvent (self):
"""
Returns the name of the event.
+
@return: The name of the event
- @rtype: string
"""
+
return self.__event__
- def drop(self):
+
+ # ---------------------------------------------------------------------------
+ # Drop the event
+ # ---------------------------------------------------------------------------
+
+ def drop (self):
"""
- When called the event will be dropped. No additional
- event listeners will receive the event.
+ Drop the event; no additional event listeners will recieve the event.
"""
+
self.__dropped__ = 1
- def setError(self, text = ""):
+
+ # ---------------------------------------------------------------------------
+ # Set the event's error flag and drop the event
+ # ---------------------------------------------------------------------------
+
+ def setError (self, text = ""):
"""
- Sets the event error flag. Setting the error
- also causes the event to be dropped in the
- same mannor as the L{drop} function.
+ Set the event's error flag and drop it (see L{drop}).
@param text: Text describing the error.
- @type text: string
"""
- self.__error__ = 1
+
+ self.__error__ = 1
self.__errortext__ = text
- def dispatchAfter(self, *args, **parms):
+
+ # ---------------------------------------------------------------------------
+ # Add an event to the event queue
+ # ---------------------------------------------------------------------------
+
+ def dispatchAfter (self, *args, **parms):
"""
- Adds an event to the event queue that will be
- dispatched upon this events completion. The arguments
- to this function match those used in creating an event.
+ Adds an event to the event queue that will be dispatched upon this events
+ completion. The arguments to this function match those used in creating an
+ event.
Sample Usage:
>>> from gnue.common.events.Event import *
- >>> myEvent = Event('myEventName', color='Blue', x=1, y=2)
- >>> myEvent.dispatchAfter('theNextEvent',name='FSF')
+ >>> myEvent = Event ('myEventName', color = 'Blue', x = 1, y = 2)
+ >>> myEvent.dispatchAfter ('theNextEvent', name = 'FSF')
"""
- self.__after__.append((args, parms))
+
+ self.__after__.append ((args, parms))
Property changes on: trunk/gnue-common/src/events/Event.py
___________________________________________________________________
Name: svn:keywords
+ Id
Modified: trunk/gnue-common/src/events/EventAware.py
===================================================================
--- trunk/gnue-common/src/events/EventAware.py 2005-11-04 02:37:33 UTC (rev
8093)
+++ trunk/gnue-common/src/events/EventAware.py 2005-11-08 10:46:18 UTC (rev
8094)
@@ -1,6 +1,9 @@
+# GNU Enterprise Common Library - Events framework - Base classes
#
-# This file is part of GNU Enterprise.
+# Copyright 2001-2005 Free Software Foundation
#
+# This file is part of GNU Enterprise
+#
# GNU Enterprise is free software; you can redistribute it
# and/or modify it under the terms of the GNU General Public
# License as published by the Free Software Foundation; either
@@ -16,19 +19,18 @@
# write to the Free Software Foundation, Inc., 59 Temple Place
# - Suite 330, Boston, MA 02111-1307, USA.
#
-# Copyright 2000-2005 Free Software Foundation
-#
-# FILE:
-# EventAware.py
-#
-# DESCRIPTION:
-#
-# NOTES:
-#
+# $Id$
-#
+"""
+Base class for objects using events.
+"""
+
+__all__ = ['EventAware']
+
+# =============================================================================
# Base for an object that sends and receives events
-#
+# =============================================================================
+
class EventAware:
"""
The base class for an object that sends and receives events
@@ -36,8 +38,8 @@
Sample Usage::
from gnue.common import events
- class myClass(events.EventAware):
- def __init__(self)
+ class myClass (events.EventAware):
+ def __init__ (self)
self.eventController = events.EventController()
events.EventAware.__init__(self, self.eventController)
self.registerEventListeners( {
@@ -50,15 +52,13 @@
def eventTwoHandler(self, event)
print "My event is named ", event.getEvent()
-
"""
- def __init__(self, controller):
- self.__controller = controller
- self.dispatchEvent = controller.dispatchEvent
+ # ---------------------------------------------------------------------------
+ # Constructor
+ # ---------------------------------------------------------------------------
+
+ def __init__ (self, controller):
+
+ self.dispatchEvent = controller.dispatchEvent
self.registerEventListeners = controller.registerEventListeners
-
- def __del__(self):
- self.__controller = None
- self.dispatchEvent = None
- self.registerEventListeners = None
Property changes on: trunk/gnue-common/src/events/EventAware.py
___________________________________________________________________
Name: svn:keywords
+ Id
Modified: trunk/gnue-common/src/events/EventController.py
===================================================================
--- trunk/gnue-common/src/events/EventController.py 2005-11-04 02:37:33 UTC
(rev 8093)
+++ trunk/gnue-common/src/events/EventController.py 2005-11-08 10:46:18 UTC
(rev 8094)
@@ -1,6 +1,9 @@
+# GNU Enterprise Common Library - Events framework - Event dispatching
#
-# This file is part of GNU Enterprise.
+# Copyright 2001-2005 Free Software Foundation
#
+# This file is part of GNU Enterprise
+#
# GNU Enterprise is free software; you can redistribute it
# and/or modify it under the terms of the GNU General Public
# License as published by the Free Software Foundation; either
@@ -16,100 +19,142 @@
# write to the Free Software Foundation, Inc., 59 Temple Place
# - Suite 330, Boston, MA 02111-1307, USA.
#
-# Copyright 2000-2005 Free Software Foundation
-#
-# FILE:
-# EventController.py
-#
-# DESCRIPTION:
-#
-# NOTES:
-#
+# $Id$
+"""
+Class for event dispatching.
+"""
+
+__all__ = ['EventController']
+
from EventAware import EventAware
from Event import Event
-class EventController(EventAware):
+
+# =============================================================================
+# Implmentation of an event dispatcher
+# =============================================================================
+
+class EventController:
"""
- An EventController is responsible for dispatching events to
- registered listeners.
+ An EventController is responsible for dispatching events to registered
+ listeners.
"""
- def __init__(self):
- # Note: Don't call EventAware.__init__
- # ... that would be fugly.
- # Store a dictionary of registered events
+ # ---------------------------------------------------------------------------
+ # Constructor
+ # ---------------------------------------------------------------------------
+
+ def __init__ (self):
+
self.__incomingEvents = {}
- def registerEventListeners(self, events):
+ # ---------------------------------------------------------------------------
+ # Nice string representation
+ # ---------------------------------------------------------------------------
+
+ def __repr__ (self):
+
+ return "<EventController at %s>" % (id (self))
+
+
+ # ---------------------------------------------------------------------------
+ # Register a listener for a specific event
+ # ---------------------------------------------------------------------------
+
+ def registerEventListeners (self, events):
"""
Registers an event listener for a specific event
@param events: A dictionary of {'eventName': listenerFuction} pairs
- @type events: dict
"""
- for event in events.keys():
- try:
- self.__incomingEvents[event].append(events[event])
- except KeyError:
- self.__incomingEvents[event] = [events[event]]
- def startCachingEvents(self):
+ checktype (events, dict)
+
+ for (event, listener) in events.items ():
+ self.__incomingEvents.setdefault (event, []).append (listener)
+
+
+ # ---------------------------------------------------------------------------
+ # Start storing events rather than dispatching them
+ # ---------------------------------------------------------------------------
+
+ def startCachingEvents (self):
"""
- Causes the event controller to start storing events
- rather than dispatching them. It will continue to
- store events until L{stopCachingEvents} is called.
+ Causes the event controller to start storing events rather than dispatching
+ them. It will continue to store events until L{stopCachingEvents} is
+ called.
"""
+
self.__cache = []
- def stopCachingEvents(self):
+
+ # ---------------------------------------------------------------------------
+ # Stop storing events
+ # ---------------------------------------------------------------------------
+
+ def stopCachingEvents (self):
"""
- Notifies the event controller that is should
- start processing events again. Any previously
- cached events are dispatched.
+ Notifies the event controller that is should start processing events again.
+ Any previously cached events are dispatched.
"""
+
cache = self.__cache
del self.__cache
+
for event, arg, parms in cache:
- self.dispatchEvent(event, *arg, **parms)
+ self.dispatchEvent (event, *arg, **parms)
- def dispatchEvent(self, event, *args, **parms):
+ # ---------------------------------------------------------------------------
+ # Dispatch or cache a given event
+ # ---------------------------------------------------------------------------
+
+ def dispatchEvent (self, event, *args, **parms):
+ """
+ Dispatch or cache the given event.
+
+ @params event: L{Event} object or text identifying the type of event to be
+ cached or dispatched.
+ @returns: the event object's __result__ attribute
+ """
+
# If we are caching our events, cache it:
try:
- self.__cache.append((event, args, parms))
+ self.__cache.append ((event, args, parms))
return
+
except AttributeError:
pass
- # Hackery so dispatchEvent can be passed
- # either an Event() object, or a text string
- # identifying the type of event. If the
- # latter, an event is created on the fly.
+ # Hackery so dispatchEvent can be passed either an Event() object, or a
+ # text string identifying the type of event. If the latter, an event is
+ # created on the fly.
try:
event.__event__
+
except:
- event = Event(event, *args, **parms)
+ event = Event (event, *args, **parms)
handlers = []
- if self.__incomingEvents.has_key(event.__event__):
- handlers = self.__incomingEvents[event.__event__]
+ if self.__incomingEvents.has_key (event.__event__):
+ handlers = self.__incomingEvents [event.__event__]
- if self.__incomingEvents.has_key('__before__'):
- handlers = self.__incomingEvents['__before__'] + handlers
+ if self.__incomingEvents.has_key ('__before__'):
+ handlers = self.__incomingEvents ['__before__'] + handlers
- if self.__incomingEvents.has_key('__after__'):
- handlers = handlers + self.__incomingEvents['__after__']
+ if self.__incomingEvents.has_key ('__after__'):
+ handlers = handlers + self.__incomingEvents ['__after__']
for handler in handlers:
- handler(event)
+ handler (event)
if event.__error__ or event.__dropped__:
break
# Fire any "dispatchAfter" events
for args, parms in event.__after__:
- self.dispatchEvent(*args, **parms)
+ self.dispatchEvent (*args, **parms)
return event.__result__
Property changes on: trunk/gnue-common/src/events/EventController.py
___________________________________________________________________
Name: svn:keywords
+ Id
Modified: trunk/gnue-common/src/events/__init__.py
===================================================================
--- trunk/gnue-common/src/events/__init__.py 2005-11-04 02:37:33 UTC (rev
8093)
+++ trunk/gnue-common/src/events/__init__.py 2005-11-08 10:46:18 UTC (rev
8094)
@@ -1,4 +1,30 @@
+# GNU Enterprise Common Library - Events framework
+#
+# Copyright 2001-2005 Free Software Foundation
+#
+# This file is part of GNU Enterprise
+#
+# GNU Enterprise is free software; you can redistribute it
+# and/or modify it under the terms of the GNU General Public
+# License as published by the Free Software Foundation; either
+# version 2, or (at your option) any later version.
+#
+# GNU Enterprise is distributed in the hope that it will be
+# useful, but WITHOUT ANY WARRANTY; without even the implied
+# warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
+# PURPOSE. See the GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public
+# License along with program; see the file COPYING. If not,
+# write to the Free Software Foundation, Inc., 59 Temple Place
+# - Suite 330, Boston, MA 02111-1307, USA.
+#
+# $Id$
+
+"""
+Framework for creating, passing and dispatching events.
+"""
+
from Event import Event
from EventAware import EventAware
from EventController import EventController
-
Property changes on: trunk/gnue-common/src/events/__init__.py
___________________________________________________________________
Name: svn:keywords
+ Id
[Prev in Thread] |
Current Thread |
[Next in Thread] |
- [gnue] r8094 - trunk/gnue-common/src/events,
johannes <=