[Checkins] SVN: hurry.resource/branches/janjaapdriessen-resource-publisher/src/hurry/resource/ move generating a library URL to the library class
Jan-Wijbrand Kolman
janwijbrand at gmail.com
Mon Nov 22 12:39:26 EST 2010
Log message for revision 118521:
move generating a library URL to the library class
Changed:
U hurry.resource/branches/janjaapdriessen-resource-publisher/src/hurry/resource/README.txt
U hurry.resource/branches/janjaapdriessen-resource-publisher/src/hurry/resource/__init__.py
U hurry.resource/branches/janjaapdriessen-resource-publisher/src/hurry/resource/core.py
U hurry.resource/branches/janjaapdriessen-resource-publisher/src/hurry/resource/publisher.py
U hurry.resource/branches/janjaapdriessen-resource-publisher/src/hurry/resource/wsgi.py
-=-
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-22 17:17:50 UTC (rev 118520)
+++ hurry.resource/branches/janjaapdriessen-resource-publisher/src/hurry/resource/README.txt 2010-11-22 17:39:25 UTC (rev 118521)
@@ -733,7 +733,7 @@
>>> print needed.render()
Traceback (most recent call last):
...
- TypeError: unsupported operand type(s) for +: 'NoneType' and 'str'
+ AttributeError: 'NoneType' object has no attribute 'endswith'
That didn't work. In order to render an inclusion, we need to tell
``hurry.resource`` the base URL for a resource inclusion. We
@@ -788,40 +788,30 @@
hash of the resource's contents, so it will look like
/foo/:hash:12345/myresource instead of /foo/myresource.
-The default hashing behavior can be changed by setting the 'hashing' variable
-in the hurry.resource module through the `configure_hashing` function::
-
- >>> import hurry.resource
- >>> hurry.resource.configure_hashing(False)
>>> print resource.render()
- <link rel="stylesheet" type="text/css" href="http://localhost/static/foo/b.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>
-
- >>> hurry.resource.configure_hashing(True)
- >>> print resource.render()
<link rel="stylesheet" type="text/css" href="http://localhost/static/:hash:.../foo/b.css" />
<script type="text/javascript" src="http://localhost/static/:hash:.../foo/a.js"></script>
<script type="text/javascript" src="http://localhost/static/:hash:.../foo/c.js"></script>
More about the devmode in a minute::
+ >>> import hurry.resource
>>> hurry.resource.configure_devmode(True)
The hash of a library is computed based on the contents of the directory.
If we alter the contents of the directory, the hash is updated.
- >>> before_hash = foo.hash()
+ >>> before_hash = foo.signature()
>>> from pkg_resources import resource_filename, resource_string
>>> before = resource_string('mypackage', 'resources/style.css')
>>> mypackage_style = resource_filename('mypackage', 'resources/style.css')
>>> open(mypackage_style, 'w').write('body {color: #0f0;}')
- >>> foo.hash() == before_hash
+ >>> foo.signature() == before_hash
False
>>> # Reset the content.
>>> open(mypackage_style, 'w').write(before)
- >>> foo.hash() == before_hash
+ >>> foo.signature() == before_hash
True
Any VCS directories are ignored in calculating the hash:
@@ -833,7 +823,7 @@
... os.path.join('resources', 'sub', '.svn')))
>>> open(os.path.join(resource_filename('mypackage', 'resources/sub/.svn'), 'test'),
... 'w').write('test')
- >>> foo.hash() == before_hash
+ >>> foo.signature() == before_hash
True
In developer mode the hash is recomputed each time the resource is asked for
@@ -846,23 +836,23 @@
function `hurry.resource.configure_devmode`.
>>> hurry.resource.configure_devmode(True)
- >>> before_hash = foo.hash()
+ >>> before_hash = foo.signature()
>>> foo_sub_dir = resource_filename('mypackage', 'resources/sub')
>>> open(os.path.join(foo_sub_dir, 'test'), 'w').write('test')
>>> # The hash is newly computed.
- >>> foo.hash() is not before_hash
+ >>> foo.signature() is not before_hash
True
When we are not in devmode, the hash is not computed again:
>>> hurry.resource.configure_devmode(False)
- >>> before_hash = foo.hash()
+ >>> before_hash = foo.signature()
>>> open(os.path.join(foo_sub_dir, 'test2'), 'w').write('test2')
>>> # The hash is not newly computed.
- >>> foo.hash() is before_hash
+ >>> foo.signature() is before_hash
True
>>> hurry.resource.configure_devmode(True)
- >>> foo.hash() is before_hash
+ >>> foo.signature() is before_hash
False
Modified: hurry.resource/branches/janjaapdriessen-resource-publisher/src/hurry/resource/__init__.py
===================================================================
--- hurry.resource/branches/janjaapdriessen-resource-publisher/src/hurry/resource/__init__.py 2010-11-22 17:17:50 UTC (rev 118520)
+++ hurry.resource/branches/janjaapdriessen-resource-publisher/src/hurry/resource/__init__.py 2010-11-22 17:39:25 UTC (rev 118521)
@@ -15,12 +15,6 @@
from hurry.resource.core import (register_plugin,
get_current_needed_inclusions)
-hashing = True
-
-def configure_hashing(enable=True):
- global hashing
- hashing = enable
-
publisher_signature = ':hash:'
devmode = False
Modified: hurry.resource/branches/janjaapdriessen-resource-publisher/src/hurry/resource/core.py
===================================================================
--- hurry.resource/branches/janjaapdriessen-resource-publisher/src/hurry/resource/core.py 2010-11-22 17:17:50 UTC (rev 118520)
+++ hurry.resource/branches/janjaapdriessen-resource-publisher/src/hurry/resource/core.py 2010-11-22 17:39:25 UTC (rev 118521)
@@ -20,20 +20,25 @@
class Library(object):
implements(interfaces.ILibrary)
- _hash = None
+ _signature = None
def __init__(self, name, rootpath):
self.name = name
self.rootpath = rootpath
self.path = os.path.join(caller_dir(), rootpath)
- def hash(self):
+ def signature(self):
# Only compute the checksum if (1) it has not been computed
# before or (2) we are in development mode.
- if self._hash is None or hurry.resource.devmode:
- self._hash = hurry.resource.hash.checksum(self.path)
- return self._hash
+ if self._signature is None or hurry.resource.devmode:
+ self._signature = hurry.resource.hash.checksum(self.path)
+ return self._signature
+ def url(self, base_url):
+ return '%s%s%s/%s/' % (
+ base_url, hurry.resource.publisher_signature,
+ self.signature(), self.name)
+
# total hack to be able to get the dir the resources will be in
def caller_dir():
return os.path.dirname(sys._getframe(2).f_globals['__file__'])
@@ -431,25 +436,17 @@
base_url - the base url for resource inclusions.
"""
result = []
- hash_cache = {}
- if base_url and not base_url.endswith('/'):
+ if not base_url.endswith('/'):
base_url += '/'
+ url_cache = {} # prevent multiple computations for a library in one request
for inclusion in inclusions:
- signature = ''
library = inclusion.library
- if hurry.resource.hashing:
- # For every request, compute the hash of each library
- # only once. XXX This is a suboptimal optimization that
- # we would like to factor out.
- hash = hash_cache.get(library.name)
- if hash is None:
- hash = library.hash()
- hash_cache[library.name] = hash
- signature = '%s%s/' % (hurry.resource.publisher_signature, hash)
- library_url = base_url + signature + inclusion.library.name + '/'
- result.append(render_inclusion(
- inclusion, library_url + inclusion.relpath))
+ library_url = url_cache.get(library.name)
+ if library_url is None:
+ library_url = url_cache[library.name] = library.url(base_url)
+ result.append(
+ render_inclusion(inclusion, library_url + inclusion.relpath))
return '\n'.join(result)
def render_inclusion(inclusion, url):
Modified: hurry.resource/branches/janjaapdriessen-resource-publisher/src/hurry/resource/publisher.py
===================================================================
--- hurry.resource/branches/janjaapdriessen-resource-publisher/src/hurry/resource/publisher.py 2010-11-22 17:17:50 UTC (rev 118520)
+++ hurry.resource/branches/janjaapdriessen-resource-publisher/src/hurry/resource/publisher.py 2010-11-22 17:39:25 UTC (rev 118521)
@@ -18,6 +18,7 @@
class Publisher(object):
+
def __init__(self, app):
self._wrapped_app = app
self.directory_apps = {}
@@ -65,4 +66,4 @@
return directory_app(environ, cache_header_start_response)
def make_publisher(app, global_conf):
- return Publisher(app, **local_conf)
+ return Publisher(app)
Modified: hurry.resource/branches/janjaapdriessen-resource-publisher/src/hurry/resource/wsgi.py
===================================================================
--- hurry.resource/branches/janjaapdriessen-resource-publisher/src/hurry/resource/wsgi.py 2010-11-22 17:17:50 UTC (rev 118520)
+++ hurry.resource/branches/janjaapdriessen-resource-publisher/src/hurry/resource/wsgi.py 2010-11-22 17:39:25 UTC (rev 118521)
@@ -1,9 +1,7 @@
import webob
NEEDED = 'hurry.resource.needed'
-PUBLISHER_PREFIX = 'hurry.resource.publisher_prefix'
-
# TODO: would be nice to make middleware smarter so it could work with
# a streamed HTML body instead of serializing it out to body. That
# would complicate the middleware signicantly, however. We would for
@@ -11,18 +9,12 @@
class InjectMiddleWare(object):
- def __init__(self, application, publisher_prefix=None):
+ def __init__(self, application):
self.application = application
- self.publisher_prefix = publisher_prefix
def __call__(self, environ, start_response):
request = webob.Request(environ)
- if self.publisher_prefix is not None:
- # Inform the wrapped application of the presence of a resource
- # publisher and resource URLs may thus be routed to this publisher.
- environ[PUBLISHER_PREFIX] = self.publisher_prefix
-
# Get the response from the wrapped application:
response = request.get_response(self.application)
More information about the checkins
mailing list