Logo Search packages:      
Sourcecode: zope-kupu version File versions

librarytool.py

##############################################################################
#
# Copyright (c) 2003-2004 Kupu Contributors. All rights reserved.
#
# This software is distributed under the terms of the Kupu
# License. See LICENSE.txt for license text. For a list of Kupu
# Contributors see CREDITS.txt.
#
##############################################################################
"""Kupu library tool

This module contains Kupu's library tool to support drawers.

$Id: librarytool.py 6772 2004-09-28 11:55:11Z guido $
"""
from Acquisition import aq_parent, aq_inner, aq_base
from Products.CMFCore.Expression import Expression
from Products.CMFCore.Expression import createExprContext
from Products.kupu.plone.interfaces import IKupuLibraryTool

00021 class KupuLibraryTool:
    """A tool to aid Kupu libraries"""

    __implements__ = IKupuLibraryTool

    def __init__(self):
        self._libraries = []
        self._res_types = {}

    def _getExpressionContext(self, object):
        portal = aq_parent(aq_inner(self))
        if object is None or not hasattr(object, 'aq_base'):
            folder = portal
        else:
            folder = object
            # Search up the containment hierarchy until we find an
            # object that claims it's a folder.
            while folder is not None:
                if getattr(aq_base(folder), 'isPrincipiaFolderish', 0):
                    # found it.
                    break
                else:
                    folder = aq_parent(aq_inner(folder))
        ec = createExprContext(folder, portal, object)
        return ec

00047     def addLibrary(self, id, title, uri, src, icon):
        """See ILibraryManager"""
        lib = dict(id=id, title=title, uri=uri, src=src, icon=icon)
        for key, value in lib.items():
            if not(value.startswith('string:') or value.startswith('python:')):
                value = 'string:' + value
            lib[key] = Expression(value)
        self._libraries.append(lib)

00056     def getLibraries(self, context):
        """See ILibraryManager"""
        expr_context = self._getExpressionContext(context)
        libraries = []
        for library in self._libraries:
            lib = {}
            for key in library.keys():
                lib[key] = library[key](expr_context)
            libraries.append(lib)
        return tuple(libraries)

00067     def deleteLibraries(self, indices):
        """See ILibraryManager"""
        indices.sort()
        indices.reverse()
        for index in indices:
            del self._libraries[index]

00074     def updateLibraries(self, libraries):
        """See ILibraryManager"""
        for index, lib in enumerate(self._libraries):
            dic = libraries[index]
            for key in lib.keys():
                if dic.has_key(key):
                    value = dic[key]
                    if not(value.startswith('string:') or
                           value.startswith('python:')):
                        value = 'string:' + value
                    lib[key] = Expression(value)
            self._libraries[index] = lib

00087     def moveUp(self, indices):
        """See ILibraryManager"""
        indices.sort()
        libraries = self._libraries[:]
        for index in indices:
            new_index = index - 1
            libraries[index], libraries[new_index] = \
                              libraries[new_index], libraries[index]
        self._libraries = libraries

00097     def moveDown(self, indices):
        """See ILibraryManager"""
        indices.sort()
        indices.reverse()
        libraries = self._libraries[:]
        for index in indices:
            new_index = index + 1
            if new_index >= len(libraries):
                new_index = 0
                #new_index = ((index + 1) % len(libraries)) - 1
            libraries[index], libraries[new_index] = \
                              libraries[new_index], libraries[index]
        self._libraries = libraries

00111     def getPortalTypesForResourceType(self, resource_type):
        """See IResourceTypeMapper"""
        return self._res_types[resource_type][:]

00115     def queryPortalTypesForResourceType(self, resource_type, default=None):
        """See IResourceTypeMapper"""
        if not self._res_types.has_key(resource_type):
            return default
        return self._res_types[resource_type][:]

00121     def addResourceType(self, resource_type, portal_types):
        """See IResourceTypeMapper"""
        portal_types = [ptype.strip() for ptype in portal_types if ptype]
        self._res_types[resource_type] = tuple(portal_types)

00126     def updateResourceTypes(self, type_info):
        """See IResourceTypeMapper"""
        type_map = self._res_types
        for type in type_info:
            del type_map[type['old_type']]
            portal_types = [ptype.strip() for ptype
                            in type['portal_types'] if ptype]
            type_map[type['resource_type']] = tuple(portal_types)

00135     def deleteResourceTypes(self, resource_types):
        """See IResourceTypeMapper"""
        for type in resource_types:
            del self._res_types[type]

Generated by  Doxygen 1.6.0   Back to index