[Zope3-checkins] SVN: Zope3/branches/ajung-target-requestpublication-next-try-branch/src/zope/app/publication/ re-adding the good stuff from the former prototype

Andreas Jung andreas at andreas-jung.com
Fri Oct 7 04:58:45 EDT 2005


Log message for revision 38845:
  re-adding the good stuff from the former prototype
  

Changed:
  U   Zope3/branches/ajung-target-requestpublication-next-try-branch/src/zope/app/publication/interfaces.py
  A   Zope3/branches/ajung-target-requestpublication-next-try-branch/src/zope/app/publication/publicationfactories.py
  A   Zope3/branches/ajung-target-requestpublication-next-try-branch/src/zope/app/publication/tests/test_publicationfactories.py

-=-
Modified: Zope3/branches/ajung-target-requestpublication-next-try-branch/src/zope/app/publication/interfaces.py
===================================================================
--- Zope3/branches/ajung-target-requestpublication-next-try-branch/src/zope/app/publication/interfaces.py	2005-10-07 08:51:13 UTC (rev 38844)
+++ Zope3/branches/ajung-target-requestpublication-next-try-branch/src/zope/app/publication/interfaces.py	2005-10-07 08:58:44 UTC (rev 38845)
@@ -82,3 +82,33 @@
     /.  In particular, if the content included HTML, relative links in
     the HTML are relative to the container the content is in.
     """
+
+
+class IRequestPublicationFactory(interface.Interface):
+    """ request-publication factory """
+
+    def canHandle(environment):
+        """ returns True if it can handle the request,
+            otherwise False. 'environment' can be used by the factory 
+            to make a decision based on the HTTP headers.
+        """
+
+    def __call__():
+        """ returns a tuple (request, publication) """
+
+class IRquestPublicationRegistry(interface.Interface):
+    """ A registry to lookup a RequestPublicationFactory by
+        request method + mime-type. Multiple factories can be configured
+        for the same method+mimetype. The factory itself can introspect
+        the environment to decide if it can handle the request as given
+        by the environment or not. Factories are sorted in the order
+        of their registration in ZCML.
+    """
+
+    def register(method, mimetype, factory):
+        """ registers a factory for method+mimetype """
+
+    def lookup(method, mimetype, environment):
+        """ Lookup a factory for a given method+mimetype and a 
+            enviroment. 
+        """

Added: Zope3/branches/ajung-target-requestpublication-next-try-branch/src/zope/app/publication/publicationfactories.py
===================================================================
--- Zope3/branches/ajung-target-requestpublication-next-try-branch/src/zope/app/publication/publicationfactories.py	2005-10-07 08:51:13 UTC (rev 38844)
+++ Zope3/branches/ajung-target-requestpublication-next-try-branch/src/zope/app/publication/publicationfactories.py	2005-10-07 08:58:44 UTC (rev 38845)
@@ -0,0 +1,83 @@
+##############################################################################
+#
+# Copyright (c) 2001, 2002 Zope Corporation and Contributors.
+# All Rights Reserved.
+#
+# This software is subject to the provisions of the Zope Public License,
+# Version 2.1 (ZPL).  A copy of the ZPL should accompany this distribution.
+# THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
+# WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+# WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
+# FOR A PARTICULAR PURPOSE.
+#
+##############################################################################
+"""Publication factories.
+
+This module provides factories for tuples (request, publication).
+
+$Id: publicationfactories.py 38841 2005-10-07 04:34:09Z andreasjung $
+"""
+__docformat__ = 'restructuredtext'
+
+from zope import component
+from zope.interface import implements
+from zope.app.publication.http import BaseHTTPPublication
+from zope.app.publication.interfaces import IRequestPublicationFactory, ISOAPRequestFactory, ISOAPRequestFactory
+from zope.app.publication import interfaces
+from zope.app.publication.soap import SOAPPublication
+from zope.app.publication.xmlrpc import XMLRPCPublication
+from zope.app.publication.http import HTTPPublication
+from zope.publisher.xmlrpc import XMLRPCRequest
+from zope.app.publication.browser import BrowserPublication 
+from zope.publisher.http import HTTPRequest
+from zope.publisher.browser import BrowserRequest
+
+class SOAPFactory(object):
+
+    implements(IRequestPublicationFactory)
+
+    def canHandle(self, environment):
+        self.soap_req = component.queryUtility(interfaces.ISOAPRequestFactory)
+        return bool(environment.get('HTTP_SOAPACTION') and self.soap_req)
+
+    def __call__(self):
+        return self.soap_req, SOAPPublication
+
+
+class XMLRPCFactory(object):
+
+    implements(IRequestPublicationFactory)
+
+    def canHandle(self, environment):
+        return True
+
+    def __call__(self):
+        request_class = component.queryUtility(
+            interfaces.IXMLRPCRequestFactory, default=XMLRPCRequest)
+        return request_class, XMLRPCPublication
+
+
+class HTTPFactory(object):
+
+    implements(IRequestPublicationFactory)
+
+    def canHandle(self, environment):
+        return True
+
+    def __call__(self):
+        request_class = component.queryUtility(
+            interfaces.IHTTPRequestFactory, default=HTTPRequest)
+        return request_class, HTTPPublication
+
+class BrowserFactory(object):
+
+    implements(IRequestPublicationFactory)
+
+    def canHandle(self, environment):
+        return True
+
+    def __call__(self):
+        request_class = component.queryUtility(
+                interfaces.IBrowserRequestFactory, default=BrowserRequest)
+        return request_class, BrowserPublication
+

Added: Zope3/branches/ajung-target-requestpublication-next-try-branch/src/zope/app/publication/tests/test_publicationfactories.py
===================================================================
--- Zope3/branches/ajung-target-requestpublication-next-try-branch/src/zope/app/publication/tests/test_publicationfactories.py	2005-10-07 08:51:13 UTC (rev 38844)
+++ Zope3/branches/ajung-target-requestpublication-next-try-branch/src/zope/app/publication/tests/test_publicationfactories.py	2005-10-07 08:58:44 UTC (rev 38845)
@@ -0,0 +1,115 @@
+##############################################################################
+#
+# Copyright (c) 2003 Zope Corporation and Contributors.
+# All Rights Reserved.
+#
+# This software is subject to the provisions of the Zope Public License,
+# Version 2.1 (ZPL).  A copy of the ZPL should accompany this distribution.
+# THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
+# WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+# WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
+# FOR A PARTICULAR PURPOSE.
+#
+##############################################################################
+"""Tests for the HTTP Publication Request Factory.
+
+$Id: test_publicationfactories.py 38841 2005-10-07 04:34:09Z andreasjung $
+"""
+from unittest import TestCase, TestSuite, main, makeSuite
+
+from StringIO import StringIO
+
+from zope import component, interface
+from zope.publisher.browser import BrowserRequest
+from zope.publisher.http import HTTPRequest
+from zope.publisher.xmlrpc import XMLRPCRequest
+from zope.component.tests.placelesssetup import PlacelessSetup
+
+from zope.app.publication.httpfactory import HTTPPublicationRequestFactory
+from zope.app.publication.browser import BrowserPublication
+from zope.app.publication.http import HTTPPublication
+from zope.app.publication.xmlrpc import XMLRPCPublication
+from zope.app.testing import ztapi
+from zope.app.publication import interfaces
+from zope.app.publication.publicationfactories import SOAPFactory, XMLRPCFactory, HTTPFactory, BrowserFactory 
+from zope.app.publication.soap import SOAPPublication
+from zope.app.publication.browser import BrowserPublication
+
+class DummyRequestFactory(object):
+    def __call__(self, input_stream, env):
+        self.input_stream = input_stream
+        self.env = env
+        return self
+
+    def setPublication(self, pub):
+        self.pub = pub
+ 
+
+class Test(PlacelessSetup, TestCase):
+
+    def setUp(self):
+        super(Test, self).setUp()
+        self.__env =  {
+            'SERVER_URL':         'http://127.0.0.1',
+            'HTTP_HOST':          '127.0.0.1',
+            'CONTENT_LENGTH':     '0',
+            'GATEWAY_INTERFACE':  'TestFooInterface/1.0',
+            }
+
+    def test_soapfactory(self):
+        soaprequestfactory = DummyRequestFactory()
+        interface.directlyProvides(
+            soaprequestfactory, interfaces.ISOAPRequestFactory)
+        component.provideUtility(soaprequestfactory)
+        env = self.__env
+        factory = SOAPFactory()
+        self.assertEqual(factory.canHandle(env), False)
+        env['HTTP_SOAPACTION'] = 'server:foo'
+        self.assertEqual(factory.canHandle(env), True)
+        request, publication = factory()
+        self.assertEqual(isinstance(request, DummyRequestFactory), True)
+        self.assertEqual(publication, SOAPPublication)
+
+    def test_xmlrpcfactory(self):
+        xmlrpcrequestfactory = DummyRequestFactory()
+        interface.directlyProvides(
+            xmlrpcrequestfactory, interfaces.IXMLRPCRequestFactory)
+        component.provideUtility(xmlrpcrequestfactory)
+        env = self.__env
+        factory = XMLRPCFactory()
+        self.assertEqual(factory.canHandle(env), True)
+        request, publication = factory()
+        self.assertEqual(isinstance(request, DummyRequestFactory), True)
+        self.assertEqual(publication, XMLRPCPublication)
+
+    def test_httpfactory(self):
+        httprequestfactory = DummyRequestFactory()
+        interface.directlyProvides(
+            httprequestfactory, interfaces.IHTTPRequestFactory)
+        component.provideUtility(httprequestfactory)
+        env = self.__env
+        factory = HTTPFactory()
+        self.assertEqual(factory.canHandle(env), True)
+        request, publication = factory()
+        self.assertEqual(isinstance(request, DummyRequestFactory), True)
+        self.assertEqual(publication, HTTPPublication)
+
+    def test_browserfactory(self):
+        browserrequestfactory = DummyRequestFactory()
+        interface.directlyProvides(
+            browserrequestfactory, interfaces.IBrowserRequestFactory)
+        component.provideUtility(browserrequestfactory)
+        env = self.__env
+        factory = BrowserFactory()
+        self.assertEqual(factory.canHandle(env), True)
+        request, publication = factory()
+        self.assertEqual(isinstance(request, DummyRequestFactory), True)
+        self.assertEqual(publication, BrowserPublication)
+
+def test_suite():
+    return TestSuite((
+        makeSuite(Test),
+        ))
+
+if __name__=='__main__':
+    main(defaultTest='test_suite')



More information about the Zope3-Checkins mailing list