[Checkins] SVN: hurry.resource/trunk/ Allow the mode to be set on INeededInclusions, as well as with a global
Martijn Faassen
faassen at infrae.com
Mon Oct 13 14:24:38 EDT 2008
Log message for revision 92144:
Allow the mode to be set on INeededInclusions, as well as with a global
``hurry.resource.mode`` function.
Changed:
U hurry.resource/trunk/CHANGES.txt
U hurry.resource/trunk/src/hurry/resource/README.txt
U hurry.resource/trunk/src/hurry/resource/__init__.py
U hurry.resource/trunk/src/hurry/resource/core.py
U hurry.resource/trunk/src/hurry/resource/interfaces.py
-=-
Modified: hurry.resource/trunk/CHANGES.txt
===================================================================
--- hurry.resource/trunk/CHANGES.txt 2008-10-13 18:24:07 UTC (rev 92143)
+++ hurry.resource/trunk/CHANGES.txt 2008-10-13 18:24:37 UTC (rev 92144)
@@ -4,7 +4,12 @@
0.2 (unreleased)
================
-* ...
+* Changed the API to set the mode. Instead of passing it to ``render``
+ and ``inclusions``, the requested mode can be set with the ``mode``
+ method on ``INeededInclusions``. For convenience there is a ``mode``
+ function as well that can be imported directly from
+ ``hurry.resource`` that sets the mode for the current needed
+ inclusions.
0.1 (2008-10-07)
================
Modified: hurry.resource/trunk/src/hurry/resource/README.txt
===================================================================
--- hurry.resource/trunk/src/hurry/resource/README.txt 2008-10-13 18:24:07 UTC (rev 92143)
+++ hurry.resource/trunk/src/hurry/resource/README.txt 2008-10-13 18:24:37 UTC (rev 92144)
@@ -305,7 +305,8 @@
We can however also get the resource for mode ``debug`` and get
``k-debug.js``::
- >>> needed.inclusions(mode='debug')
+ >>> needed.mode('debug')
+ >>> needed.inclusions()
[<ResourceInclusion 'k-debug.js' in library 'foo'>]
Modes can also be specified fully with a resource inclusion, which allows
@@ -324,12 +325,45 @@
We can however also get the resource for mode ``debug`` and get
``a2-debug.js``::
- >>> needed.inclusions(mode='debug')
+ >>> needed.mode('debug')
+ >>> needed.inclusions()
[<ResourceInclusion 'k2-debug.js' in library 'foo'>]
Note that modes are assumed to be identical in dependency structure;
they functionally should do the same.
+Mode convenience
+================
+
+Like for ``need``, there is also a convenience spelling for
+``mode``. It uses ``ICurrentNeededInclusions``, which we've already
+set up to look at the ``request.needed`` variable. Let's set up
+a new request::
+
+ >>> request = Request()
+
+Let's set up a resource and need it::
+
+ >>> l1 = ResourceInclusion(foo, 'l1.js', debug='l1-debug.js')
+ >>> l1.need()
+
+Let's look at the resources needed by default::
+
+ >>> c = component.getUtility(ICurrentNeededInclusions)
+ >>> c().inclusions()
+ [<ResourceInclusion 'l1.js' in library 'foo'>]
+
+Let's now change the mode using the convenience
+``hurry.resource.mode`` spelling::
+
+ >>> from hurry.resource import mode
+ >>> mode('debug')
+
+When we request the resources now, we get them in the ``debug`` mode::
+
+ >>> c().inclusions()
+ [<ResourceInclusion 'l1-debug.js' in library 'foo'>]
+
"Rollups"
=========
@@ -486,7 +520,8 @@
>>> needed.need(f2)
>>> needed.inclusions()
[<ResourceInclusion 'giantf.js' in library 'foo'>]
- >>> needed.inclusions(mode='debug')
+ >>> needed.mode('debug')
+ >>> needed.inclusions()
[<ResourceInclusion 'giantf-debug.js' in library 'foo'>]
What if the rolled up resources have no mode but the superseding resource
@@ -502,7 +537,8 @@
>>> needed.need(g2)
>>> needed.inclusions()
[<ResourceInclusion 'giantg.js' in library 'foo'>]
- >>> needed.inclusions(mode='debug')
+ >>> needed.mode('debug')
+ >>> needed.inclusions()
[<ResourceInclusion 'giantg.js' in library 'foo'>]
What if the rolled up resources have a mode but the superseding resource
@@ -520,7 +556,8 @@
Since there is no superseder for the debug mode, we will get the two
resources, not rolled up::
- >>> needed.inclusions(mode='debug')
+ >>> needed.mode('debug')
+ >>> needed.inclusions()
[<ResourceInclusion 'h1-debug.js' in library 'foo'>,
<ResourceInclusion 'h2-debug.js' in library 'foo'>]
Modified: hurry.resource/trunk/src/hurry/resource/__init__.py
===================================================================
--- hurry.resource/trunk/src/hurry/resource/__init__.py 2008-10-13 18:24:07 UTC (rev 92143)
+++ hurry.resource/trunk/src/hurry/resource/__init__.py 2008-10-13 18:24:37 UTC (rev 92144)
@@ -1,4 +1,5 @@
from hurry.resource.core import (Library, ResourceInclusion, NeededInclusions,
+ mode,
sort_inclusions_topological,
sort_inclusions_by_extension,
generate_code)
Modified: hurry.resource/trunk/src/hurry/resource/core.py
===================================================================
--- hurry.resource/trunk/src/hurry/resource/core.py 2008-10-13 18:24:07 UTC (rev 92143)
+++ hurry.resource/trunk/src/hurry/resource/core.py 2008-10-13 18:24:37 UTC (rev 92144)
@@ -127,19 +127,23 @@
class NeededInclusions(object):
def __init__(self):
self._inclusions = []
-
+ self._mode = None
+
def need(self, inclusion):
self._inclusions.append(inclusion)
+ def mode(self, mode):
+ self._mode = mode
+
def _sorted_inclusions(self):
return reversed(sorted(self._inclusions, key=lambda i: i.depth()))
- def inclusions(self, mode=None):
+ def inclusions(self):
inclusions = []
for inclusion in self._inclusions:
inclusions.extend(inclusion.inclusions())
- inclusions = apply_mode(inclusions, mode)
+ inclusions = apply_mode(inclusions, self._mode)
inclusions = consolidate(inclusions)
# sort only by extension, not dependency, as we can rely on
# python's stable sort to keep inclusion order intact
@@ -147,10 +151,10 @@
inclusions = remove_duplicates(inclusions)
return inclusions
- def render(self, mode=None):
+ def render(self):
result = []
library_urls = {}
- for inclusion in self.inclusions(mode):
+ for inclusion in self.inclusions():
library = inclusion.library
# get cached library url
library_url = library_urls.get(library.name)
@@ -164,6 +168,13 @@
library_url + inclusion.relpath))
return '\n'.join(result)
+def mode(mode):
+ """Set the mode for the currently needed resources.
+ """
+ needed = component.getUtility(
+ interfaces.ICurrentNeededInclusions)()
+ needed.mode(mode)
+
def apply_mode(inclusions, mode):
return [inclusion.mode(mode) for inclusion in inclusions]
Modified: hurry.resource/trunk/src/hurry/resource/interfaces.py
===================================================================
--- hurry.resource/trunk/src/hurry/resource/interfaces.py 2008-10-13 18:24:07 UTC (rev 92143)
+++ hurry.resource/trunk/src/hurry/resource/interfaces.py 2008-10-13 18:24:37 UTC (rev 92144)
@@ -69,25 +69,37 @@
See also IInclusion.need() for a convenience method.
"""
- def inclusions(mode=None):
+ def mode(mode):
+ """Set the mode in which needed inclusions will be returned.
+
+ try to put inclusions returned by ``render`` and
+ ``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.
+
+ The default mode is None; it is suggested this is the
+ non-compressed/minified version of the Javascript/CSS to make
+ debugging easier.
+
+ Some suggested modes to use generally are 'debug' and 'minified'.
+ 'debug' is for full-source versions of the code so that it is
+ easy to debug, while 'minified' is
+
+ mode - a string indicating the mode, or None if no mode.
+
+ NOTE: there is also a ``hurry.resource.mode`` function which
+ can be used to set the mode for the currently needed inclusions.
+ """
+
+ def inclusions():
"""Give all resource inclusions needed.
- 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 list of resource inclusions needed.
"""
- def render(self, mode=None):
+ def render():
"""Render all resource inclusions for HTML header.
- 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 resource inclusions.
"""
More information about the Checkins
mailing list