[z3-checkins] r21857 - z3/sqlos/branch/jinty-utilities/src/sqlos

jinty at codespeak.net jinty at codespeak.net
Tue Jan 10 02:03:20 CET 2006


Author: jinty
Date: Tue Jan 10 02:03:18 2006
New Revision: 21857

Modified:
   z3/sqlos/branch/jinty-utilities/src/sqlos/container.py
Log:
Create a _getAllowedIISQLObjectUtilities function and convert users of get allowedFactories to use this. Also make the logic of the NameChooser to follow that of _getAllowedIISQLObjectUtilities. This fixes the bug uncovered in the previous commit.

Modified: z3/sqlos/branch/jinty-utilities/src/sqlos/container.py
==============================================================================
--- z3/sqlos/branch/jinty-utilities/src/sqlos/container.py	(original)
+++ z3/sqlos/branch/jinty-utilities/src/sqlos/container.py	Tue Jan 10 02:03:18 2006
@@ -65,8 +65,10 @@
             for name, factory in zapi.getFactoriesFor(ISQLObject, context=self.context):
                 if checkFactory(self, None, factory):
                     # get the sqlobject class
-                    factory = zapi.getUtility(IISQLObject, name, context=self.context)
-                    if obj.sqlmeta.table == factory.sqlmeta.table:
+                    utility = zapi.getUtility(IISQLObject, name, context=self.context)
+                    if utility is None:
+                        continue
+                    if obj.sqlmeta.table == utility.sqlmeta.table:
                         # if the tables names are the same, we assume that the
                         # sqlobject is an instance of that class, perhaps that
                         # is wrong
@@ -86,6 +88,13 @@
             if checkFactory(self, None, factory):
                 yield name
 
+    def _getAllowedIISQLObjectUtilities(self):
+        for name, factory in zapi.getFactoriesFor(ISQLObject, context=self):
+            if checkFactory(self, None, factory):
+                utility = zapi.queryUtility(IISQLObject, name, context=self)
+                if utility is not None:
+                    yield name, utility
+
     def keys(self):
         """ Return a sequence-like object containing the names
         associated with the objects that appear in the folder
@@ -105,10 +114,9 @@
         """Return a sequence-like object containing tuples of the form
         (name, object) for the objects that appear in the folder.
         """
-        for factoryName in self.allowedFactories():
-            factory = zapi.getUtility(IISQLObject, factoryName, context=self)
-            for obj in factory.select():
-                name = '%s.%s' % (factoryName, obj.id)
+        for utility_name, utility in self._getAllowedIISQLObjectUtilities():
+            for obj in utility.select():
+                name = '%s.%s' % (utility_name, obj.id)
                 yield (name, contained(obj, parent=self, name=name))
 
     def __getitem__(self, name):
@@ -140,15 +148,15 @@
         except ValueError:
             raise KeyError, name
 
-        if factoryName not in self.allowedFactories():
-            raise KeyError, name
-
-        factory = zapi.getUtility(IISQLObject, factoryName, context=self)
-        try:
-            obj = factory.get(id)
-            return contained(obj, parent=self, name=name)
-        except SQLObjectNotFound:
-            raise KeyError, name
+        for utility_name, utility in self._getAllowedIISQLObjectUtilities():
+            if factoryName != utility_name:
+                continue
+            try:
+                obj = utility.get(id)
+                return contained(obj, parent=self, name=name)
+            except SQLObjectNotFound:
+                raise KeyError, name
+        raise KeyError, name
 
     def get(self, name, default=None):
         """Return the named object, or the value of the default
@@ -168,9 +176,8 @@
     def __len__(self):
         """Return the number of objects in the folder."""
         i = 0
-        for factoryName in self.allowedFactories():
-            factory = zapi.getUtility(IISQLObject, factoryName, context=self)
-            i += factory.select().count() # optimal, does not get all objects
+        for utility_name, utility in self._getAllowedIISQLObjectUtilities():
+            i += utility.select().count() # optimal, does not get all objects
         return i
 
     def __delitem__(self, name):
@@ -192,11 +199,17 @@
     _container_id = None
 
     def allowedFactories(self):
-        # Ignore all factories not providing ISQLObjectIsolated
+        # Ignore all factories not implementing ISQLObjectIsolated
         for f in SQLObjectContainer.allowedFactories(self):
             implemented = zapi.getFactoryInterfaces(f)
             if implemented.isOrExtends(ISQLObjectIsolated):
                 yield f
+    
+    def _getAllowedIISQLObjectUtilities(self):
+        # Ignore all utilities not implementing ISQLObjectIsolated
+        for name, utility in SQLObjectContainer._getAllowedIISQLObjectUtilities(self):
+            if ISQLObjectIsolated.implementedBy(utility):
+                yield name, utility
 
     def _getContainerId(self):
         if self._container_id is None:
@@ -209,9 +222,8 @@
     def __len__(self):
         """Return the number of objects in the folder."""
         i = 0
-        for factoryName in self.allowedFactories():
-            factory = zapi.getUtility(IISQLObject, factoryName, context=self)
-            i += factory.countByDomain(self.container_id)
+        for name, utility in self._getAllowedIISQLObjectUtilities():
+            i += utility.countByDomain(self.container_id)
         return i
 
     def __delitem__(self, name):
@@ -236,10 +248,9 @@
         """Return a sequence-like object containing tuples of the form
         (name, object) for the objects that appear in the folder.
         """
-        for factoryName in self.allowedFactories():
-            factory = zapi.getUtility(IISQLObject, factoryName, context=self)
-            for obj in factory.selectByDomain(self.container_id):
-                name = '%s.%s' % (factoryName, obj.id)
+        for utility_name, utility in self._getAllowedIISQLObjectUtilities():
+            for obj in utility.selectByDomain(self.container_id):
+                name = '%s.%s' % (utility_name, obj.id)
                 yield (name, contained(obj, parent=self, name=name))
 
     def __getitem__(self, name):


More information about the z3-checkins mailing list