|
From: Rowland S. <mon...@us...> - 2004-05-02 16:32:32
|
Update of /cvsroot/pythoncard/PythonCard In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv13195 Modified Files: component.py Log Message: Copied model.Scriptable to component.Scriptable. Changing design to encapsulate behavior to execute a handler inside the Scriptable class. Index: component.py =================================================================== RCS file: /cvsroot/pythoncard/PythonCard/component.py,v retrieving revision 1.9 retrieving revision 1.10 diff -C2 -d -r1.9 -r1.10 *** component.py 21 Apr 2004 16:35:03 -0000 1.9 --- component.py 2 May 2004 16:32:23 -0000 1.10 *************** *** 4,9 **** """ ! import event, registry class AttributeSpec : --- 4,149 ---- """ ! import event, registry, log ! import inspect ! from PythonCard.event import EventLog ! ! class Scriptable: ! """ ! RDS - 2004-05-02 ! A new Scriptable that will Component will inherit from. ! In this design, the Scriptable is responsible for ! execution of it's handlers. ! ! All classes that may contain PythonCard Handler definitions ! must implement Scriptable. ! ! A Scriptable object may be specified as the parent of ! this object. The parent will be searched for Handlers ! if a Handler can't be found in this object. ! """ ! ! def __init__(self, aScriptableParent): ! self._parentScript = aScriptableParent ! self._handlers = {} ! self._parseHandlers() ! if False: ! print "Scriptable init", self._handlers.keys() ! ! def _parseHandlers(self): ! """ ! Find all of the methods in this object that are ! PythonCard handlers, and register them. ! """ ! ! # KEA 2004-03-05 ! # an even slicker way of finding event handlers ! # using the inspect module ! pythoncardMethods = [] ! methods = inspect.getmembers(self.__class__, inspect.ismethod) ! for m in methods: ! if m[0].split('_')[0] == 'on': ! pythoncardMethods.append(m[1]) ! print pythoncardMethods ! ! map(self._addHandler, pythoncardMethods) ! ! def _isPythonCardHandler(self, aObject): ! """ ! Return true if the object is a PythonCard handler. ! """ ! print 'found handler', aObject ! return isinstance(aObject, types.FunctionType) and aObject.__name__.split('_')[0] == 'on' ! ! def _addHandler(self, aMethod): ! # Add the Handler to our Handler list. ! if aMethod.__name__ not in self._handlers: ! log.debug("_addHandler: " + aMethod.__name__) ! self._handlers[aMethod.__name__] = event.Handler(aMethod) ! ! """ ! RDS - Never gets called - Remove. ! ! def addMethod(self, aFunction): ! if isinstance(aFunction, types.FunctionType): ! if self._isPythonCardHandler(aFunction) : ! #aMethod = new.instancemethod(aFunction, self, self.__class__) ! aMethod = new.instancemethod(aFunction, None, self.__class__) ! #print aFunction ! #print self.__class__ ! #print aMethod.__name__ ! #print aMethod ! self.__class__.__dict__[aMethod.__name__] = aMethod ! # now add the method info to our handler lookup dictionary ! # KEA 2001-11-29 simplified _addHandler ! #handler = event.Handler(aMethod.__name__, aMethod) ! #self._addHandler(aMethod.__name__, handler) ! self._addHandler(aMethod) ! """ ! ! def _findHandler(self, aString): ! """ ! Look for a Handler that matches aString in our ! list of Handlers. If a Handler is not found, ! ask our parent script to look for the Handler, ! continuing up the Scriptable hierarchy until ! either a Handler is found, or None is returned. ! """ ! # KEA 2004-04-26 ! # findHandler is actually called for each event dispatch ! # depending on the level of dynamic code we think we're going ! # to have it might be simpler to just statically bind when ! # the component is created ! if True: ! print "findHandler", aString, self ! handler = self._handlers.get(aString, None) + if handler: + return self, handler + + # We couldn't find a handler, so look in our parent. + if self._parentScript: + print 'looking for handler in parent', self._parentScript + script, handler = self._parentScript._findHandler( aString ) + + # have we found a Handler yet? + if handler: + return script, handler + + # Change the handler name to target this Scriptable object + # and look in our list of Handlers. + + words = aString.split('_') + #print words, self._getName() + #if len(words) == 2: + # print words + # aString = words[ 0 ] + '_' + words[ 1 ] + #else: + aString = words[0] + '_' + self.getName() + '_' + words[len(words) - 1] + temp = self._handlers.get(aString, None) + if temp: + return ( self, temp ) + else: + # search for Background and Stack handlers like + # on_mouseClick, on_initialize + aString = words[0] + '_' + words[len(words) - 1] + return self._handlers.get(aString, None) + + def execute( self, handlerName, event ) : + """ + RDS - 2004-05-02 + Find the handler that matches handlerName and + execute it. + Should we throw an exception if the handlerName + is not found? The caller would be responsible + for reporting a missing handler as an error. + """ + print 'finding handler', handlerName + script, handler = self._findHandler( handlerName ) + if handler is not None : + print script, handler + handler.execute( script, event ) + EventLog.getInstance().log( event, handler.getSourceName(), True ) + + class AttributeSpec : |