[Checkins] SVN: hurry.resource/trunk/src/hurry/resource/ Calculate the URL to the library, not to the full resource, as this

Martijn Faassen faassen at infrae.com
Mon Oct 6 12:11:34 EDT 2008


Log message for revision 91812:
  Calculate the URL to the library, not to the full resource, as this
  is already library_url + relpath, which the framework can do for us.
  

Changed:
  U   hurry.resource/trunk/src/hurry/resource/README.txt
  U   hurry.resource/trunk/src/hurry/resource/core.py
  U   hurry.resource/trunk/src/hurry/resource/interfaces.py

-=-
Modified: hurry.resource/trunk/src/hurry/resource/README.txt
===================================================================
--- hurry.resource/trunk/src/hurry/resource/README.txt	2008-10-06 15:14:05 UTC (rev 91811)
+++ hurry.resource/trunk/src/hurry/resource/README.txt	2008-10-06 16:11:33 UTC (rev 91812)
@@ -543,25 +543,31 @@
   >>> print needed.render()
   Traceback (most recent call last):
     ...
-  ComponentLookupError: (<InterfaceClass hurry.resource.interfaces.IInclusionUrl>, '')
+  TypeError: ('Could not adapt', <hurry.resource.core.Library object at ...>, <InterfaceClass hurry.resource.interfaces.ILibraryUrl>)
 
 That didn't work. In order to render an inclusion, we need to tell
-``hurry.resource`` how to get the URL for a resource inclusion. 
+``hurry.resource`` how to get the URL for a resource inclusion. We
+already know the relative URL, so we need to specify how to get a URL
+to the library itself that the relative URL can be added to.
 
 For the purposes of this document, we define a function that renders
 resources as some static URL on localhost::
 
-  >>> def get_inclusion_url(inclusion):
-  ...    return 'http://localhost/static/%s/%s' % (
-  ...      inclusion.library.name, inclusion.relpath)
+  >>> def get_library_url(library):
+  ...    return 'http://localhost/static/%s/' % library.name
 
-We should now register this function as a``IInclusionUrl`` utility so
-the system can find it::
+Note that the library URL should end with a ``/`` so we can add
+``inclusion.relpath`` to it.
 
-  >>> from hurry.resource.interfaces import IInclusionUrl
-  >>> component.provideUtility(get_inclusion_url, 
-  ...     IInclusionUrl)
+We should now register this function as a``ILibrarUrl`` adapter for
+``Library`` so the system can find it::
 
+  >>> from hurry.resource.interfaces import ILibraryUrl
+  >>> component.provideAdapter(
+  ...     factory=get_library_url,
+  ...     adapts=(Library,), 
+  ...     provides=ILibraryUrl)
+
 Rendering the inclusions now will will result in the HTML fragment we need::
 
   >>> print needed.render()

Modified: hurry.resource/trunk/src/hurry/resource/core.py
===================================================================
--- hurry.resource/trunk/src/hurry/resource/core.py	2008-10-06 15:14:05 UTC (rev 91811)
+++ hurry.resource/trunk/src/hurry/resource/core.py	2008-10-06 16:11:33 UTC (rev 91812)
@@ -149,10 +149,15 @@
             
     def render(self, mode=None):
         result = []
-        get_inclusion_url = component.getUtility(interfaces.IInclusionUrl)
+        library_urls = {}        
         for inclusion in self.inclusions(mode):
-            url = get_inclusion_url(inclusion)
-            result.append(render_inclusion(inclusion, url))
+            library = inclusion.library
+            library_url = library_urls.get(library.name)
+            if library_url is None:
+                library_urls[library.name] = library_url =\
+                                             interfaces.ILibraryUrl(library)
+            result.append(render_inclusion(inclusion,
+                                           library_url + inclusion.relpath))
         return '\n'.join(result)
 
 def apply_mode(inclusions, mode):

Modified: hurry.resource/trunk/src/hurry/resource/interfaces.py
===================================================================
--- hurry.resource/trunk/src/hurry/resource/interfaces.py	2008-10-06 15:14:05 UTC (rev 91811)
+++ hurry.resource/trunk/src/hurry/resource/interfaces.py	2008-10-06 16:11:33 UTC (rev 91812)
@@ -98,7 +98,10 @@
         These can for instance be retrieved from the current request.
         """
 
-class IInclusionUrl(Interface):
+class ILibraryUrl(Interface):
     def __call__(inclusion):
-        """Return the URL for given resource inclusion.
+        """Return the URL for the library.
+
+        This is the URL that we can add inclusion.rel_path to, to obtain
+        the complete URL of the resource.
         """



More information about the Checkins mailing list