[Zope3-checkins] SVN: Zope3/branches/jim-adapter/src/zope/component/ Added persistent component registries.

Jim Fulton jim at zope.com
Tue Feb 28 15:17:43 EST 2006


Log message for revision 65612:
  Added persistent component registries.
  

Changed:
  A   Zope3/branches/jim-adapter/src/zope/component/persistentregistry.py
  A   Zope3/branches/jim-adapter/src/zope/component/persistentregistry.txt
  U   Zope3/branches/jim-adapter/src/zope/component/tests.py

-=-
Added: Zope3/branches/jim-adapter/src/zope/component/persistentregistry.py
===================================================================
--- Zope3/branches/jim-adapter/src/zope/component/persistentregistry.py	2006-02-28 20:17:37 UTC (rev 65611)
+++ Zope3/branches/jim-adapter/src/zope/component/persistentregistry.py	2006-02-28 20:17:43 UTC (rev 65612)
@@ -0,0 +1,43 @@
+##############################################################################
+#
+# Copyright (c) 2004 Zope Corporation and Contributors.
+# All Rights Reserved.
+#
+# This software is subject to the provisions of the Zope Public License,
+# Version 2.1 (ZPL).  A copy of the ZPL should accompany this distribution.
+# THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
+# WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+# WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
+# FOR A PARTICULAR PURPOSE.
+#
+##############################################################################
+"""Persistent component managers
+
+$Id$
+"""
+import persistent.mapping
+import persistent.list
+import zope.interface.adapter
+
+import zope.component.components
+
+class PersistentAdapterRegistry(zope.interface.adapter.AdapterRegistry,
+                                persistent.Persistent):
+
+    def changed(self):
+        self._p_changed = True
+        super(PersistentAdapterRegistry, self).changed()
+        
+class PersistentComponents(zope.component.components.Components):
+
+    def _init_registries(self):
+        self.adapters = PersistentAdapterRegistry()
+        self.utilities = PersistentAdapterRegistry()
+
+    def _init_registrations(self):
+        self._utility_registrations = persistent.mapping.PersistentMapping()
+        self._adapter_registrations = persistent.mapping.PersistentMapping()
+        self._subscription_registrations = persistent.list.PersistentList()
+        self._handler_registrations = persistent.list.PersistentList()
+
+    


Property changes on: Zope3/branches/jim-adapter/src/zope/component/persistentregistry.py
___________________________________________________________________
Name: svn:keywords
   + Id
Name: svn:eol-style
   + native

Added: Zope3/branches/jim-adapter/src/zope/component/persistentregistry.txt
===================================================================
--- Zope3/branches/jim-adapter/src/zope/component/persistentregistry.txt	2006-02-28 20:17:37 UTC (rev 65611)
+++ Zope3/branches/jim-adapter/src/zope/component/persistentregistry.txt	2006-02-28 20:17:43 UTC (rev 65612)
@@ -0,0 +1,6 @@
+Persistent Component Management
+===============================
+
+Persistent component management allows persistent management of
+components.  From a usage point of view, there shouldn't be any new
+behavior beyond what's described in components.txt.


Property changes on: Zope3/branches/jim-adapter/src/zope/component/persistentregistry.txt
___________________________________________________________________
Name: svn:eol-style
   + native

Modified: Zope3/branches/jim-adapter/src/zope/component/tests.py
===================================================================
--- Zope3/branches/jim-adapter/src/zope/component/tests.py	2006-02-28 20:17:37 UTC (rev 65611)
+++ Zope3/branches/jim-adapter/src/zope/component/tests.py	2006-02-28 20:17:43 UTC (rev 65612)
@@ -93,6 +93,21 @@
 def noop(*args):
     pass
 
+ at component.adapter(I1)
+def handle1(x):
+    print 'handle1', x
+
+def handle(*objects):
+    print 'handle', objects
+
+ at component.adapter(I1)
+def handle3(x):
+    print 'handle3', x
+
+ at component.adapter(I1)
+def handle4(x):
+    print 'handle4', x
+
 class Ob(object):
     interface.implements(I1)
     def __repr__(self):
@@ -672,6 +687,109 @@
       True
     """
 
+def test_persistent_component_managers():
+    """
+Here, we'll demonstrate that changes work even when data are stored in 
+a database and when accessed from multiple connections.
+
+Start by setting up a database and creating two transaction
+managers and database connections to work with.
+
+    >>> import ZODB.tests.util
+    >>> db = ZODB.tests.util.DB()
+    >>> import transaction
+    >>> t1 = transaction.TransactionManager()
+    >>> c1 = db.open(transaction_manager=t1)
+    >>> r1 = c1.root()
+    >>> t2 = transaction.TransactionManager()
+    >>> c2 = db.open(transaction_manager=t2)
+    >>> r2 = c2.root()
+
+Create a set of components registries in the database, alternating
+connections. 
+
+    >>> from zope.component.persistentregistry import PersistentComponents
+
+    >>> _ = t1.begin()
+    >>> r1[1] = PersistentComponents()
+    >>> t1.commit()
+
+    >>> _ = t2.begin()
+    >>> r2[2] = PersistentComponents((r2[1], ))
+    >>> t2.commit()
+
+    >>> _ = t1.begin()
+    >>> r1[3] = PersistentComponents((r1[1], ))
+    >>> t1.commit()
+
+    >>> _ = t2.begin()
+    >>> r2[4] = PersistentComponents((r2[2], r2[3]))
+    >>> t2.commit()
+
+    >>> _ = t1.begin()
+    >>> r1[1].__bases__
+    ()
+    >>> r1[2].__bases__ == (r1[1], )
+    True
+
+    >>> r1[1].registerUtility(U1(1))
+    >>> r1[1].queryUtility(I1)
+    U1(1)
+    >>> r1[2].queryUtility(I1)
+    U1(1)
+    >>> t1.commit()
+    
+    >>> _ = t2.begin()
+    >>> r2[1].registerUtility(U1(2))
+    >>> r2[2].queryUtility(I1)
+    U1(2)
+
+    >>> r2[4].queryUtility(I1)
+    U1(2)
+    >>> t2.commit()
+
+    
+    >>> _ = t1.begin()
+    >>> r1[1].registerUtility(U12(1), I2)
+    >>> r1[4].queryUtility(I2)
+    U12(1)
+    >>> t1.commit()
+
+
+    >>> _ = t2.begin()
+    >>> r2[3].registerUtility(U12(3), I2)
+    >>> r2[4].queryUtility(I2)
+    U12(3)
+    >>> t2.commit()
+
+    >>> _ = t1.begin()
+
+    >>> r1[1].registerHandler(handle1, info="First handler")
+    >>> r1[2].registerHandler(handle, required=[U])
+    
+    >>> r1[3].registerHandler(handle3)
+    
+    >>> r1[4].registerHandler(handle4)
+
+    >>> r1[4].handle(U1(1))
+    handle1 U1(1)
+    handle3 U1(1)
+    handle (U1(1),)
+    handle4 U1(1)
+
+    >>> t1.commit()
+
+    >>> _ = t2.begin()
+    >>> r2[4].handle(U1(1))
+    handle1 U1(1)
+    handle3 U1(1)
+    handle (U1(1),)
+    handle4 U1(1)
+    >>> t2.abort()
+
+    >>> db.close()
+    """
+
 def test_suite():
     checker = renormalizing.RENormalizing([
         (re.compile('at 0x[0-9a-f]+'), 'at <SOME ADDRESS>'),



More information about the Zope3-Checkins mailing list