[Checkins] SVN: zc.resourcelibrary/trunk/ In order for the resource library to respect the exising points of pluggability in terms of resource publishing (and thus, URL generation), an attempt is now made to generate resource URLs using the 'resources view' (@@); if unsuccessful, we fall back to the previous method of crafting the URL from the site url.

Malthe Borch mborch at gmail.com
Thu Apr 30 05:20:33 EDT 2009


Log message for revision 99596:
  In order for the resource library to respect the exising points of pluggability in terms of resource publishing (and thus, URL generation), an attempt is now made to generate resource URLs using the 'resources view' (@@); if unsuccessful, we fall back to the previous method of crafting the URL from the site url.

Changed:
  U   zc.resourcelibrary/trunk/CHANGES.txt
  U   zc.resourcelibrary/trunk/src/zc/resourcelibrary/README.txt
  U   zc.resourcelibrary/trunk/src/zc/resourcelibrary/publication.py
  U   zc.resourcelibrary/trunk/src/zc/resourcelibrary/tests/tests.py

-=-
Modified: zc.resourcelibrary/trunk/CHANGES.txt
===================================================================
--- zc.resourcelibrary/trunk/CHANGES.txt	2009-04-30 06:26:26 UTC (rev 99595)
+++ zc.resourcelibrary/trunk/CHANGES.txt	2009-04-30 09:20:33 UTC (rev 99596)
@@ -7,6 +7,12 @@
 
 New features:
 
+- An attempt to generate resource URLs using the "resources view" (@@)
+  is now made; if unsuccesful, we fall back to the previous method of
+  crafting the URL by hand from the site url. This ensures that the
+  resource library respects the existing plugging points for resource
+  publishing (see ``zope.app.publisher.browser.resources``).
+
 - You can now explicitly specify where resource links should be
   inserted using the special marker comment '<!-- zc.resourcelibrary -->'.
 

Modified: zc.resourcelibrary/trunk/src/zc/resourcelibrary/README.txt
===================================================================
--- zc.resourcelibrary/trunk/src/zc/resourcelibrary/README.txt	2009-04-30 06:26:26 UTC (rev 99595)
+++ zc.resourcelibrary/trunk/src/zc/resourcelibrary/README.txt	2009-04-30 09:20:33 UTC (rev 99596)
@@ -149,6 +149,7 @@
     ...         'This is the body.</body>')
 
     >>> class View(object):
+    ...     context = getRootFolder()
     ...     def doSomething(self):
     ...         pass
 
@@ -160,6 +161,7 @@
 
     >>> import zc.resourcelibrary
     >>> class View(object):
+    ...     context = getRootFolder()
     ...     def doSomething(self):
     ...         zc.resourcelibrary.need('my-lib')
 

Modified: zc.resourcelibrary/trunk/src/zc/resourcelibrary/publication.py
===================================================================
--- zc.resourcelibrary/trunk/src/zc/resourcelibrary/publication.py	2009-04-30 06:26:26 UTC (rev 99595)
+++ zc.resourcelibrary/trunk/src/zc/resourcelibrary/publication.py	2009-04-30 09:20:33 UTC (rev 99596)
@@ -16,10 +16,15 @@
 """
 from zope import interface
 from zope.app.publication.interfaces import IBrowserRequestFactory
+from zope.app.publisher.browser.resource import Resource
+from zope.publisher.interfaces.browser import IBrowserPublisher
 from zope.publisher.browser import BrowserRequest, BrowserResponse
 from zope.publisher.browser import isHTML
+from zope.location.interfaces import ISite
 from zope.app.component.hooks import getSite
+from zope.component import queryMultiAdapter
 from zope.component import getMultiAdapter
+from zope.component import getSiteManager
 from zope.traversing.browser.interfaces import IAbsoluteURL
 
 import zc.resourcelibrary
@@ -97,28 +102,54 @@
 
     def _generateIncludes(self, libraries):
         # generate the HTML that will be included in the response
-        html = []
         site = getSite()
-        baseURL = str(getMultiAdapter((site, self._request),
-                                      IAbsoluteURL))
+        if site is None:
+            raise RuntimeError(
+                "Unable to locate resources; no site has been set.")
 
+        # look up resources view factory
+        factory = getSiteManager().adapters.lookup(
+            (ISite, interface.providedBy(self._request)),
+            interface.Interface, name="")
+
+        if IBrowserPublisher.implementedBy(factory):
+            resources = factory(site, self._request)
+        else:
+            # a setup with no resources factory is supported; in this
+            # case, we manually craft a URL to the resource publisher
+            # (see ``zope.app.publisher.browser.resource``).
+            resources = None
+            base = queryMultiAdapter(
+                (site, self._request), IAbsoluteURL, name="resource")
+            if base is None: 
+                baseURL = str(getMultiAdapter(
+                    (site, self._request), IAbsoluteURL))
+            else:
+                baseURL = str(base)
+
+        html = []
         for lib in libraries:
+            if resources is not None:
+                library_resources = resources[lib]
+
             included = zc.resourcelibrary.getIncluded(lib)
             for file_name in included:
+                if resources is not None:
+                    url = library_resources[file_name]()
+                else:
+                    url = "%s/@@/%s/%s" % (baseURL, lib, file_name)
                 if file_name.endswith('.js'):
-                    html.append('<script src="%s/@@/%s/%s" '
-                                % (baseURL, lib, file_name))
+                    html.append('<script src="%s" ' % url)
                     html.append('    type="text/javascript">')
                     html.append('</script>')
                 elif file_name.endswith('.css'):
                     html.append('<style type="text/css" media="all">')
                     html.append('  <!--')
-                    html.append('    @import url("%s/@@/%s/%s");'
-                                % (baseURL, lib, file_name))
+                    html.append('    @import url("%s");' % url)
                     html.append('  -->')
                     html.append('</style>')
                 elif file_name.endswith('.kss'):
-                    html.append('<link type="text/kss" href="%s/@@/%s/%s" rel="kinetic-stylesheet" />' % (baseURL, lib, file_name))
+                    html.append('<link type="text/kss" href="%s" rel="kinetic-stylesheet" />' % url)
                 else:
                     # shouldn't get here; zcml.py is supposed to check includes
                     raise RuntimeError('Resource library doesn\'t know how to '

Modified: zc.resourcelibrary/trunk/src/zc/resourcelibrary/tests/tests.py
===================================================================
--- zc.resourcelibrary/trunk/src/zc/resourcelibrary/tests/tests.py	2009-04-30 06:26:26 UTC (rev 99595)
+++ zc.resourcelibrary/trunk/src/zc/resourcelibrary/tests/tests.py	2009-04-30 09:20:33 UTC (rev 99596)
@@ -18,6 +18,8 @@
 from zc.resourcelibrary import publication
 from zc.resourcelibrary import tal
 from zope.app.testing import functional
+from zope.app.component.hooks import setSite
+from zope.app.component.hooks import getSite
 from zope.configuration import xmlconfig
 import zope.interface
 from zope.pagetemplate import pagetemplate
@@ -36,10 +38,14 @@
         zope.publisher.interfaces.browser.IBrowserPublisher)
 
     def __init__(self, source, checker, name):
+        self.name = name
         self.__Security_checker__ = checker
 
     def __call__(self, request):
         return self
+
+    def __getitem__(self, name):
+        return lambda: "http://localhost/@@/%s/%s" % (self.name, name)
     
     def publishTraverse(self, request, name):
         return getattr(self, name.replace('.', '_'))
@@ -47,7 +53,6 @@
     def foo_js(self):
         return 'foo = 1;\n'
 
-                       
 
 
 #### testing framework ####
@@ -60,7 +65,6 @@
         context.end()
         raise
 
-
 class TestPageTemplate(pagetemplate.PageTemplate):
     def __init__(self, view):
         self.view = view
@@ -75,6 +79,11 @@
 def zpt(s, view=None, content_type=None):
     request = publication.Request(body_instream=StringIO(''), environ={})
     zope.security.management.newInteraction(request)
+
+    # if no set has been set, try setting it the view context
+    if getSite() is None and hasattr(view, 'context'):
+        setSite(view.context)
+
     pt = TestPageTemplate(view)
 
     # if the resource library expression hasn't been registered, do so



More information about the Checkins mailing list