[Checkins] SVN: zope.publisher/trunk/src/zope/publisher/ Generalised the skin support a bit to allow skins not only for browser but also

Christian Zagrodnick cz at gocept.com
Thu Aug 9 09:52:10 EDT 2007


Log message for revision 78726:
  Generalised the skin support a bit to allow skins not only for browser but also
  for other requests, notably XML-RPC.
  
  

Changed:
  U   zope.publisher/trunk/src/zope/publisher/browser.py
  U   zope.publisher/trunk/src/zope/publisher/http.py
  A   zope.publisher/trunk/src/zope/publisher/http.txt
  U   zope.publisher/trunk/src/zope/publisher/interfaces/xmlrpc.py
  U   zope.publisher/trunk/src/zope/publisher/tests/test_http.py
  U   zope.publisher/trunk/src/zope/publisher/xmlrpc.py
  U   zope.publisher/trunk/src/zope/publisher/xmlrpc.txt

-=-
Modified: zope.publisher/trunk/src/zope/publisher/browser.py
===================================================================
--- zope.publisher/trunk/src/zope/publisher/browser.py	2007-08-09 13:49:32 UTC (rev 78725)
+++ zope.publisher/trunk/src/zope/publisher/browser.py	2007-08-09 13:52:09 UTC (rev 78726)
@@ -34,6 +34,7 @@
 from zope.i18n.interfaces import IUserPreferredCharsets
 from zope.location import Location
 
+import zope.publisher.http
 from zope.publisher.interfaces import NotFound
 from zope.publisher.interfaces.browser import IBrowserRequest
 from zope.publisher.interfaces.browser import IDefaultBrowserLayer
@@ -44,7 +45,6 @@
 from zope.publisher.interfaces.browser import IBrowserSkinType
 from zope.publisher.interfaces.browser import ISkinChangedEvent
 from zope.publisher.interfaces.http import IHTTPRequest
-from zope.publisher.http import HTTPRequest, HTTPResponse
 
 __ArrayTypes = (ListType, TupleType)
 
@@ -210,7 +210,7 @@
             + ", ".join(["%s: %s" % (key, repr(value))
             for key, value in items]) + "}")
 
-class BrowserRequest(HTTPRequest):
+class BrowserRequest(zope.publisher.http.HTTPRequest):
     implements(IBrowserRequest, IBrowserApplicationRequest)
 
     __slots__ = (
@@ -653,7 +653,7 @@
 
 
 
-class BrowserResponse(HTTPResponse):
+class BrowserResponse(zope.publisher.http.HTTPResponse):
     """Browser response
     """
 
@@ -1000,12 +1000,7 @@
     >>> cleanUp()
 
     """
-    # Remove all existing skin declarations (commonly the default skin).
-    ifaces = [iface for iface in directlyProvidedBy(request)
-              if not IBrowserSkinType.providedBy(iface)]
-    # Add the new skin.
-    ifaces.append(skin)
-    directlyProvides(request, *ifaces)
+    zope.publisher.http.applySkin(request, skin, IBrowserSkinType)
     zope.event.notify(SkinChangedEvent(request))
 
 class SkinChangedEvent(object):

Modified: zope.publisher/trunk/src/zope/publisher/http.py
===================================================================
--- zope.publisher/trunk/src/zope/publisher/http.py	2007-08-09 13:49:32 UTC (rev 78725)
+++ zope.publisher/trunk/src/zope/publisher/http.py	2007-08-09 13:52:09 UTC (rev 78726)
@@ -996,3 +996,17 @@
 
     def __iter__(self):
         return iter(self.body)
+
+
+def applySkin(request, skin, skin_type):
+    """Change the presentation skin for this request.
+
+
+    """
+    # Remove all existing skin declarations (commonly the default skin).
+
+    ifaces = [iface for iface in interface.directlyProvidedBy(request)
+              if not skin_type.providedBy(iface)]
+    # Add the new skin.
+    ifaces.append(skin)
+    interface.directlyProvides(request, *ifaces)

Added: zope.publisher/trunk/src/zope/publisher/http.txt
===================================================================
--- zope.publisher/trunk/src/zope/publisher/http.txt	                        (rev 0)
+++ zope.publisher/trunk/src/zope/publisher/http.txt	2007-08-09 13:52:09 UTC (rev 78726)
@@ -0,0 +1,48 @@
+====
+HTTP
+====
+
+Generic Skin Support
+++++++++++++++++++++
+   
+The skin support is generic. Create a new skin type:
+
+>>> import zope.interface
+>>> import zope.interface.interfaces
+>>> class ISkinType(zope.interface.interfaces.Interface):
+...     pass
+
+Create two skins:
+
+>>> class SkinA(zope.interface.Interface):
+...     pass
+>>> zope.interface.directlyProvides(SkinA, ISkinType)
+>>> class SkinB(zope.interface.Interface):
+...     pass
+>>> zope.interface.directlyProvides(SkinB, ISkinType)
+
+And create a  request:
+
+>>> class IRequest(zope.interface.Interface):
+...     pass
+>>> class Request(object):
+...     zope.interface.implements(IRequest)
+
+>>> request = Request()
+
+Now apply SkinA two our request
+
+>>> import zope.publisher.http
+>>> zope.publisher.http.applySkin(request, SkinA, ISkinType)
+>>> list(zope.interface.providedBy(request).interfaces())
+...     # doctest: +NORMALIZE_WHITESPACE
+[<InterfaceClass __builtin__.SkinA>,
+ <InterfaceClass __builtin__.IRequest>]
+
+Applying SkinB removes SkinA:
+
+>>> zope.publisher.http.applySkin(request, SkinB, ISkinType)
+>>> list(zope.interface.providedBy(request).interfaces())
+...     # doctest: +NORMALIZE_WHITESPACE
+[<InterfaceClass __builtin__.SkinB>,
+ <InterfaceClass __builtin__.IRequest>]


Property changes on: zope.publisher/trunk/src/zope/publisher/http.txt
___________________________________________________________________
Name: svn:keywords
   + Id Rev Date
Name: svn:eol-style
   + native

Modified: zope.publisher/trunk/src/zope/publisher/interfaces/xmlrpc.py
===================================================================
--- zope.publisher/trunk/src/zope/publisher/interfaces/xmlrpc.py	2007-08-09 13:49:32 UTC (rev 78725)
+++ zope.publisher/trunk/src/zope/publisher/interfaces/xmlrpc.py	2007-08-09 13:52:09 UTC (rev 78726)
@@ -18,6 +18,7 @@
 
 __docformat__ = "reStructuredText"
 
+import zope.interface.interfaces
 from zope.interface import Interface
 
 from zope.publisher.interfaces import IPublication
@@ -50,4 +51,5 @@
         """Return the given object without proxies."""
 
 
-
+class IXMLRPCSkinType(zope.interface.interfaces.IInterface):
+    """Skin for XML-RPC."""

Modified: zope.publisher/trunk/src/zope/publisher/tests/test_http.py
===================================================================
--- zope.publisher/trunk/src/zope/publisher/tests/test_http.py	2007-08-09 13:49:32 UTC (rev 78725)
+++ zope.publisher/trunk/src/zope/publisher/tests/test_http.py	2007-08-09 13:52:09 UTC (rev 78726)
@@ -774,6 +774,8 @@
     suite.addTest(unittest.makeSuite(HTTPInputStreamTests))
     suite.addTest(doctest.DocFileSuite(
         '../httpresults.txt', setUp=cleanUp, tearDown=cleanUp))
+    suite.addTest(doctest.DocFileSuite(
+        '../http.txt', setUp=cleanUp, tearDown=cleanUp))
     suite.addTest(unittest.makeSuite(APITests))
     return suite
 

Modified: zope.publisher/trunk/src/zope/publisher/xmlrpc.py
===================================================================
--- zope.publisher/trunk/src/zope/publisher/xmlrpc.py	2007-08-09 13:49:32 UTC (rev 78725)
+++ zope.publisher/trunk/src/zope/publisher/xmlrpc.py	2007-08-09 13:52:09 UTC (rev 78726)
@@ -27,13 +27,13 @@
 import zope.component
 import zope.interface
 from zope.interface import implements
-from zope.publisher.interfaces.xmlrpc import \
-        IXMLRPCPublisher, IXMLRPCRequest, IXMLRPCPremarshaller
+from zope.publisher.interfaces.xmlrpc import (
+    IXMLRPCPublisher, IXMLRPCRequest, IXMLRPCPremarshaller, IXMLRPCSkinType)
 
-from zope.publisher.http import HTTPRequest, HTTPResponse, DirectResult
+import zope.publisher.http
 from zope.security.proxy import isinstance
 
-class XMLRPCRequest(HTTPRequest):
+class XMLRPCRequest(zope.publisher.http.HTTPRequest):
     implements(IXMLRPCRequest)
 
     _args = ()
@@ -80,7 +80,7 @@
         super(TestRequest, self).__init__(body_instream, _testEnv, response)
 
 
-class XMLRPCResponse(HTTPResponse):
+class XMLRPCResponse(zope.publisher.http.HTTPResponse):
     """XMLRPC response.
 
     This object is responsible for converting all output to valid XML-RPC.
@@ -117,7 +117,8 @@
         headers = [('content-type', 'text/xml;charset=utf-8'),
                    ('content-length', str(len(body)))]
         self._headers.update(dict((k, [v]) for (k, v) in headers))
-        super(XMLRPCResponse, self).setResult(DirectResult((body,)))
+        super(XMLRPCResponse, self).setResult(
+            zope.publisher.http.DirectResult((body,)))
 
 
     def handleException(self, exc_info):
@@ -232,3 +233,7 @@
     if premarshaller is not None:
         return premarshaller()
     return data
+
+
+def applySkin(request, skin):
+    zope.publisher.http.applySkin(request, skin, IXMLRPCSkinType)

Modified: zope.publisher/trunk/src/zope/publisher/xmlrpc.txt
===================================================================
--- zope.publisher/trunk/src/zope/publisher/xmlrpc.txt	2007-08-09 13:49:32 UTC (rev 78725)
+++ zope.publisher/trunk/src/zope/publisher/xmlrpc.txt	2007-08-09 13:52:09 UTC (rev 78726)
@@ -67,3 +67,46 @@
   >>> stripped_binary = premarshal(proxied_binary)
   >>> type(stripped_binary) is Proxy
   False
+
+Skin-Support
+============
+
+There is also skin support for XML-RPC:
+
+Create two skins:
+
+>>> import zope.interface
+>>> from zope.publisher.interfaces.xmlrpc import IXMLRPCSkinType
+>>> class SkinA(zope.interface.Interface):
+...     pass
+>>> zope.interface.directlyProvides(SkinA, IXMLRPCSkinType)
+>>> class SkinB(zope.interface.Interface):
+...     pass
+>>> zope.interface.directlyProvides(SkinB, IXMLRPCSkinType)
+
+And create a  request:
+
+>>> class IRequest(zope.interface.Interface):
+...     pass
+>>> class Request(object):
+...     zope.interface.implements(IRequest)
+
+>>> request = Request()
+
+Now apply SkinA two our request
+
+>>> import zope.publisher.xmlrpc
+>>> zope.publisher.xmlrpc.applySkin(request, SkinA)
+>>> list(zope.interface.providedBy(request).interfaces())
+...     # doctest: +NORMALIZE_WHITESPACE
+[<InterfaceClass __builtin__.SkinA>,
+ <InterfaceClass __builtin__.IRequest>]
+
+
+Applying SkinB removes SkinA:
+
+>>> zope.publisher.xmlrpc.applySkin(request, SkinB)
+>>> list(zope.interface.providedBy(request).interfaces())
+...     # doctest: +NORMALIZE_WHITESPACE
+[<InterfaceClass __builtin__.SkinB>,
+ <InterfaceClass __builtin__.IRequest>]



More information about the Checkins mailing list