[Checkins] SVN: Products.GenericSetup/branches/1.4/Products/GenericSetup/ don't overwrite existing local utilities if they're already of the correct

Rob Miller ra at burningman.com
Wed Sep 17 14:18:27 EDT 2008


Log message for revision 91212:
  don't overwrite existing local utilities if they're already of the correct
  type
  

Changed:
  U   Products.GenericSetup/branches/1.4/Products/GenericSetup/CHANGES.txt
  U   Products.GenericSetup/branches/1.4/Products/GenericSetup/components.py
  U   Products.GenericSetup/branches/1.4/Products/GenericSetup/tests/test_components.py

-=-
Modified: Products.GenericSetup/branches/1.4/Products/GenericSetup/CHANGES.txt
===================================================================
--- Products.GenericSetup/branches/1.4/Products/GenericSetup/CHANGES.txt	2008-09-17 18:13:33 UTC (rev 91211)
+++ Products.GenericSetup/branches/1.4/Products/GenericSetup/CHANGES.txt	2008-09-17 18:18:27 UTC (rev 91212)
@@ -4,6 +4,9 @@
 GenericSetup 1.4.2 (unreleased)
 -------------------------------
 
+- Update local component registry importer to prevent it from overwriting
+  existing utilities if they are already of the correct type
+
 - Property import/export tests: Fix and test for non-ASCII properties.
   (https://bugs.launchpad.net/zope-cmf/+bug/202356)
   (https://bugs.launchpad.net/zope-cmf/+bug/242588)

Modified: Products.GenericSetup/branches/1.4/Products/GenericSetup/components.py
===================================================================
--- Products.GenericSetup/branches/1.4/Products/GenericSetup/components.py	2008-09-17 18:13:33 UTC (rev 91211)
+++ Products.GenericSetup/branches/1.4/Products/GenericSetup/components.py	2008-09-17 18:18:27 UTC (rev 91212)
@@ -127,6 +127,8 @@
 
     def _initUtilities(self, node):
         site = self._getSite()
+        current_utilities = self.context.registeredUtilities()
+        
         for child in node.childNodes:
             if child.nodeName != 'utility':
                 continue
@@ -163,7 +165,16 @@
             elif component:
                 self.context.registerUtility(component, provided, name)
             elif factory is not None:
-                self.context.registerUtility(factory(), provided, name)
+                current = [ utility for utility in current_utilities
+                            if utility.provided==provided and 
+                            utility.name==name ]
+                assert len(current) <=1
+
+                new_utility = factory()
+                if current and type(current[0].component) == type(new_utility):
+                    continue
+                
+                self.context.registerUtility(new_utility, provided, name)
             else:
                 self._logger.warning("Invalid utility registration for "
                                      "interface %s" % provided)

Modified: Products.GenericSetup/branches/1.4/Products/GenericSetup/tests/test_components.py
===================================================================
--- Products.GenericSetup/branches/1.4/Products/GenericSetup/tests/test_components.py	2008-09-17 18:13:33 UTC (rev 91211)
+++ Products.GenericSetup/branches/1.4/Products/GenericSetup/tests/test_components.py	2008-09-17 18:18:27 UTC (rev 91212)
@@ -26,13 +26,17 @@
 from Products.Five.component import enableSite
 from Products.Five.component.interfaces import IObjectManagerSite
 from zope.app.component.hooks import setSite, clearSite, setHooks
+from zope.app.component.hooks import getSite
+from zope.component import getMultiAdapter
 from zope.component import getSiteManager
 from zope.component import queryUtility
 from zope.component.globalregistry import base
 from zope.interface import implements
 from zope.interface import Interface
 
+from Products.GenericSetup.interfaces import IBody
 from Products.GenericSetup.testing import BodyAdapterTestCase
+from Products.GenericSetup.testing import DummySetupEnviron
 from Products.GenericSetup.testing import ExportImportZCMLLayer
 
 try:
@@ -180,6 +184,30 @@
         self._obj = sm
         self._BODY = _COMPONENTS_BODY
 
+    def test_no_overwrite(self):
+        # make sure we don't overwrite an existing tool when it
+        # already exists and has the same factory
+        context = DummySetupEnviron()
+        context._should_purge = False
+        importer = getMultiAdapter((self._obj, context), IBody)
+        importer.body = self._BODY # <-- triggers the import :(
+
+        util = queryUtility(IDummyInterface)
+        value = 'bar'
+        util.foo = value
+
+        # re-retrieve to make sure it's the same object
+        util = queryUtility(IDummyInterface)
+        self.assertEquals(getattr(util, 'foo', None), value)
+
+        context = DummySetupEnviron()
+        context._should_purge = False
+        importer = getMultiAdapter((self._obj, context), IBody)
+        importer.body = self._BODY # <-- triggers the import :(
+
+        util = queryUtility(IDummyInterface)
+        self.assertEquals(getattr(util, 'foo', None), value)
+
     def tearDown(self):
         clearSite()
 



More information about the Checkins mailing list