Author: jmorliaguet
Date: Mon Apr  3 20:48:00 2006
New Revision: 2768

Modified:
   cpsskins/branches/jmo-perspectives/browser/negotiation/interfaces.py
   cpsskins/branches/jmo-perspectives/browser/negotiation/views.py
   cpsskins/branches/jmo-perspectives/configuration/negotiation/metaconfigure.py
   
cpsskins/branches/jmo-perspectives/configuration/negotiation/metadirectives.py
Log:

- implemented negotiation strategies



Modified: cpsskins/branches/jmo-perspectives/browser/negotiation/interfaces.py
==============================================================================
--- cpsskins/branches/jmo-perspectives/browser/negotiation/interfaces.py        
(original)
+++ cpsskins/branches/jmo-perspectives/browser/negotiation/interfaces.py        
Mon Apr  3 20:48:00 2006
@@ -40,13 +40,12 @@
         """Return the viewer used to render the page."""
 
 
-class INegotiationScheme(Interface):
+class INegotiationStrategy(Interface):
+    """A marker interface for registering negotiation strategies
+    """
 
-    def __call__(self):
-        """Return the result of the negotiation step:
-        - the requested object if available
-        - None if the objected is not available
-        """
+    def __call__():
+        """Return the list of negotiation chains"""
 
 
 class INegotiationChain(Interface):
@@ -54,4 +53,14 @@
     """
 
     def __call__():
-        """Return the list of interfaces"""
+        """Return the list of megotiation schemes"""
+
+
+class INegotiationScheme(Interface):
+
+    def __call__(self):
+        """Return the result of the negotiation step:
+        - the requested object if available
+        - None if the objected is not available
+        """
+

Modified: cpsskins/branches/jmo-perspectives/browser/negotiation/views.py
==============================================================================
--- cpsskins/branches/jmo-perspectives/browser/negotiation/views.py     
(original)
+++ cpsskins/branches/jmo-perspectives/browser/negotiation/views.py     Mon Apr 
 3 20:48:00 2006
@@ -25,6 +25,7 @@
 from cpsskins.browser.rendering.interfaces import IViewer
 from cpsskins.browser.negotiation.interfaces import INegotiation
 from cpsskins.browser.negotiation.interfaces import INegotiationChain
+from cpsskins.browser.negotiation.interfaces import INegotiationStrategy
 from cpsskins.utils import getThemeManager
 
 class NegotiationView(BrowserView):
@@ -32,17 +33,20 @@
     """
     implements(INegotiation)
 
-    def negotiate(self, type):
-        chain = self.getChain(type)
+    def negotiate(self, name):
+        chain = self.getChain(name)
         negotiation_context = (self.context, getThemeManager(), self.request)
         for scheme in chain:
-            result = getMultiAdapter(negotiation_context, scheme, name=type)()
+            result = getMultiAdapter(negotiation_context, scheme, name=name)()
             if result:
                 return result
         return None
 
-    def getChain(self, type):
-        return zapi.getUtility(interface=INegotiationChain, name=type)()
+    def getStrategy(self, name=u'default'):
+        return zapi.getUtility(interface=INegotiationStrategy, name=name)
+
+    def getChain(self, name):
+        return self.getStrategy().getChain(name)
 
     ###################################################################
     # Themes and pages
@@ -89,16 +93,3 @@
         page = self.getPage()
         return zapi.getMultiAdapter((page, self.request), IViewer)
 
-class NegotiationChain:
-
-    implements(INegotiationChain)
-
-    def __init__(self):
-        self.chain = ()
-
-    def add(self, scheme):
-        self.chain += scheme,
-
-    def __call__(self):
-        return self.chain
-

Modified: 
cpsskins/branches/jmo-perspectives/configuration/negotiation/metaconfigure.py
==============================================================================
--- 
cpsskins/branches/jmo-perspectives/configuration/negotiation/metaconfigure.py   
    (original)
+++ 
cpsskins/branches/jmo-perspectives/configuration/negotiation/metaconfigure.py   
    Mon Apr  3 20:48:00 2006
@@ -20,28 +20,37 @@
 from zope.app.component.metaconfigure import adapter
 from zope.configuration.exceptions import ConfigurationError
 from zope.publisher.interfaces.browser import IBrowserRequest
-from zope.component import provideUtility
+from zope.component import provideUtility, queryUtility
 
 from cpsskins.browser.negotiation.interfaces import INegotiationScheme
 from cpsskins.interfaces import IThemeManagementFolder
 from cpsskins.browser.negotiation.interfaces import INegotiationChain
-from cpsskins.browser.negotiation.views import NegotiationChain
-
-chains = {}
+from cpsskins.browser.negotiation.interfaces import INegotiationStrategy
+from cpsskins.browser.negotiation.chain import NegotiationChain
+from cpsskins.browser.negotiation.strategy import NegotiationStrategy
 
 class negotiationchain(object):
 
-    def __init__(self, _context, name=u''):
+    def __init__(self, _context, name=u'', strategy=u'default'):
         self.name = name
-        chain = chains[name] = NegotiationChain()
-        provideUtility(chain, INegotiationChain, name)
+        self.chain = NegotiationChain()
+
+        st = queryUtility(INegotiationStrategy, strategy)
+        if st is None:
+            st = NegotiationStrategy()
+            provideUtility(st, INegotiationStrategy, strategy)
+
+        st.addChain(self.chain, name)
 
     def scheme(self, _context, factory=None, interface=None):
         if not interface:
-            raise ConfigurationError(
-              "Must specify a negotiation scheme interface.")
+            raise ConfigurationError("Must specify a negotiation scheme.")
+
+        # add the negotiation scheme to the chain
+        self.chain.add(interface)
+
         return scheme(_context, name=self.name, factory=factory,
-                          interface=interface)
+                      interface=interface)
 
 def scheme(_context, name=u'', factory=None, interface=None):
 
@@ -63,6 +72,3 @@
     adapter(_context=_context, factory=(factory,), provides=interface,
             for_=adapts, name=name)
 
-    # add the negotiation scheme to the chain
-    chains[name].add(interface)
-

Modified: 
cpsskins/branches/jmo-perspectives/configuration/negotiation/metadirectives.py
==============================================================================
--- 
cpsskins/branches/jmo-perspectives/configuration/negotiation/metadirectives.py  
    (original)
+++ 
cpsskins/branches/jmo-perspectives/configuration/negotiation/metadirectives.py  
    Mon Apr  3 20:48:00 2006
@@ -26,6 +26,12 @@
 
 class INegotiationChainDirective(Interface):
 
+    strategy = TextLine(
+        title=_(u"The negotiation strategy"),
+        description=_(u"The name of the negotiation strategy."),
+        required=False,
+        )
+
     name = TextLine(
         title=_(u"The chain's name"),
         description=_(u"The name of the negotiation chain."),
-- 
http://lists.nuxeo.com/mailman/listinfo/z3lab-checkins

Reply via email to