Annotation of kupuMPIWG/plone/librarytool.py, revision 1.1

1.1     ! dwinter     1: ##############################################################################
        !             2: #
        !             3: # Copyright (c) 2003-2005 Kupu Contributors. All rights reserved.
        !             4: #
        !             5: # This software is distributed under the terms of the Kupu
        !             6: # License. See LICENSE.txt for license text. For a list of Kupu
        !             7: # Contributors see CREDITS.txt.
        !             8: #
        !             9: ##############################################################################
        !            10: """Kupu library tool
        !            11: 
        !            12: This module contains Kupu's library tool to support drawers.
        !            13: 
        !            14: $Id: librarytool.py 14851 2005-07-21 11:39:15Z duncan $
        !            15: """
        !            16: import Acquisition
        !            17: from Acquisition import aq_parent, aq_inner, aq_base
        !            18: from Products.CMFCore.Expression import Expression
        !            19: from Products.CMFCore.Expression import createExprContext
        !            20: from Products.kupu.plone.interfaces import IKupuLibraryTool
        !            21: from Products.CMFCore.utils import getToolByName
        !            22: 
        !            23: class KupuError(Exception): pass
        !            24: 
        !            25: class KupuLibraryTool(Acquisition.Implicit):
        !            26:     """A tool to aid Kupu libraries"""
        !            27: 
        !            28:     __implements__ = IKupuLibraryTool
        !            29: 
        !            30:     def __init__(self):
        !            31:         self._libraries = []
        !            32:         self._res_types = {}
        !            33: 
        !            34:     def _getExpressionContext(self, object):
        !            35:         portal = aq_parent(aq_inner(self))
        !            36:         if object is None or not hasattr(object, 'aq_base'):
        !            37:             folder = portal
        !            38:         else:
        !            39:             folder = object
        !            40:             # Search up the containment hierarchy until we find an
        !            41:             # object that claims it's a folder.
        !            42:             while folder is not None:
        !            43:                 if getattr(aq_base(folder), 'isPrincipiaFolderish', 0):
        !            44:                     # found it.
        !            45:                     break
        !            46:                 else:
        !            47:                     folder = aq_parent(aq_inner(folder))
        !            48:         ec = createExprContext(folder, portal, object)
        !            49:         return ec
        !            50: 
        !            51:     def addLibrary(self, id, title, uri, src, icon):
        !            52:         """See ILibraryManager"""
        !            53:         lib = dict(id=id, title=title, uri=uri, src=src, icon=icon)
        !            54:         for key, value in lib.items():
        !            55:             if key=='id':
        !            56:                 lib[key] = value
        !            57:             else:
        !            58:                 if not(value.startswith('string:') or value.startswith('python:')):
        !            59:                     value = 'string:' + value
        !            60:                 lib[key] = Expression(value)
        !            61:         self._libraries.append(lib)
        !            62: 
        !            63:     def getLibraries(self, context):
        !            64:         """See ILibraryManager"""
        !            65:         expr_context = self._getExpressionContext(context)
        !            66:         libraries = []
        !            67:         for library in self._libraries:
        !            68:             lib = {}
        !            69:             for key in library.keys():
        !            70:                 if isinstance(library[key], str):
        !            71:                     lib[key] = library[key]
        !            72:                 else:
        !            73:                     # Automatic migration from old version.
        !            74:                     if key=='id':
        !            75:                         lib[key] = library[key] = library[key].text
        !            76:                     else:
        !            77:                         lib[key] = library[key](expr_context)
        !            78:             libraries.append(lib)
        !            79:         return tuple(libraries)
        !            80: 
        !            81:     def deleteLibraries(self, indices):
        !            82:         """See ILibraryManager"""
        !            83:         indices.sort()
        !            84:         indices.reverse()
        !            85:         for index in indices:
        !            86:             del self._libraries[index]
        !            87: 
        !            88:     def updateLibraries(self, libraries):
        !            89:         """See ILibraryManager"""
        !            90:         for index, lib in enumerate(self._libraries):
        !            91:             dic = libraries[index]
        !            92:             for key in lib.keys():
        !            93:                 if dic.has_key(key):
        !            94:                     value = dic[key]
        !            95:                     if key=='id':
        !            96:                         lib[key] = value
        !            97:                     else:
        !            98:                         if not(value.startswith('string:') or
        !            99:                                value.startswith('python:')):
        !           100:                             value = 'string:' + value
        !           101:                         lib[key] = Expression(value)
        !           102:             self._libraries[index] = lib
        !           103: 
        !           104:     def moveUp(self, indices):
        !           105:         """See ILibraryManager"""
        !           106:         indices.sort()
        !           107:         libraries = self._libraries[:]
        !           108:         for index in indices:
        !           109:             new_index = index - 1
        !           110:             libraries[index], libraries[new_index] = \
        !           111:                               libraries[new_index], libraries[index]
        !           112:         self._libraries = libraries
        !           113: 
        !           114:     def moveDown(self, indices):
        !           115:         """See ILibraryManager"""
        !           116:         indices.sort()
        !           117:         indices.reverse()
        !           118:         libraries = self._libraries[:]
        !           119:         for index in indices:
        !           120:             new_index = index + 1
        !           121:             if new_index >= len(libraries):
        !           122:                 new_index = 0
        !           123:                 #new_index = ((index + 1) % len(libraries)) - 1
        !           124:             libraries[index], libraries[new_index] = \
        !           125:                               libraries[new_index], libraries[index]
        !           126:         self._libraries = libraries
        !           127: 
        !           128:     def getPortalTypesForResourceType(self, resource_type):
        !           129:         """See IResourceTypeMapper"""
        !           130:         return self._res_types[resource_type][:]
        !           131: 
        !           132:     def queryPortalTypesForResourceType(self, resource_type, default=None):
        !           133:         """See IResourceTypeMapper"""
        !           134:         if not self._res_types.has_key(resource_type):
        !           135:             return default
        !           136:         return self._res_types[resource_type][:]
        !           137: 
        !           138:     def _validate_portal_types(self, resource_type, portal_types):
        !           139:         typetool = getToolByName(self, 'portal_types')
        !           140:         all_portal_types = dict([ (t.id, 1) for t in typetool.listTypeInfo()])
        !           141: 
        !           142:         portal_types = [ptype.strip() for ptype in portal_types if ptype]
        !           143:         for p in portal_types:
        !           144:             if p not in all_portal_types:
        !           145:                 raise KupuError, "Resource type: %s, invalid type: %s" % (resource_type, p)
        !           146:         return portal_types
        !           147: 
        !           148:     def addResourceType(self, resource_type, portal_types):
        !           149:         """See IResourceTypeMapper"""
        !           150:         portal_types = self._validate_portal_types(resource_type, portal_types)
        !           151:         self._res_types[resource_type] = tuple(portal_types)
        !           152: 
        !           153:     def updateResourceTypes(self, type_info):
        !           154:         """See IResourceTypeMapper"""
        !           155:         type_map = self._res_types
        !           156:         for type in type_info:
        !           157:             resource_type = type['resource_type']
        !           158:             portal_types = self._validate_portal_types(resource_type, type['portal_types'])
        !           159:             del type_map[type['old_type']]
        !           160:             type_map[resource_type] = tuple(portal_types)
        !           161: 
        !           162:     def deleteResourceTypes(self, resource_types):
        !           163:         """See IResourceTypeMapper"""
        !           164:         for type in resource_types:
        !           165:             del self._res_types[type]

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>