[Checkins] SVN: hurry.resource/branches/janjaapdriessen-resource-publisher/src/hurry/resource/core.py Add functionality for generating hashed urls

Jan-Jaap Driessen jdriessen at thehealthagency.com
Sun Nov 14 09:39:21 EST 2010


Log message for revision 118401:
  Add functionality for generating hashed urls

Changed:
  U   hurry.resource/branches/janjaapdriessen-resource-publisher/src/hurry/resource/core.py

-=-
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-14 14:38:43 UTC (rev 118400)
+++ hurry.resource/branches/janjaapdriessen-resource-publisher/src/hurry/resource/core.py	2010-11-14 14:39:21 UTC (rev 118401)
@@ -1,5 +1,4 @@
 import os, sys
-from types import TupleType
 import pkg_resources
 
 try:
@@ -12,6 +11,7 @@
     ZCA = False
 
 from hurry.resource import interfaces
+import hurry.resource.hash
 
 EXTENSIONS = ['.css', '.kss', '.js']
 
@@ -23,6 +23,9 @@
         self.rootpath = rootpath
         self.path = os.path.join(caller_dir(), rootpath)
 
+    def hash(self):
+        return hurry.resource.hash.checksum(self.path)
+
 # 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__'])
@@ -413,17 +416,26 @@
     '.js': render_js,
     }
 
-def render_inclusions(inclusions, base_url):
+def render_inclusions(inclusions, base_url, hash_in_url=True):
     """Render a set of inclusions.
 
     inclusions - the inclusions to render
     base_url - the base url for resource inclusions.
     """
     result = []
+    hash_cache = {}
+    if not base_url.endswith('/'):
+        base_url += '/'
     for inclusion in inclusions:
-        if not base_url.endswith('/'):
-            base_url += '/'
+        library = inclusion.library
         library_url = base_url + inclusion.library.name + '/'
+        if hash_in_url:
+             hash = hash_cache.get(library.name)
+             if hash is None:
+                 hash = library.hash()
+                 hash_cache[library.name] = hash
+             library_url += 'hash:%s/' % hash
+
         result.append(render_inclusion(inclusion,
                                        library_url + inclusion.relpath))
     return '\n'.join(result)



More information about the checkins mailing list