[Zope3-checkins] SVN: Zope3/branches/ajung-target-requestpublication-next-try-branch/src/zope/app/publication/ first bleeding-edge implementation

Andreas Jung andreas at andreas-jung.com
Fri Oct 7 06:15:37 EDT 2005


Log message for revision 38858:
  first bleeding-edge implementation
  

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/requestpublicationregistry.py
  A   Zope3/branches/ajung-target-requestpublication-next-try-branch/src/zope/app/publication/tests/test_requestpublicationregistry.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 10:08:38 UTC (rev 38857)
+++ Zope3/branches/ajung-target-requestpublication-next-try-branch/src/zope/app/publication/interfaces.py	2005-10-07 10:15:36 UTC (rev 38858)
@@ -112,3 +112,8 @@
         """ Lookup a factory for a given method+mimetype and a 
             environment. 
         """
+
+    def getFactoriesFor(method, mimetype):
+        """ return the internal datastructure representing the configured
+            factories (basically for testing, not for introspection)
+        """

Added: 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 10:08:38 UTC (rev 38857)
+++ Zope3/branches/ajung-target-requestpublication-next-try-branch/src/zope/app/publication/requestpublicationregistry.py	2005-10-07 10:15:36 UTC (rev 38858)
@@ -0,0 +1,59 @@
+##############################################################################
+#
+# 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.
+#
+##############################################################################
+"""A registry for Request-Publication factories.
+
+
+$Id: publicationfactories.py 38841 2005-10-07 04:34:09Z andreasjung $
+"""
+__docformat__ = 'restructuredtext'
+
+from zope.interface import implements
+from zope.app.publication.interfaces import IRequestPublicationRegistry
+
+class RequestPublicationRegistry(object):
+
+    implements(IRequestPublicationRegistry)
+
+    def __init__(self):
+        self._d = {}   # method -> { mimetype -> [factories]}
+
+    def register(self, method, mimetype, name, priority, factory):
+        """ registers a factory for method+mimetype """
+
+        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
+            if d['name'] == name:
+                del l[pos]
+                break
+        l.append({'name' : name, 'factory' : factory, 'priority' : priority})
+        l.sort(lambda x,y: cmp(x['priority'], y['priority']))
+
+    def getFactoriesFor(self, method, mimetype):
+        try:
+            return self._d[method][mimetype]
+        except:
+            return None
+        
+
+    def lookup(self, 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/tests/test_requestpublicationregistry.py
===================================================================
--- Zope3/branches/ajung-target-requestpublication-next-try-branch/src/zope/app/publication/tests/test_requestpublicationregistry.py	2005-10-07 10:08:38 UTC (rev 38857)
+++ Zope3/branches/ajung-target-requestpublication-next-try-branch/src/zope/app/publication/tests/test_requestpublicationregistry.py	2005-10-07 10:15:36 UTC (rev 38858)
@@ -0,0 +1,64 @@
+##############################################################################
+#
+# 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.interface.verify import verifyClass
+from zope.component.tests.placelesssetup import PlacelessSetup
+
+from zope.app.publication.interfaces import IRequestPublicationRegistry
+from zope.app.publication.requestpublicationregistry import RequestPublicationRegistry
+
+
+def DummyFactory():
+    return object
+
+class Test(PlacelessSetup, TestCase):
+
+    def setUp(self):
+        super(Test, self).setUp()
+        self._registry = RequestPublicationRegistry()
+
+    def test_interface(self):
+        verifyClass(IRequestPublicationRegistry, RequestPublicationRegistry)
+
+    def test_registration(self):
+        r = self._registry
+        xmlrpc_f = DummyFactory()
+        r.register('POST', 'text/xml', 'xmlrpc', 0, xmlrpc_f)
+        soap_f = DummyFactory()
+        r.register('POST', 'text/xml', 'soap', 1, soap_f)
+        browser_f = DummyFactory()
+        r.register('*', '*', 'browser_default', 0, browser_f)
+        l = r.getFactoriesFor('POST', 'text/xml')
+        self.assertEqual(l, [{'name' : 'xmlrpc', 'priority' : 0, 'factory' : object},
+                             {'name' : 'soap', 'priority' : 1, 'factory' : object},
+                            ])
+        self.assertEqual(r.getFactoriesFor('POST', 'text/html'), None)
+
+
+
+def test_suite():
+    return TestSuite((
+        makeSuite(Test),
+        ))
+
+if __name__=='__main__':
+    main(defaultTest='test_suite')



More information about the Zope3-Checkins mailing list