[shpy-commit] r2878 - shpy/trunk/dist/shpy/ui_pygame

arigo@codespeak.net arigo@codespeak.net
Thu, 22 Jan 2004 19:16:15 +0100 (MET)


Author: arigo
Date: Thu Jan 22 19:16:01 2004
New Revision: 2878

Modified:
   shpy/trunk/dist/shpy/ui_pygame/decorate.py
Log:
Cache decorators.


Modified: shpy/trunk/dist/shpy/ui_pygame/decorate.py
==============================================================================
--- shpy/trunk/dist/shpy/ui_pygame/decorate.py	(original)
+++ shpy/trunk/dist/shpy/ui_pygame/decorate.py	Thu Jan 22 19:16:01 2004
@@ -8,7 +8,7 @@
     #slots = ()
 
     def __init__(self, **kwds):
-        if not hasattr(self.__class__, 'typeid2class'):
+        if not hasattr(self.__class__, '_typeid2class'):
             raise TypeError, 'cannot instantiate class (use collectdecorators)'
         _value = Structure(typeid = self.__class__.typeid)
         self.__dict__['_value'] = _value
@@ -47,14 +47,18 @@
 
     def decorate(cls, value):
         if isinstance(value, Structure):
-            typeid2class = cls.typeid2class
-            try:
-                classobj = typeid2class[value.typeid]
-            except KeyError:
-                raise DecorateError, 'no decorator for %r' % (value.typeid,)
-            decorator = new.instance(classobj, {'_value': value})
-            decorator.init_more()
-            return decorator
+            if value in cls._decoratorcache:
+                return cls._decoratorcache[value]
+            else:
+                typeid2class = cls._typeid2class
+                try:
+                    classobj = typeid2class[value.typeid]
+                except KeyError:
+                    raise DecorateError, 'no decorator for %r' % (value.typeid,)
+                decorator = new.instance(classobj, {'_value': value})
+                decorator.init_more()
+                cls._decoratorcache[value] = decorator
+                return decorator
         elif isinstance(value, ATOMIC):
             return value
         elif isinstance(value, tuple):
@@ -133,10 +137,12 @@
 def collectdecorators(changedict, classes):
     result = {}
     typeid2class = {}
+    decoratorcache = {}
     for existingcls in classes:
         newcls = new.classobj(existingcls.__name__, (existingcls,),
-                              {'typeid2class': typeid2class,
-                               '_changedict':  changedict})
+                              {'_typeid2class': typeid2class,
+                               '_changedict':  changedict,
+                               '_decoratorcache': decoratorcache})
         typeid2class[existingcls.typeid] = newcls
         result[existingcls.__name__] = newcls
     return result