[Checkins] SVN: z3c.ptcompat/trunk/ Existing template instances are now migrated to the right engine when using the enable and disable methods.

Malthe Borch mborch at gmail.com
Sat Jul 25 03:47:40 EDT 2009


Log message for revision 102291:
  Existing template instances are now migrated to the right engine when using the enable and disable methods.

Changed:
  U   z3c.ptcompat/trunk/CHANGES.txt
  U   z3c.ptcompat/trunk/src/z3c/ptcompat/__init__.py
  U   z3c.ptcompat/trunk/src/z3c/ptcompat/config.py

-=-
Modified: z3c.ptcompat/trunk/CHANGES.txt
===================================================================
--- z3c.ptcompat/trunk/CHANGES.txt	2009-07-24 21:29:22 UTC (rev 102290)
+++ z3c.ptcompat/trunk/CHANGES.txt	2009-07-25 07:47:38 UTC (rev 102291)
@@ -1,10 +1,10 @@
 Changelog
 =========
 
-0.5.6 (unreleased)
-------------------
+In next release...
 
-- ...
+- Existing template instances are now migrated to the right engine
+  when using the ``enable`` and ``disable`` methods. [malthe]
 
 0.5.5 (2009-07-24)
 ------------------

Modified: z3c.ptcompat/trunk/src/z3c/ptcompat/__init__.py
===================================================================
--- z3c.ptcompat/trunk/src/z3c/ptcompat/__init__.py	2009-07-24 21:29:22 UTC (rev 102290)
+++ z3c.ptcompat/trunk/src/z3c/ptcompat/__init__.py	2009-07-25 07:47:38 UTC (rev 102291)
@@ -54,6 +54,11 @@
             return output.getvalue()
 
         return render
-    
+
 class ViewPageTemplateFile(ViewPageTemplateFile):
     """View page template file."""
+
+    def __new__(cls, *args, **kwargs):
+        inst = object.__new__(cls)
+        config.REGISTRY[inst] = (args, kwargs)
+        return inst

Modified: z3c.ptcompat/trunk/src/z3c/ptcompat/config.py
===================================================================
--- z3c.ptcompat/trunk/src/z3c/ptcompat/config.py	2009-07-24 21:29:22 UTC (rev 102290)
+++ z3c.ptcompat/trunk/src/z3c/ptcompat/config.py	2009-07-25 07:47:38 UTC (rev 102291)
@@ -1,7 +1,10 @@
 import os
 import logging
 import z3c.ptcompat
+import weakref
 
+REGISTRY = weakref.WeakKeyDictionary()
+
 PREFER_Z3C_PT = os.environ.get("PREFER_Z3C_PT", 'false').lower() in \
                 ('y', 'yes', 't', 'true', 'on', '1')
 
@@ -18,8 +21,20 @@
     PREFER_Z3C_PT = True
     reload(z3c.ptcompat)
 
+    for inst, (args, kwargs) in REGISTRY.items():
+        migrate(inst, args, kwargs)
+
 def disable():
     global PREFER_Z3C_PT
     PREFER_Z3C_PT = False
     reload(z3c.ptcompat)
 
+    for inst, (args, kwargs) in REGISTRY.items():
+        migrate(inst, args, kwargs)
+
+def migrate(inst, args, kwargs):
+    cls = inst.__class__
+    new_cls = getattr(z3c.ptcompat, cls.__name__)
+    inst.__class__ = new_cls
+    inst.__dict__.clear()
+    inst.__init__(*args, **kwargs)



More information about the Checkins mailing list