[Checkins] SVN: hurry.resource/branches/janjaapdriessen-resource-publisher/ Fix tests. Update changes
Jan-Jaap Driessen
jdriessen at thehealthagency.com
Fri Nov 12 11:16:42 EST 2010
Log message for revision 118383:
Fix tests. Update changes
Changed:
U hurry.resource/branches/janjaapdriessen-resource-publisher/CHANGES.txt
_U hurry.resource/branches/janjaapdriessen-resource-publisher/src/
U hurry.resource/branches/janjaapdriessen-resource-publisher/src/hurry/resource/README.txt
-=-
Modified: hurry.resource/branches/janjaapdriessen-resource-publisher/CHANGES.txt
===================================================================
--- hurry.resource/branches/janjaapdriessen-resource-publisher/CHANGES.txt 2010-11-12 16:15:36 UTC (rev 118382)
+++ hurry.resource/branches/janjaapdriessen-resource-publisher/CHANGES.txt 2010-11-12 16:16:42 UTC (rev 118383)
@@ -4,9 +4,19 @@
0.11 (unreleased)
=================
-- Nothing changed yet.
+- Add entry point for wsgi middleware.
+- Moved ZCA plugin to hurry.zoperesource.
+- Plugins now have two responsibilities:
+
+ 1. Leaving a NeededInclusions() object in the WSGI environment.
+
+ 2. Leaving a `base_url` attribute on this NeededInclusions object.
+
+- The WSGI `injector` middleware takes care of rendering the needed inclusions
+ into the HTML.
+
0.10 (2010-07-24)
=================
@@ -14,12 +24,12 @@
dependency. Don't expose it into the ``__init__.py`` therefore, as
that needs to be independent of WebOb. To use the middleware import
it directly from ``hurry.resource.wsgi``.
-
+
* The ``Library`` factory now has to be called with a rootpath
argument as the second argument. This is a path to the actual
resource directory that the library represents, relative to the
directory of the package. This is needed to make `` hurry.resource``
- useful for frameworks not based on ``zope.configuration``.
+ useful for frameworks not based on ``zope.configuration``.
This breaks backwards compatibility with previous versions of
``hurry.resource``; all code that uses ``Library`` should be
@@ -37,7 +47,7 @@
applications. If the application supplies a ``NeededInclusions``
object in ``environ`` with the key ``hurry.resource.needed``, the
middleware will pick up on this and insert the needed inclusions.
-
+
The WebOb library is needed to make this work and depending on
``hurry.resource [wsgi]`` will pull in the required dependency.
@@ -106,7 +116,7 @@
* Added support for rendering resources into two fragments, one to
be included at the top of the HTML page in the ``<head>`` section,
- the other to be included just before the ``</body>`` section. In
+ the other to be included just before the ``</body>`` section. In
some circumstances doing this can `speed up page load time`_.
.. _`speed up page load time`: http://developer.yahoo.net/blog/archives/2007/07/high_performanc_5.html
Property changes on: hurry.resource/branches/janjaapdriessen-resource-publisher/src
___________________________________________________________________
Added: svn:ignore
+ *egg-info
Modified: hurry.resource/branches/janjaapdriessen-resource-publisher/src/hurry/resource/README.txt
===================================================================
--- hurry.resource/branches/janjaapdriessen-resource-publisher/src/hurry/resource/README.txt 2010-11-12 16:15:36 UTC (rev 118382)
+++ hurry.resource/branches/janjaapdriessen-resource-publisher/src/hurry/resource/README.txt 2010-11-12 16:16:42 UTC (rev 118383)
@@ -111,8 +111,8 @@
Let's now see what resources are needed by this inclusion::
>>> needed.inclusions() #doctest: +NORMALIZE_WHITESPACE
- [<ResourceInclusion 'b.css' in library 'foo'>,
- <ResourceInclusion 'a.js' in library 'foo'>,
+ [<ResourceInclusion 'b.css' in library 'foo'>,
+ <ResourceInclusion 'a.js' in library 'foo'>,
<ResourceInclusion 'c.js' in library 'foo'>]
As you can see, ``css`` resources are sorted before ``js`` resources.
@@ -131,7 +131,7 @@
>>> needed = NeededInclusions()
>>> needed.need(group)
>>> group.inclusions() #doctest: +NORMALIZE_WHITESPACE
- [<ResourceInclusion 'a.js' in library 'foo'>,
+ [<ResourceInclusion 'a.js' in library 'foo'>,
<ResourceInclusion 'b.css' in library 'foo'>]
A group can also be depended on; it won't show up in the list of
@@ -139,7 +139,7 @@
>>> more_stuff = ResourceInclusion(foo, 'more_stuff.js', depends=[group])
>>> more_stuff.inclusions() #doctest: +NORMALIZE_WHITESPACE
- [<ResourceInclusion 'a.js' in library 'foo'>,
+ [<ResourceInclusion 'a.js' in library 'foo'>,
<ResourceInclusion 'b.css' in library 'foo'>,
<ResourceInclusion 'more_stuff.js' in library 'foo'>]
@@ -156,7 +156,7 @@
We can require a resource without reference to the needed inclusions
object directly as there is typically only a single set of needed
-inclusions that is generated during the rendering of a page.
+inclusions that is generated during the rendering of a page.
So let's try out this spelling to see it fail::
@@ -176,7 +176,7 @@
web page that has the inclusions on them. One place where
we can maintain the needed inclusions is the request object
itself, if we indeed have global access to it. Alternatively you could
-store the currently needed inclusions in a thread local variable.
+store the currently needed inclusions in a thread local variable.
Let's introduce a simple request object (your mileage may vary in your
own web framework)::
@@ -205,7 +205,7 @@
There is an API to retrieve the current needed inclusions, so let's
check which resources our request needs currently::
-
+
>>> from hurry.resource import get_current_needed_inclusions
>>> get_current_needed_inclusions().inclusions()
[]
@@ -217,8 +217,8 @@
The resource inclusion will now indeed be needed::
>>> get_current_needed_inclusions().inclusions() #doctest: +NORMALIZE_WHITESPACE
- [<ResourceInclusion 'b.css' in library 'foo'>,
- <ResourceInclusion 'a.js' in library 'foo'>,
+ [<ResourceInclusion 'b.css' in library 'foo'>,
+ <ResourceInclusion 'a.js' in library 'foo'>,
<ResourceInclusion 'c.js' in library 'foo'>]
Let's go back to the original spelling of ``needed.need(y)``
@@ -239,7 +239,7 @@
the mode system. You can use ``hurry.resource.mode('minified')``
somewhere in the request handling of your application. This will
make sure that resources included on your page are supplied as
- minified versions, if these are available.
+ minified versions, if these are available.
* rolling up of resources. Resource libraries can specify rollup
resources that combine multiple resources into one. This reduces the
@@ -279,8 +279,8 @@
>>> needed.need(y1)
>>> needed.inclusions() #doctest: +NORMALIZE_WHITESPACE
- [<ResourceInclusion 'b.css' in library 'foo'>,
- <ResourceInclusion 'a.js' in library 'foo'>,
+ [<ResourceInclusion 'b.css' in library 'foo'>,
+ <ResourceInclusion 'a.js' in library 'foo'>,
<ResourceInclusion 'c.js' in library 'foo'>]
Needing the same inclusion twice won't make any difference for the
@@ -289,8 +289,8 @@
>>> needed.need(y1)
>>> needed.inclusions() #doctest: +NORMALIZE_WHITESPACE
- [<ResourceInclusion 'b.css' in library 'foo'>,
- <ResourceInclusion 'a.js' in library 'foo'>,
+ [<ResourceInclusion 'b.css' in library 'foo'>,
+ <ResourceInclusion 'a.js' in library 'foo'>,
<ResourceInclusion 'c.js' in library 'foo'>]
Needing ``x1`` or ``x2`` won't make any difference either, as ``y1``
@@ -298,13 +298,13 @@
>>> needed.need(x1)
>>> needed.inclusions() #doctest: +NORMALIZE_WHITESPACE
- [<ResourceInclusion 'b.css' in library 'foo'>,
- <ResourceInclusion 'a.js' in library 'foo'>,
+ [<ResourceInclusion 'b.css' in library 'foo'>,
+ <ResourceInclusion 'a.js' in library 'foo'>,
<ResourceInclusion 'c.js' in library 'foo'>]
>>> needed.need(x2)
>>> needed.inclusions() #doctest: +NORMALIZE_WHITESPACE
- [<ResourceInclusion 'b.css' in library 'foo'>,
- <ResourceInclusion 'a.js' in library 'foo'>,
+ [<ResourceInclusion 'b.css' in library 'foo'>,
+ <ResourceInclusion 'a.js' in library 'foo'>,
<ResourceInclusion 'c.js' in library 'foo'>]
Let's do it in reverse, and require the ``x1`` and ``x2`` resources
@@ -316,7 +316,7 @@
>>> needed.need(y1)
>>> needed.inclusions() #doctest: +NORMALIZE_WHITESPACE
[<ResourceInclusion 'b.css' in library 'foo'>,
- <ResourceInclusion 'a.js' in library 'foo'>,
+ <ResourceInclusion 'a.js' in library 'foo'>,
<ResourceInclusion 'c.js' in library 'foo'>]
Let's try it with more complicated dependency structures now::
@@ -371,10 +371,10 @@
>>> needed.need(a3)
>>> needed.need(a5)
>>> needed.inclusions() #doctest: +NORMALIZE_WHITESPACE
- [<ResourceInclusion 'a1.js' in library 'foo'>,
- <ResourceInclusion 'a2.js' in library 'foo'>,
- <ResourceInclusion 'a3.js' in library 'foo'>,
- <ResourceInclusion 'a4.js' in library 'foo'>,
+ [<ResourceInclusion 'a1.js' in library 'foo'>,
+ <ResourceInclusion 'a2.js' in library 'foo'>,
+ <ResourceInclusion 'a3.js' in library 'foo'>,
+ <ResourceInclusion 'a4.js' in library 'foo'>,
<ResourceInclusion 'a5.js' in library 'foo'>]
Modes
@@ -406,7 +406,7 @@
Modes can also be specified fully with a resource inclusion, which allows
you to specify a different ``library`` argumnent::
- >>> k2 = ResourceInclusion(foo, 'k2.js',
+ >>> k2 = ResourceInclusion(foo, 'k2.js',
... debug=ResourceInclusion(foo, 'k2-debug.js'))
>>> needed = NeededInclusions()
>>> needed.need(k2)
@@ -550,7 +550,7 @@
>>> c2 = ResourceInclusion(foo, 'c2.css')
>>> c3 = ResourceInclusion(foo, 'c3.css')
>>> giantc = ResourceInclusion(foo, 'giantc.css', supersedes=[c1, c2, c3])
-
+
It will not roll up one resource::
>>> needed = NeededInclusions()
@@ -568,7 +568,7 @@
>>> needed.inclusions() #doctest: +NORMALIZE_WHITESPACE
[<ResourceInclusion 'c1.css' in library 'foo'>,
<ResourceInclusion 'c2.css' in library 'foo'>]
-
+
It will however roll up three resources::
>>> needed = NeededInclusions()
@@ -622,7 +622,7 @@
be taken::
>>> d4 = ResourceInclusion(foo, 'd4.js')
- >>> giantd_bigger = ResourceInclusion(foo, 'giantd-bigger.js',
+ >>> giantd_bigger = ResourceInclusion(foo, 'giantd-bigger.js',
... supersedes=[d1, d2, d3, d4], eager_superseder=True)
>>> needed = NeededInclusions()
>>> needed.rollup()
@@ -649,7 +649,7 @@
that rolls up the most resources is used. So, if there are two
potential non-eager superseders, the one that rolls up the most
resources will be used::
-
+
>>> e1 = ResourceInclusion(foo, 'e1.js')
>>> e2 = ResourceInclusion(foo, 'e2.js')
>>> e3 = ResourceInclusion(foo, 'e3.js')
@@ -713,7 +713,7 @@
>>> needed.inclusions() #doctest: +NORMALIZE_WHITESPACE
[<ResourceInclusion 'gianth.js' in library 'foo'>]
-Since there is no superseder for the debug mode, we will get the two
+Since there is no superseder for the debug mode, we will get the two
resources, not rolled up::
>>> needed.mode('debug')
@@ -729,8 +729,8 @@
>>> needed = NeededInclusions()
>>> needed.need(y1)
>>> needed.inclusions() #doctest: +NORMALIZE_WHITESPACE
- [<ResourceInclusion 'b.css' in library 'foo'>,
- <ResourceInclusion 'a.js' in library 'foo'>,
+ [<ResourceInclusion 'b.css' in library 'foo'>,
+ <ResourceInclusion 'a.js' in library 'foo'>,
<ResourceInclusion 'c.js' in library 'foo'>]
Now let's try to render these inclusions::
@@ -738,26 +738,22 @@
>>> print needed.render()
Traceback (most recent call last):
...
- AttributeError: 'Plugin' object has no attribute 'get_library_url'
+ AttributeError: 'NoneType' object has no attribute 'endswith'
+
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. We
+``hurry.resource`` the base 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.
-We'll extend the existing plugin that already knows how to obtain the
-current needed inclusions. For the purposes of this document, we
-define a function that renders resources as some static URL on
-localhost::
+Before rendering the resources, the base_url attribute has to be set
+on the needed inclusions.
- >>> class NewPlugin(Plugin):
- ... def get_library_url(self, library):
- ... return 'http://localhost/static/%s' % library.name
+For the purposes of this document, we define a function that renders
+resources as some static URL on localhost::
-Let's register the plugin::
+ >>> needed.base_url = 'http://localhost/static/'
- >>> register_plugin(NewPlugin())
-
Rendering the inclusions now will result in the HTML fragments we
need to include on the top of our page (just under the ``<head>`` tag
for instance)::
@@ -768,7 +764,7 @@
<script type="text/javascript" src="http://localhost/static/foo/c.js"></script>
Let's set this a currently needed inclusions::
-
+
>>> request.needed = needed
There is a function available as well for rendering the resources for
@@ -784,7 +780,7 @@
===========================
When you have the HTML it can be convenient to have a way to insert
-resources directly into some HTML.
+resources directly into some HTML.
The insertion system assumes a HTML text that has a ``<head>`` tag in it::
@@ -819,7 +815,7 @@
It's also possible to render the resource inclusions into two
fragments, some to be included just after the ``<head>`` tag, but some
to be included at the very bottom of the HTML page, just before the
-``</body>`` tag. This is useful as it can `speed up page load times`_.
+``</body>`` tag. This is useful as it can `speed up page load times`_.
.. _`speed up page load times`: http://developer.yahoo.com/performance/rules.html
@@ -842,7 +838,7 @@
Since none of the resources indicated it was safe to render them at
the bottom, even this explicit call will not result in any changes::
-
+
>>> top, bottom = needed.render_topbottom()
>>> print top
<link rel="stylesheet" type="text/css" href="http://localhost/static/foo/b.css" />
@@ -864,13 +860,14 @@
Let's now introduce a javascript resource that says it is safe to be
included on the bottom::
-
+
>>> y2 = ResourceInclusion(foo, 'y2.js', bottom=True)
When we start over without ``bottom`` enabled, we get this resource
show up in the top fragment after all::
>>> needed = NeededInclusions()
+ >>> needed.base_url = 'http://localhost/static/'
>>> needed.need(y1)
>>> needed.need(y2)
@@ -975,23 +972,24 @@
We create a simple WSGI application. In our application we declare that
we need a resource (``y1``) and put that in the WSGI ``environ`` under the
key ``hurry.resource.needed``::
-
+
>>> def app(environ, start_response):
... start_response('200 OK', [])
... needed = environ['hurry.resource.needed'] = NeededInclusions()
... needed.need(y1)
+ ... needed.base_url = 'http://localhost/static/'
... return ['<html><head></head><body</body></html>']
We now wrap this in our middleware, so that the middleware is activated::
- >>> from hurry.resource.wsgi import Middleware
- >>> wrapped_app = Middleware(app)
+ >>> from hurry.resource.wsgi import InjectMiddleWare
+ >>> wrapped_app = InjectMiddleWare(app)
Now we make a request (using webob for convenience)::
>>> import webob
>>> req = webob.Request.blank('/')
- >>> res = req.get_response(wrapped_app)
+ >>> res = req.get_response(wrapped_app)
We can now see that the resources are added to the HTML by the middleware::
@@ -1011,7 +1009,7 @@
... needed = environ['hurry.resource.needed'] = NeededInclusions()
... needed.need(y1)
... return ['<html><head></head><body</body></html>']
- >>> wrapped_app = Middleware(app)
+ >>> wrapped_app = InjectMiddleWare(app)
>>> req = webob.Request.blank('/')
>>> res = req.get_response(wrapped_app)
>>> res.body
@@ -1030,6 +1028,7 @@
Let's look at the resources needed by default::
>>> c = get_current_needed_inclusions()
+ >>> c.base_url = 'http://localhost/static'
>>> top, bottom = c.render_topbottom()
>>> print top
<script type="text/javascript" src="http://localhost/static/foo/l1.js"></script>
@@ -1125,10 +1124,10 @@
>>> i = [a5, a3, a1, a2, a4]
>>> sort_inclusions_topological(i) #doctest: +NORMALIZE_WHITESPACE
- [<ResourceInclusion 'a1.js' in library 'foo'>,
- <ResourceInclusion 'a4.js' in library 'foo'>,
- <ResourceInclusion 'a2.js' in library 'foo'>,
- <ResourceInclusion 'a3.js' in library 'foo'>,
+ [<ResourceInclusion 'a1.js' in library 'foo'>,
+ <ResourceInclusion 'a4.js' in library 'foo'>,
+ <ResourceInclusion 'a2.js' in library 'foo'>,
+ <ResourceInclusion 'a3.js' in library 'foo'>,
<ResourceInclusion 'a5.js' in library 'foo'>]
@@ -1154,7 +1153,7 @@
>>> a6 = ResourceInclusion(foo, 'nothing.unknown')
>>> from hurry.resource.core import render_inclusions
- >>> render_inclusions([a6]) #doctest: +NORMALIZE_WHITESPACE +ELLIPSIS
+ >>> render_inclusions([a6], '/') #doctest: +NORMALIZE_WHITESPACE +ELLIPSIS
Traceback (most recent call last):
...
UnknownResourceExtension: Unknown resource extension .unknown for resource
@@ -1167,5 +1166,5 @@
... return '<link rel="unknown" href="%s" />' % url
>>> inclusion_renderers['.unknown'] = render_unknown
- >>> render_inclusions([a6])
+ >>> render_inclusions([a6], 'http://localhost/static/')
'<link rel="unknown" href="http://localhost/static/foo/nothing.unknown" />'
More information about the checkins
mailing list