[Checkins] SVN: hurry.resource/trunk/src/hurry/resource/ Provide for way to render resources if IResourceUrl utility is supplied.
Martijn Faassen
faassen at infrae.com
Mon Sep 22 08:13:39 EDT 2008
Log message for revision 91325:
Provide for way to render resources if IResourceUrl utility is supplied.
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-09-22 11:58:04 UTC (rev 91324)
+++ hurry.resource/trunk/src/hurry/resource/README.txt 2008-09-22 12:13:39 UTC (rev 91325)
@@ -352,3 +352,48 @@
[<Resource 'giant.js' in library 'foo'>]
>>> needed.resources(mode='debug')
[<Resource 'giant-debug.js' in library 'foo'>]
+
+Rendering resources
+-------------------
+
+Let's define some needed resource inclusions::
+
+ >>> needed = NeededInclusions()
+ >>> needed.need(y)
+ >>> needed.resources()
+ [<Resource 'b.css' in library 'foo'>,
+ <Resource 'd.css' in library 'foo'>,
+ <Resource 'a.js' in library 'foo'>,
+ <Resource 'c.js' in library 'foo'>]
+
+Now let's try to render these inclusions::
+
+ >>> print needed.render()
+ Traceback (most recent call last):
+ ...
+ ComponentLookupError: (<InterfaceClass hurry.resource.interfaces.IResourceUrl>, '')
+
+That didn't work. In order to render a resource, we need to tell
+``hurry.resource`` how to get the URL for a resource specification. So
+let's define a function that renders resources as some static URL on
+localhost::
+
+ >>> def get_resource_url(resource):
+ ... return 'http://localhost/static/%s/%s' % (
+ ... resource.library.name, resource.relpath)
+
+We should now register this function as a``IResourceUrl`` utility so the system
+can find it::
+
+ >>> from hurry.resource.interfaces import IResourceUrl
+ >>> component.provideUtility(get_resource_url,
+ ... IResourceUrl)
+
+Rendering the resources now will will result in the HTML fragment we need::
+
+ >>> print needed.render()
+ <link rel="stylesheet" type="text/css" href="http://localhost/static/foo/b.css" />
+ <link rel="stylesheet" type="text/css" href="http://localhost/static/foo/d.css" />
+ <script type="text/javascript" src="http://localhost/static/foo/a.js"></script>
+ <script type="text/javascript" src="http://localhost/static/foo/c.js"></script>
+
Modified: hurry.resource/trunk/src/hurry/resource/core.py
===================================================================
--- hurry.resource/trunk/src/hurry/resource/core.py 2008-09-22 11:58:04 UTC (rev 91324)
+++ hurry.resource/trunk/src/hurry/resource/core.py 2008-09-22 12:13:39 UTC (rev 91325)
@@ -137,10 +137,11 @@
resources = apply_mode(resources, mode)
return remove_duplicates(consolidate(resources))
- def render(self):
+ def render(self, mode=None):
result = []
- for resource in self.resources():
- url = ''
+ get_resource_url = component.getUtility(interfaces.IResourceUrl)
+ for resource in self.resources(mode):
+ url = get_resource_url(resource)
result.append(render_resource(resource, url))
return '\n'.join(result)
Modified: hurry.resource/trunk/src/hurry/resource/interfaces.py
===================================================================
--- hurry.resource/trunk/src/hurry/resource/interfaces.py 2008-09-22 11:58:04 UTC (rev 91324)
+++ hurry.resource/trunk/src/hurry/resource/interfaces.py 2008-09-22 12:13:39 UTC (rev 91325)
@@ -14,6 +14,9 @@
A resource specification specifies a single resource in a library.
"""
+ library = Attribute("The resource library this resource is in")
+ relpath = Attribute("The relative path of the resource "
+ "within the resource library")
def ext():
"""Get the filesystem extension of this resource.
@@ -93,12 +96,26 @@
def resources(mode=None):
"""Give all resources needed.
- mode - optional argument that tries to give inclusions in
+ mode - optional argument that tries to put inclusions into
a particular mode (such as debug, minified, etc)
- Has no effect if none of the included resources know
- about that mode.
+ Has no effect if an included resource does not know
+ about that mode; the original resource will be included.
+
+ Returns a list of resource specifications needed.
"""
+ def render(self, mode=None):
+ """Render all resources
+
+ mode - optional argument that tries to put inclusions into
+ a particular mode (such as debug, minified, etc).
+ Has no effect if an included resource does not know
+ about that mode; the original resource will be included.
+
+ Returns a HTML snippet that includes the required resources.
+ """
+
+
class ICurrentNeededInclusions(Interface):
def __call__():
"""Return the current needed inclusions object.
@@ -106,3 +123,7 @@
These can for instance be retrieved from the current request.
"""
+class IResourceUrl(Interface):
+ def __call__(resource):
+ """Return the URL for given resource spec.
+ """
More information about the Checkins
mailing list