# Copyright (c) 2002-2007 Infrae. All rights reserved.
# See also LICENSE.txt
# Zope 3
from zope.app.traversing import adapters
# SilvaLayout
from Products.SilvaLayout import interfaces

class SkinnyTraverser(adapters.Traverser):
    
    def traverse(self, path, default=None, request=None):
        """Get to metadata of object, to determine what skin to
        use for rendering.
        """
        
        # XXX this is not optimal: we retrieve metadata for each
        # traversed object (that has the SkinnyTraverser adapter
        # registered) to determine the skin to use.
        # This means, the *effective* skin, is the skin set at the last
        # traversal step.

        
        from_request = request.get('skin_from_request')
        if from_request:
            skin = request.getPresentationSkin()
            request.set('resourcebase', self.context.aq_inner)
            return adapters.Traverser.traverse(self, path, default, request)
        try:
            metadata = interfaces.IMetadata(self.context)
        except TypeError, e:
            request.set('resourcebase', self.context.aq_inner)
        else:
            skin = metadata('silva-layout', 'skin')
            if skin:
                # Skin name can be an empty string in case
                # its value was set to be 'acquired'.
                if not skin == request.getPresentationSkin():
                    request.setPresentationSkin(skin)
                    # set the base url for resources 
                    request.set('resourcebase', self.context.aq_inner)
        object = adapters.Traverser.traverse(self, path, default, request)
        return object
    
