Back to index

plone3  3.1.7
exportimport.py
Go to the documentation of this file.
00001 ##############################################################################
00002 #
00003 # Copyright (c) 2005 Zope Corporation and Contributors. All Rights Reserved.
00004 #
00005 # This software is subject to the provisions of the Zope Public License,
00006 # Version 2.1 (ZPL).  A copy of the ZPL should accompany this distribution.
00007 # THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
00008 # WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
00009 # WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
00010 # FOR A PARTICULAR PURPOSE.
00011 #
00012 ##############################################################################
00013 """ActionIconsTool setup handlers.
00014 
00015 $Id: exportimport.py 77019 2007-06-24 19:01:14Z hannosch $
00016 """
00017 
00018 import os
00019 
00020 from AccessControl import ClassSecurityInfo
00021 from Globals import InitializeClass
00022 from Globals import package_home
00023 from Products.PageTemplates.PageTemplateFile import PageTemplateFile
00024 
00025 from zope.component import getSiteManager
00026 
00027 from Products.GenericSetup.utils import CONVERTER
00028 from Products.GenericSetup.utils import DEFAULT
00029 from Products.GenericSetup.utils import ExportConfiguratorBase
00030 from Products.GenericSetup.utils import ImportConfiguratorBase
00031 from Products.GenericSetup.utils import KEY
00032 
00033 from interfaces import IActionIconsTool
00034 from permissions import ManagePortal
00035 
00036 _pkgdir = package_home( globals() )
00037 _xmldir = os.path.join( _pkgdir, 'xml' )
00038 
00039 #
00040 #   Configurator entry points
00041 #
00042 _FILENAME = 'actionicons.xml'
00043 
00044 def importActionIconsTool(context):
00045     """ Import action icons tool settings from an XML file.
00046     """
00047     site = context.getSite()
00048     sm = getSiteManager(site)
00049     ait = sm.queryUtility(IActionIconsTool)
00050     if ait is None:
00051         return 'Action icons: No tool!'
00052 
00053     body = context.readDataFile(_FILENAME)
00054     if body is None:
00055         return 'Action icons: Nothing to import.'
00056 
00057     if context.shouldPurge():
00058         ait.clearActionIcons()
00059 
00060     # now act on the settings we've retrieved
00061     configurator = ActionIconsToolImportConfigurator(site)
00062     ait_info = configurator.parseXML(body)
00063 
00064     for action_icon in ait_info['action_icons']:
00065         category = action_icon['category']
00066         action_id = action_icon['action_id']
00067         # Ignore the i18n markup
00068         if action_icon.get('i18n:attributes', None) is not None:
00069             del action_icon['i18n:attributes']
00070         if ait.queryActionInfo(category, action_id) is not None:
00071             ait.updateActionIcon(**action_icon)
00072         else:
00073             ait.addActionIcon(**action_icon)
00074 
00075     return 'Action icons settings imported.'
00076 
00077 def exportActionIconsTool(context):
00078     """ Export caching policy manager settings as an XML file.
00079     """
00080     site = context.getSite()
00081     mhc = ActionIconsToolExportConfigurator( site ).__of__( site )
00082     text = mhc.generateXML()
00083 
00084     context.writeDataFile( _FILENAME, text, 'text/xml' )
00085 
00086     return 'Action icons tool settings exported.'
00087 
00088 
00089 class ActionIconsToolExportConfigurator(ExportConfiguratorBase):
00090     """ Synthesize XML description of cc properties.
00091     """
00092     security = ClassSecurityInfo()
00093 
00094     security.declareProtected( ManagePortal, 'listActionIconInfo' )
00095     def listActionIconInfo(self):
00096         """ Return a list of mappings describing the action icons.
00097         """
00098         sm = getSiteManager(self._site)
00099         ait = sm.getUtility(IActionIconsTool)
00100         for action_icon in ait.listActionIcons():
00101             yield {'category': action_icon.getCategory(),
00102                    'action_id': action_icon.getActionId(),
00103                    'title': action_icon.getTitle(),
00104                    'priority': action_icon.getPriority(),
00105                    'icon_expr': action_icon.getExpression(),
00106                   }
00107 
00108     security.declarePrivate('_getExportTemplate')
00109     def _getExportTemplate(self):
00110 
00111         return PageTemplateFile('aitExport.xml', _xmldir)
00112 
00113 InitializeClass(ActionIconsToolExportConfigurator)
00114 
00115 
00116 class ActionIconsToolImportConfigurator(ImportConfiguratorBase):
00117 
00118     def _getImportMapping(self):
00119         return {
00120           'action-icons':
00121              { 'action-icon': {KEY: 'action_icons', DEFAULT: ()},
00122                'i18n:domain': {},
00123                'xmlns:i18n': {},
00124              },
00125           'action-icon':
00126              { 'category': {},
00127                'action_id': {},
00128                'title': {},
00129                'icon_expr': {},
00130                'priority': {CONVERTER: self._convertToInteger},
00131                'i18n:attributes': {},
00132              },
00133           }
00134 
00135     def _convertToInteger(self, val):
00136         return int(val.strip())
00137 
00138 InitializeClass(ActionIconsToolImportConfigurator)