[Checkins] SVN: hurry.zoperesource/branches/janjaapdriessen-wsgi/ pass on the library object from directory resource to (directory) resource in order to effectively compute a hurry.resource URL

Jan-Wijbrand Kolman janwijbrand at gmail.com
Tue Nov 23 06:49:50 EST 2010


Log message for revision 118527:
  pass on the library object from directory resource to (directory) resource in order to effectively compute a hurry.resource URL

Changed:
  U   hurry.zoperesource/branches/janjaapdriessen-wsgi/buildout.cfg
  U   hurry.zoperesource/branches/janjaapdriessen-wsgi/src/hurry/zoperesource/README.txt
  U   hurry.zoperesource/branches/janjaapdriessen-wsgi/src/hurry/zoperesource/tests/tests.py
  U   hurry.zoperesource/branches/janjaapdriessen-wsgi/src/hurry/zoperesource/zcml.py
  U   hurry.zoperesource/branches/janjaapdriessen-wsgi/src/hurry/zoperesource/zopesupport.py

-=-
Modified: hurry.zoperesource/branches/janjaapdriessen-wsgi/buildout.cfg
===================================================================
--- hurry.zoperesource/branches/janjaapdriessen-wsgi/buildout.cfg	2010-11-23 10:28:14 UTC (rev 118526)
+++ hurry.zoperesource/branches/janjaapdriessen-wsgi/buildout.cfg	2010-11-23 11:49:49 UTC (rev 118527)
@@ -11,6 +11,7 @@
 hurry.resource = svn svn+ssh://svn.zope.org/repos/main/hurry.resource/branches/janjaapdriessen-resource-publisher
 
 [versions]
+zope.app.wsgi = 3.10.0
 
 [test]
 recipe = zc.recipe.testrunner

Modified: hurry.zoperesource/branches/janjaapdriessen-wsgi/src/hurry/zoperesource/README.txt
===================================================================
--- hurry.zoperesource/branches/janjaapdriessen-wsgi/src/hurry/zoperesource/README.txt	2010-11-23 10:28:14 UTC (rev 118526)
+++ hurry.zoperesource/branches/janjaapdriessen-wsgi/src/hurry/zoperesource/README.txt	2010-11-23 11:49:49 UTC (rev 118527)
@@ -26,7 +26,7 @@
   >>> print browser.contents
   <html>
   <head>
-      <script type="text/javascript" src="http://localhost/media/foo/hash:.../a.js"></script>
+      <script type="text/javascript" src="http://localhost/@@/:hash:.../foo/a.js"></script>
   <BLANKLINE>
   </head>
   <body>
@@ -41,8 +41,8 @@
   >>> print browser.contents
   <html>
   <head>
-      <script type="text/javascript" src="http://localhost/media/foo/hash:.../a.js"></script>
-    <script type="text/javascript" src="http://localhost/media/foo/hash:.../b.js"></script>
+    <script type="text/javascript" src="http://localhost/@@/:hash:.../foo/a.js"></script>
+    <script type="text/javascript" src="http://localhost/@@/:hash:.../foo/b.js"></script>
   <BLANKLINE>
   </head>
   <body>
@@ -60,8 +60,8 @@
   </head>
   <body>
   <p>the widget HTML itself</p>
-  <script type="text/javascript" src="http://localhost/media/foo/hash:.../a.js"></script>
-  <script type="text/javascript" src="http://localhost/media/foo/hash:.../b.js"></script></body>
+  <script type="text/javascript" src="http://localhost/@@/:hash:.../foo/a.js"></script>
+  <script type="text/javascript" src="http://localhost/@@/:hash:.../foo/b.js"></script></body>
   </html>
 
 In-template resources
@@ -77,7 +77,7 @@
   </head>
   <body>
     <img src="http://localhost/media/foo/hash:.../evencaveman.jpg"/>
-    <img src="http://localhost/media/foo/hash:.../sub/evencaveman.jpg"/>
+    <img src="http://localhost/media/foo/hash:.../evencaveman.jpg"/>
   </body>
   </html>
 

Modified: hurry.zoperesource/branches/janjaapdriessen-wsgi/src/hurry/zoperesource/tests/tests.py
===================================================================
--- hurry.zoperesource/branches/janjaapdriessen-wsgi/src/hurry/zoperesource/tests/tests.py	2010-11-23 10:28:14 UTC (rev 118526)
+++ hurry.zoperesource/branches/janjaapdriessen-wsgi/src/hurry/zoperesource/tests/tests.py	2010-11-23 11:49:49 UTC (rev 118527)
@@ -11,7 +11,6 @@
 from hurry.zoperesource.tests.view import foo
 import hurry.zoperesource.tests
 
-
 class HurryResourceBrowserLayer(BrowserLayer):
 
     def testSetUp(self):
@@ -24,11 +23,9 @@
         gsm.registerAdapter(
             resource_factory, (IBrowserRequest,), Interface, foo.name)
 
-
     def setup_middleware(self, app):
-        return InjectMiddleWare(app, publisher_prefix='/media')
+        return InjectMiddleWare(app)
 
-
 def test_suite():
     readme = doctest.DocFileSuite(
         '../README.txt',

Modified: hurry.zoperesource/branches/janjaapdriessen-wsgi/src/hurry/zoperesource/zcml.py
===================================================================
--- hurry.zoperesource/branches/janjaapdriessen-wsgi/src/hurry/zoperesource/zcml.py	2010-11-23 10:28:14 UTC (rev 118526)
+++ hurry.zoperesource/branches/janjaapdriessen-wsgi/src/hurry/zoperesource/zcml.py	2010-11-23 11:49:49 UTC (rev 118527)
@@ -7,16 +7,21 @@
 from hurry.zoperesource.zopesupport import Plugin
 from hurry.zoperesource.zopesupport import HurryDirectoryResourceFactory
 
-
 def create_resource_factory(library):
-    allowed_resource_names = ('GET', 'HEAD', 'publishTraverse',
-                              'browserDefault', 'request', '__call__')
+    allowed_names = (
+        'GET',
+        'HEAD',
+        '__call__',
+        '__getitem__',
+        'browserDefault',
+        'get',
+        'publishTraverse',
+        'request',
+        )
+    checker = NamesChecker(allowed_names)
+    factory = HurryDirectoryResourceFactory(library, checker)
+    return factory
 
-    allowed_resourcedir_names = allowed_resource_names + ('__getitem__', 'get')
-
-    checker = NamesChecker(allowed_resourcedir_names)
-    return HurryDirectoryResourceFactory(library.path, checker, library.name)
-
 def action_setup(_context):
     """Publish all hurry.resource library entry points as resources.
     """

Modified: hurry.zoperesource/branches/janjaapdriessen-wsgi/src/hurry/zoperesource/zopesupport.py
===================================================================
--- hurry.zoperesource/branches/janjaapdriessen-wsgi/src/hurry/zoperesource/zopesupport.py	2010-11-23 10:28:14 UTC (rev 118526)
+++ hurry.zoperesource/branches/janjaapdriessen-wsgi/src/hurry/zoperesource/zopesupport.py	2010-11-23 11:49:49 UTC (rev 118527)
@@ -1,17 +1,16 @@
 # zope integration for hurry.resource
-from zope.interface import alsoProvides
-from zope.component import adapts, adapter
-import zope.security.management
-
+from zope.browserresource import directory
+from zope.component import adapts, adapter, getMultiAdapter
+from zope.interface import alsoProvides, implements
 from zope.publisher.interfaces import IEndRequestEvent, IRequest
 from zope.publisher.interfaces.browser import IBrowserRequest
-from zope.traversing.browser.absoluteurl import absoluteURL
+from zope.site.hooks import getSite
+from zope.traversing.browser.interfaces import IAbsoluteURL
 import zope.browserresource.resource
-from zope.browserresource.directory import DirectoryResourceFactory
-from zope.browserresource.directory import DirectoryResource
+import zope.security.management
 
 from hurry.resource import NeededInclusions
-from hurry.resource.wsgi import NEEDED, PUBLISHER_PREFIX
+from hurry.resource.wsgi import NEEDED
 
 from hurry.zoperesource.interfaces import IHurryResource
 
@@ -40,12 +39,10 @@
     """
     def get_current_needed_inclusions(self):
         request = getRequest()
-
-        # Find the NeededInclusions object in the WSGI environment;
-        # If none can be found, create a new one and add it to the
-        # environment.
-        # Unfortunately we don't have easy access to the WSGI environment,
-        # so we have to use request._orig_env.
+        # Find the NeededInclusions object in the WSGI environment; If
+        # none can be found, create a new one and add it to the
+        # environment.  Unfortunately we don't have easy access to the
+        # WSGI environment, so we have to use request._orig_env.
         return request._orig_env.setdefault(NEEDED, NeededInclusions())
 
 @adapter(IEndRequestEvent)
@@ -54,43 +51,65 @@
     # Unfortunately we don't have easy access to the WSGI environment,
     # so we have to use request._orig_env.
     needed = request._orig_env.get(NEEDED)
-    # Only set the base_url if resources have been needed during this request.
     if needed is not None and needed.base_url is None:
-        publisher_prefix = request._orig_env.get(PUBLISHER_PREFIX)
-        # Compute URLs to the resource publisher,
-        # Taking into account skins and virtual host specifications
-        # XXX Do we need to skip ++skin++ information?
-        absolute_url = absoluteURL(None, request)
-        if publisher_prefix is not None:
-            needed.base_url = absolute_url + publisher_prefix
-        else:
-            needed.base_url = absolute_url + '/@@/'
+        # Only set the base_url if resources have been needed during
+        # this request.
+        site_url = str(getMultiAdapter((getSite(), request), IAbsoluteURL))
+        needed.base_url = '%s/@@' % site_url
 
-# Adapter for constructing URLs from page templates using
-# `context/++resource++foo` that may point to the hurry.resource publisher.
-class AbsoluteURL(zope.browserresource.resource.AbsoluteURL):
+# Custom DirectoryResource(Factory) implementations that allow to
+# inject the library object that the IAbsoluteURL adapter can use.
 
-    adapts(IHurryResource, IBrowserRequest)
+def hurrify(resource, library):
+    alsoProvides(resource, IHurryResource)
+    resource.library = library
+    return resource
 
-    def __str__(self):
-        request = self.request
-        url = absoluteURL(None, request)
-        publisher_prefix = request._orig_env.get(PUBLISHER_PREFIX)
-        name = self.context.__name__
-        if publisher_prefix is None:
-            return self._createUrl(url, name)
-        return url + publisher_prefix + '/' + name
+class DirectoryResource(directory.DirectoryResource):
 
+    implements(IHurryResource)
 
-class HurryDirectoryResource(DirectoryResource):
+    def get(self, name, *args, **kw):
+        resource = super(DirectoryResource, self).get(name, *args, **kw)
+        return hurrify(resource, self.library)
 
-    def get(self, *args, **kwargs):
-        result = super(HurryDirectoryResource, self).get(*args, **kwargs)
-        alsoProvides(result, IHurryResource)
-        return result
+class DirectoryResourceFactory(directory.DirectoryResourceFactory):
 
+    factoryClass = DirectoryResource
+
+    def __call__(self, request):
+        resource = super(DirectoryResourceFactory, self).__call__(request)
+        return hurrify(resource, self.library)
+
+# Close the cirular relationship between resource and resource factory
+# for directories.
+def directory_resource_factory(self, path, checker, name):
+    directory_resource = DirectoryResourceFactory(path, checker, name)
+    return hurrify(directory_resource, self.library)
+
+DirectoryResource.directory_factory = directory_resource_factory
+
+# "Top-level" directory resource factory, that allows us to inject the
+# library object. This, with the custom DirectoryResource(Factory)
+# implementation then is used to inject the libary object as an
+# attribute on all the subsequent resources. The IAbsoluteURL adapter
+# for IHurryResource is thus able to compute library URLs.
 class HurryDirectoryResourceFactory(DirectoryResourceFactory):
 
-    factoryClass = HurryDirectoryResource
+    def __init__(self, library, checker):
+        super(HurryDirectoryResourceFactory, self).__init__(
+            library.path, checker, library.name)
+        self.library = library
 
-HurryDirectoryResource.directory_factory = HurryDirectoryResourceFactory
+# Adapter for constructing URLs from page templates using
+# `context/++resource++foo` that may point to the hurry.resource
+# publisher.
+class AbsoluteURL(zope.browserresource.resource.AbsoluteURL):
+
+    adapts(IHurryResource, IBrowserRequest)
+
+    def __str__(self):
+        site_url = str(
+            getMultiAdapter((getSite(), self.request), IAbsoluteURL))
+        return '%s/@@/%s/%s' % (
+            site_url, self.context.library.signature(), self.context.__name__)



More information about the checkins mailing list