[Checkins] SVN: z3c.resourcecollector/trunk/ use GET directly in getResources of the utility

Juergen Kartnaller juergen at kartnaller.at
Fri Feb 1 06:29:50 EST 2008


Log message for revision 83370:
  use GET directly in getResources of the utility
  some cleanup in code an tests
  

Changed:
  A   z3c.resourcecollector/trunk/CHANGES.txt
  U   z3c.resourcecollector/trunk/setup.py
  D   z3c.resourcecollector/trunk/src/z3c/resourcecollector/CHANGES.txt
  U   z3c.resourcecollector/trunk/src/z3c/resourcecollector/browser.py
  U   z3c.resourcecollector/trunk/src/z3c/resourcecollector/interfaces.py
  U   z3c.resourcecollector/trunk/src/z3c/resourcecollector/tests.py
  U   z3c.resourcecollector/trunk/src/z3c/resourcecollector/testview.py
  U   z3c.resourcecollector/trunk/src/z3c/resourcecollector/utility.py
  U   z3c.resourcecollector/trunk/src/z3c/resourcecollector/zcml.py
  U   z3c.resourcecollector/trunk/src/z3c/resourcecollector/zcml.txt

-=-
Copied: z3c.resourcecollector/trunk/CHANGES.txt (from rev 83126, z3c.resourcecollector/trunk/src/z3c/resourcecollector/CHANGES.txt)
===================================================================
--- z3c.resourcecollector/trunk/CHANGES.txt	                        (rev 0)
+++ z3c.resourcecollector/trunk/CHANGES.txt	2008-02-01 11:29:49 UTC (rev 83370)
@@ -0,0 +1,25 @@
+================================
+Changes in z3c.resourcecollector
+================================
+
+TODO: add tests for the viewlets !!!!
+
+
+2008/02/01 1.0.1
+----------------
+
+ - use GET directly in getResources of the utility
+ - some cleanup in code an tests
+
+1.0.0
+-----
+
+Initial release
+---------------
+
+Create zcml directives for creating a custom resource.
+It collects javascripts and css files and merges them into a single resource.
+It behaves exactly as a normal resource.
+Also provides 2 viewlets JSCollectorViewlet, and CSSCollectorViewlet.
+These viewlets are returning the html element what can reach the collector resource.
+Also adds a hash in the end of the resource for versionning.

Modified: z3c.resourcecollector/trunk/setup.py
===================================================================
--- z3c.resourcecollector/trunk/setup.py	2008-02-01 01:09:09 UTC (rev 83369)
+++ z3c.resourcecollector/trunk/setup.py	2008-02-01 11:29:49 UTC (rev 83370)
@@ -3,7 +3,7 @@
 from setuptools import setup, find_packages, Extension
 
 setup(name='z3c.resourcecollector',
-      version='1.0.0',
+      version='1.0.1',
       url='https://svn.lovelysystems.com/repos/dev/package/z3c.resourcecollector',
       license='ZPL',
       description='',

Deleted: z3c.resourcecollector/trunk/src/z3c/resourcecollector/CHANGES.txt
===================================================================
--- z3c.resourcecollector/trunk/src/z3c/resourcecollector/CHANGES.txt	2008-02-01 01:09:09 UTC (rev 83369)
+++ z3c.resourcecollector/trunk/src/z3c/resourcecollector/CHANGES.txt	2008-02-01 11:29:49 UTC (rev 83370)
@@ -1,11 +0,0 @@
-1.0.0
------
-
-Initial release
-
-Create zcml directives for creating a custom resource.
-It collects javascripts and css files and merges them into a single resource.
-It behaves exactly as a normal resource.
-Also provides 2 viewlets JSCollectorViewlet, and CSSCollectorViewlet.
-These viewlets are returning the html element what can reach the collector resource.
-Also adds a hash in the end of the resource for versionning.
\ No newline at end of file

Modified: z3c.resourcecollector/trunk/src/z3c/resourcecollector/browser.py
===================================================================
--- z3c.resourcecollector/trunk/src/z3c/resourcecollector/browser.py	2008-02-01 01:09:09 UTC (rev 83369)
+++ z3c.resourcecollector/trunk/src/z3c/resourcecollector/browser.py	2008-02-01 11:29:49 UTC (rev 83370)
@@ -22,17 +22,19 @@
 
 
 class CollectorResource(FileResource):
-        
+
     def __init__(self, request):
         self.request = request
-        
+
     def GET(self):
         rs = zope.component.getUtility(ICollectorUtility, self.__name__)
 
         resources = rs.getResources(self.request)
-        self.request.response.setHeader('Content-Type', rs.content_type)
+        if rs.content_type is not None:
+            self.request.response.setHeader('Content-Type', rs.content_type)
         secs = 31536000
-        self.request.response.setHeader('Cache-Control', 'public,max-age=%s' % secs)
+        self.request.response.setHeader('Cache-Control',
+                                        'public,max-age=%s' % secs)
         t = time.time() + secs
         self.request.response.setHeader('Expires',
                        time.strftime("%a, %d %b %Y %H:%M:%S GMT",
@@ -40,26 +42,36 @@
         return resources
 
 
-class JSCollectorViewlet(viewlet.ViewletBase):
-    template = """<script src="%s?hash=%s" 
-                    type="text/javascript">
-               </script>"""
-    
+class CollectorViewlet(viewlet.ViewletBase):
+
     @property
     def collector(self):
         return self.__name__
-               
+
     def render(self):
         originalHeader = self.request.response.getHeader('Content-Type')
         if originalHeader is None:
             originalHeader = "text/html"
         rs = zope.component.getUtility(ICollectorUtility, self.collector)
         versionedresource = rs.getUrl(self.context,self.request)
-        view=zope.component.getAdapter(self.request,name=self.collector)
+        view=zope.component.getAdapter(self.request, name=self.collector)
         url = view()
-        script = self.template  %(url, versionedresource)
-        self.request.response.setHeader('Content-Type',originalHeader)
+        script = self.template% {'url':url, 'hash':versionedresource}
+        self.request.response.setHeader('Content-Type', originalHeader)
         return script
-        
-class CSSCollectorViewlet(JSCollectorViewlet):
-    template = """<link rel="stylesheet" type="text/css" href="%s?hash=%s" />"""
+
+
+class JSCollectorViewlet(CollectorViewlet):
+    """Render a link to include Javascript resources"""
+
+    template = """<script src="%(url)s?hash=%(hash)s"
+                    type="text/javascript">
+                  </script>"""
+
+
+class CSSCollectorViewlet(CollectorViewlet):
+    """Render a link to include CSS resources"""
+
+    template = """<link rel="stylesheet" type="text/css"
+                        href="%(url)s?hash=%(hash)s" />"""
+

Modified: z3c.resourcecollector/trunk/src/z3c/resourcecollector/interfaces.py
===================================================================
--- z3c.resourcecollector/trunk/src/z3c/resourcecollector/interfaces.py	2008-02-01 01:09:09 UTC (rev 83369)
+++ z3c.resourcecollector/trunk/src/z3c/resourcecollector/interfaces.py	2008-02-01 11:29:49 UTC (rev 83370)
@@ -11,7 +11,9 @@
 # FOR A PARTICULAR PURPOSE.
 #
 ##############################################################################
+
 from zope import interface
 
 class ICollectorUtility(interface.Interface):
-    """interface class"""    
+    """interface class"""
+

Modified: z3c.resourcecollector/trunk/src/z3c/resourcecollector/tests.py
===================================================================
--- z3c.resourcecollector/trunk/src/z3c/resourcecollector/tests.py	2008-02-01 01:09:09 UTC (rev 83369)
+++ z3c.resourcecollector/trunk/src/z3c/resourcecollector/tests.py	2008-02-01 11:29:49 UTC (rev 83370)
@@ -1,20 +1,32 @@
+##############################################################################
+#
+# Copyright (c) 2002 Zope Corporation and Contributors.
+# All Rights Reserved.
+#
+# This software is subject to the provisions of the Zope Public License,
+# Version 2.1 (ZPL).  A copy of the ZPL should accompany this distribution.
+# THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
+# WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+# WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
+# FOR A PARTICULAR PURPOSE.
+#
+##############################################################################
 __docformat__ = "reStructuredText"
 
+import os
 import doctest
 import unittest
+
 from zope.testing import doctest
 
 from zope.testing.doctestunit import DocFileSuite, DocTestSuite
-import os
 
-here = os.path.dirname(__file__)
 
 def test_suite():
     return unittest.TestSuite(
-        (
-        DocFileSuite('zcml.txt',
-                     optionflags=doctest.NORMALIZE_WHITESPACE|doctest.ELLIPSIS,
-                     ),
+        (DocFileSuite('zcml.txt',
+                  optionflags=doctest.NORMALIZE_WHITESPACE|doctest.ELLIPSIS,
+                  ),
         ))
 
 if __name__ == '__main__':

Modified: z3c.resourcecollector/trunk/src/z3c/resourcecollector/testview.py
===================================================================
--- z3c.resourcecollector/trunk/src/z3c/resourcecollector/testview.py	2008-02-01 01:09:09 UTC (rev 83369)
+++ z3c.resourcecollector/trunk/src/z3c/resourcecollector/testview.py	2008-02-01 11:29:49 UTC (rev 83370)
@@ -1,4 +1,18 @@
+##############################################################################
+#
+# Copyright (c) 2002 Zope Corporation and Contributors.
+# All Rights Reserved.
+#
+# This software is subject to the provisions of the Zope Public License,
+# Version 2.1 (ZPL).  A copy of the ZPL should accompany this distribution.
+# THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
+# WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+# WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
+# FOR A PARTICULAR PURPOSE.
+#
+##############################################################################
 
+
 class res1(object):
     def __call__(self):
         return "test resource #1"
@@ -9,4 +23,5 @@
 
 class res3(object):
     def __call__(self):
-        return "test resource #3"
\ No newline at end of file
+        return "test resource #3"
+

Modified: z3c.resourcecollector/trunk/src/z3c/resourcecollector/utility.py
===================================================================
--- z3c.resourcecollector/trunk/src/z3c/resourcecollector/utility.py	2008-02-01 01:09:09 UTC (rev 83369)
+++ z3c.resourcecollector/trunk/src/z3c/resourcecollector/utility.py	2008-02-01 11:29:49 UTC (rev 83370)
@@ -13,15 +13,12 @@
 ##############################################################################
 
 import sha
-import zope.component
 
 from zope import interface
+from zope import component
 
 from interfaces import ICollectorUtility
 
-class Content(object):
-    interface.implements(interface.Interface)
-    pass
 
 class CollectorUtility(object):
     """utility"""
@@ -35,16 +32,15 @@
         filetoreturn = self.getResources(request)
         x = sha.new()
         x.update(filetoreturn)
-        return x.hexdigest()        
-        
+        return x.hexdigest()
+
     def getResources(self, request):
         filetoreturn = ""
         reducedrs = self.resources.values()
         orderedrs = sorted(reducedrs, cmp=lambda a,b: cmp (a['weight'],b['weight']))
         for resource in orderedrs:
-            res = zope.component.getAdapter(request,name=resource['resource'])
+            res = component.getAdapter(request,name=resource['resource'])
             res.__name__ = resource['resource']
-            filetoreturn += res.browserDefault(request)[0]() + "\n"
+            filetoreturn += res.GET() + "\n"
         return filetoreturn
-        
 

Modified: z3c.resourcecollector/trunk/src/z3c/resourcecollector/zcml.py
===================================================================
--- z3c.resourcecollector/trunk/src/z3c/resourcecollector/zcml.py	2008-02-01 01:09:09 UTC (rev 83369)
+++ z3c.resourcecollector/trunk/src/z3c/resourcecollector/zcml.py	2008-02-01 11:29:49 UTC (rev 83370)
@@ -45,9 +45,9 @@
         required=False
         )
 
-  
+
 def handleCollector(_context, name, type, content_type = None):
-    
+
     zapi.getGlobalSiteManager().registerUtility(CollectorUtility(content_type),name=name)
     class_=CollectorResource
     for_ = (zope.interface.Interface,)
@@ -70,7 +70,7 @@
         The resource""",
         required=True,
         )
-        
+
     weight = zope.schema.Int(
         title=u"The position of the resource in the library",
         description=u"""\

Modified: z3c.resourcecollector/trunk/src/z3c/resourcecollector/zcml.txt
===================================================================
--- z3c.resourcecollector/trunk/src/z3c/resourcecollector/zcml.txt	2008-02-01 01:09:09 UTC (rev 83369)
+++ z3c.resourcecollector/trunk/src/z3c/resourcecollector/zcml.txt	2008-02-01 11:29:49 UTC (rev 83370)
@@ -1,25 +1,18 @@
-===================================
-``z3c.resourcecollector`` Directive
-===================================
+=================================
+``z3c.resourcollector`` Directive
+=================================
 
 This package provides a new directive to use the special resource
 directive. You can merge with it more .js or .css files into one.
 Beware, you have to check before if your files are compatible.
 There are 2 zcml directives: collector and collectorItem.
 Also 2 viewlets are defined what returns the html code for inserting
-the js resource or the css resource: JSCollectorViewlet, CSSCollectorViewlet. 
-For versioning the urls for the resources also contains a hash code 
+the js resource or the css resource: JSCollectorViewlet, CSSCollectorViewlet.
+For versioning the urls for the resources also contains a hash code
 calculated from the files. This helps at caching the resources.
 
 First we need to define a fake absolute URL for testing:
 
-  >>> from zope.configuration import xmlconfig
-  >>> context = xmlconfig.string('''
-  ... <configure i18n_domain="zope">
-  ...   <include package="z3c.resourcecollector" file="meta.zcml" />
-  ... </configure>
-  ... ''')
-  
   >>> from zope.traversing.browser.interfaces import IAbsoluteURL
   >>> from zope import interface, component
   >>> from zope.publisher.interfaces.browser import IBrowserRequest
@@ -32,10 +25,22 @@
   ...        return 'homeofsite'
   >>> component.provideAdapter(FakeAbsoluteURL)
 
-Now in the zcml we can define a collector. We will add our files to this 
-collector.
+The meta.zcml registers the new directives in the browser namespace.
 
+  >>> from zope.configuration import xmlconfig
   >>> context = xmlconfig.string('''
+  ... <configure i18n_domain="zope">
+  ...   <include package="z3c.resourcecollector" file="meta.zcml" />
+  ... </configure>
+  ... ''')
+
+
+Resource Collector registration
+-------------------------------
+
+The "collector" directive creates a new collector utility.
+
+  >>> context = xmlconfig.string('''
   ... <configure xmlns="http://namespaces.zope.org/browser">
   ...   <include package="z3c.resourcecollector" file="meta.zcml" />
   ...   <collector
@@ -44,52 +49,53 @@
   ... />
   ... </configure>''')
 
-Using getUtility we check if the collectorUtility was created:
+Now we have a named utility.
 
-  >>> import zope.component
   >>> from z3c.resourcecollector.interfaces import ICollectorUtility
-  >>> rs = zope.component.getUtility(ICollectorUtility, "test.js")
-  >>> rs 
+  >>> rs = component.getUtility(ICollectorUtility, "test.js")
+  >>> rs
   <z3c.resourcecollector.utility.CollectorUtility ...>
-  
-With getAdapter, we check if the collector was registered.
 
-  >>> from zope import interface
-  >>> class Content(object):
-  ...     interface.implements(interface.Interface)
-  ...     pass
-  >>> content = Content()
+There is also a new resource available which collectes the resources. Resource
+are simple named adapters on the request.
+
   >>> from zope.publisher.browser import TestRequest
   >>> request = TestRequest()
-  >>> jsview = zope.component.getAdapter(request,name="test.js")
-  >>> jsview.__name__ = 'test.js'
-  >>> jsview
+  >>> jsResource = component.getAdapter(request, name="test.js")
+  >>> jsResource.__name__ = 'test.js'
+  >>> jsResource
   <z3c.resourcecollector.browser.CollectorResource object at ...>
-  
-For testing we define a fake resource type, then we create 3 fake resources.
 
+
+Adding resources to a collector
+-------------------------------
+
+The collector is now ready to receive resources which it should the collect
+together into one resource.
+
   >>> from zope.publisher.interfaces.browser import IBrowserRequest
   >>> class FakeResource(object):
   ...     interface.implements(interface.Interface)
-  ...     def browserDefault(self, request):
-  ...         return getattr(self, '__call__'), ()
   ...     def __init__(self, request):
   ...         pass
+  ...     def browserDefault(self, request):
+  ...         return getattr(self, 'GET'), ()
+  ...     def GET(self):
+  ...         return 'I am resource "%s"'% self.__name__
   ...     def __call__(self):
-  ...         return self.__name__
-  >>> zope.component.provideAdapter(FakeResource,(IBrowserRequest,),name="res_test1.js")
-  >>> zope.component.provideAdapter(FakeResource,(IBrowserRequest,),name="res_test2.js")
-  >>> zope.component.provideAdapter(FakeResource,(IBrowserRequest,),name="res_test3.js")
-  
-  >>> firstresource = zope.component.getAdapter(request,name="res_test1.js")
+  ...         return '<url to="%s"/>'% self.__name__
+
+  >>> component.provideAdapter(FakeResource, (IBrowserRequest,), name="res_test1.js")
+  >>> component.provideAdapter(FakeResource, (IBrowserRequest,), name="res_test2.js")
+  >>> component.provideAdapter(FakeResource, (IBrowserRequest,), name="res_test3.js")
+
+  >>> firstresource = component.getAdapter(request, name="res_test1.js")
   >>> firstresource.__name__ = "res_test1.js"
-  >>> secondresource = zope.component.getAdapter(request,name="res_test2.js")
+  >>> secondresource = component.getAdapter(request, name="res_test2.js")
   >>> secondresource.__name__ = "res_test2.js"
-  >>> thirdresource = zope.component.getAdapter(request,name="res_test1.js")
+  >>> thirdresource = component.getAdapter(request, name="res_test1.js")
   >>> thirdresource.__name__ = "res_test3.js"
-  >>> firstresource()
-  'res_test1.js'
- 
+
 Using the collectorItem directive we add 2 resources to a collector
 
   >>> context = xmlconfig.string('''
@@ -106,17 +112,20 @@
   ...    weight="2"
   ...   />
   ... </configure>''')
-  
-With getAdapter we find our collector, and check if it returns the merged resources
 
-  >>> resourcecollector = zope.component.getAdapter(request,name="test.js")
-  >>> resourcecollector.__name__ = "test.js"
-  >>> resourcecollector.GET()
-  u'res_test1.js\nres_test2.js\n'
-  
-Check if I can add my resourcecollector to another collector.
-First create a new collector:
-  
+Now we can use our collector resource to get the result.
+
+  >>> print jsResource.GET()
+  I am resource "res_test1.js"
+  I am resource "res_test2.js"
+
+
+Cascading Collectors
+--------------------
+
+Because a resource collector is itself also a resource we can use this fact to
+include a collector as a collectorItem into another collector.
+
   >>> context = xmlconfig.string('''
   ... <configure xmlns="http://namespaces.zope.org/browser">
   ...   <include package="z3c.resourcecollector" file="meta.zcml" />
@@ -142,11 +151,13 @@
   ...    weight="1"
   ...   />
   ... </configure>''')
-  
-With getAdapter we find our new resource collector and check if it contains all
-the resources what we added to it.
 
-  >>> resourcecollector2 = zope.component.getAdapter(request,name="bigtest.js")
-  >>> resourcecollector2.__name__ = "bigtest.js"
-  >>> resourcecollector2.GET()
-  u'res_test3.js\nres_test1.js\nres_test2.js\n\n'
\ No newline at end of file
+We use the new resource.
+
+  >>> jsResource2 = component.getAdapter(request, name="bigtest.js")
+  >>> jsResource2.__name__ = "bigtest.js"
+  >>> print jsResource2.GET()
+  I am resource "res_test3.js"
+  I am resource "res_test1.js"
+  I am resource "res_test2.js"
+



More information about the Checkins mailing list