[Zope3-checkins] SVN: Zope3/branches/ajung-target-requestpublication-next-try-branch/src/zope/app/publication/requestpublicationregistry.py some more comments and tweaks

Andreas Jung andreas at andreas-jung.com
Sat Oct 8 02:56:55 EDT 2005


Log message for revision 38915:
  some more comments and tweaks
  

Changed:
  U   Zope3/branches/ajung-target-requestpublication-next-try-branch/src/zope/app/publication/requestpublicationregistry.py

-=-
Modified: Zope3/branches/ajung-target-requestpublication-next-try-branch/src/zope/app/publication/requestpublicationregistry.py
===================================================================
--- Zope3/branches/ajung-target-requestpublication-next-try-branch/src/zope/app/publication/requestpublicationregistry.py	2005-10-07 23:21:26 UTC (rev 38914)
+++ Zope3/branches/ajung-target-requestpublication-next-try-branch/src/zope/app/publication/requestpublicationregistry.py	2005-10-08 06:56:55 UTC (rev 38915)
@@ -20,6 +20,7 @@
 
 from zope.interface import implements
 from zope.app.publication.interfaces import IRequestPublicationRegistry
+from zope.configuration.exception import ConfigurationError
 
 class RequestPublicationRegistry(object):
     """ The registry implements a three stage lookup for registred factories
@@ -42,18 +43,34 @@
     def register(self, method, mimetype, name, priority, factory):
         """ registers a factory for method+mimetype """
 
+        # initialize the two-level deep nested datastructure if necessary
         if not self._d.has_key(method):
             self._d[method] = {}
         if not self._d[method].has_key(mimetype):
             self._d[method][mimetype] = []
         l = self._d[method][mimetype]
-        for pos, d in enumerate(l): # override existing factory by name
+
+        # Check if there is already a registered publisher factory (check by name).
+        # If yes then it will be removed and replaced by a new publisher.
+        for pos, d in enumerate(l): 
             if d['name'] == name:
                 del l[pos]
                 break
+        # add the publisher factory + additional informations            
         l.append({'name' : name, 'factory' : factory, 'priority' : priority})
-        l.sort(lambda x,y: -cmp(x['priority'], y['priority'])) # order by descending priority
 
+        # order by descending priority
+        l.sort(lambda x,y: -cmp(x['priority'], y['priority'])) 
+
+        # check if the priorities are unique
+        priorities = [item['priority'] for item in l]
+        if len(sets.Set(priorities)) != len(l)  
+            raise ConfigurationError('All registered publishers for a given ' 
+                                     'method+mimetype must have distinct '
+                                     'priorities. Please check your ZCML '
+                                     'configuration')
+                                     
+
     def getFactoriesFor(self, method, mimetype):
         try:
             return self._d[method][mimetype]
@@ -72,7 +89,8 @@
             if not factory_lst:
                 factory_lst = self.getFactoriesFor('*', '*')
                 if not factory_lst:
-                    return None
+                    raise ConfigurationError('No registered publisher found '
+                                             'for (%s/%s)' % (method, mimetype))
 
         # now iterate over all factory candidates and let them introspect
         # the request environment to figure out if they can handle the
@@ -82,4 +100,6 @@
             if factory.canHandle(environment):
                 return factory
 
+        # Actually we should never get here unless of improper 
+        # configuration (no default handler for method=* and mimetype=*
         return None



More information about the Zope3-Checkins mailing list