[Zope-CMF] Accessing skins objects

Tres Seaver tseaver at palladion.com
Mon Feb 9 10:10:11 EST 2009



-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Charlie Clark wrote:
> Hi,
> 
> I've written a very simple view for concactenating resources such as  
> CSS or Javascript files to reduce the number of http browser requests:
> 
> 
> from Products.CMFCore.utils import getToolByName
> from Products.CMFDefault.browser.utils import memoize, ViewBase
> 
> class Javascript(ViewBase):
>      """Return all Javascript from the skin as a single string"""
> 
>      folder_name = 'js'
>      content_type = 'application/x-javascript'
> 
>      @memoize
>      def contents(self):
>          skin_tool = getToolByName(self.context, 'portal_skins')
>          layer = skin_tool.getDefaultSkin()
>          skin = skin_tool.getSkinByName(layer)
>          folder = getattr(skin, self.folder_name)
>          obs = [str(ob) for ob in folder.objectValues()
>                         if ob.meta_type == "Filesystem File"]
>          return "\n".join(obs)
> 
>      def __call__(self):
>          data = self.contents()
>          self.request.response.setHeader("Content-Type",  
> self.content_type)
>          self.request.response.setHeader("Content-Length", len(data))
>          self.request.response.write(data)
> 
> While this works fine, it seems to run considerably slower (about half  
> the speed) than a PythonScript that calls the relevant objects  
> explicitly via getattr(context, id). I'm a bit surprised at this but  
> suspect it may down to whether the file system is actually accessed or  
> whether a persistent object is being called. In my browser view it is  
> impossible to avoid reading in the file again as this happens the  
> __str__ method of the FSFile object.
> 
> In the hope of enlightenment.

Try profiling the two requests and see what looks different.  The FSFile
object *never* stores the bits read from the filesystem, so the more
likely case is that your view is hitting some other bottleneck.

BTW, it might be faster to compute the *filenames* in the memo, and then
return an IFilestreamIterator which chunked the response by reading the
files one at a time.  Running 'cat /path/to/fsidr/*.js' in an
os.system() call might be faster, too ;)


Tres.
- --
===================================================================
Tres Seaver          +1 540-429-0999          tseaver at palladion.com
Palladion Software   "Excellence by Design"    http://palladion.com
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.6 (GNU/Linux)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org

iD8DBQFJkEdT+gerLs4ltQ4RApmBAJ4teYF55sK0pKu6uarUXBelhg6dXwCfcT5P
GVaGJKF4i1KD+JJTWkm1qOA=
=nq9A
-----END PGP SIGNATURE-----



More information about the Zope-CMF mailing list