Back to index

plone3  3.1.7
storage.py
Go to the documentation of this file.
00001 import os
00002 
00003 from zope.component import getUtility, queryUtility, queryMultiAdapter
00004 
00005 from plone.app.viewletmanager.interfaces import IViewletSettingsStorage
00006 
00007 from Products.GenericSetup.interfaces import IBody
00008 from Products.GenericSetup.utils import XMLAdapterBase
00009 
00010 def importViewletSettingsStorage(context):
00011     """Import viewlet settings."""
00012     logger = context.getLogger('plone.app.viewletmanager')
00013 
00014     body = context.readDataFile('viewlets.xml')
00015     if body is None:
00016         logger.info("Nothing to import")
00017         return
00018 
00019     storage = getUtility(IViewletSettingsStorage)
00020 
00021     importer = queryMultiAdapter((storage, context), IBody)
00022     if importer is None:
00023         logger.warning("Import adapter missing.")
00024         return
00025         
00026     # set filename on importer so that syntax errors can be reported properly
00027     try:
00028         subdir = context._profile_path
00029     except AttributeError:
00030         subdir = ''
00031     importer.filename = os.path.join( subdir, 'viewlets.xml' )
00032 
00033     importer.body = body
00034     logger.info("Imported.")
00035 
00036 def exportViewletSettingsStorage(context):
00037     """Export viewlet settings."""
00038     logger = context.getLogger('plone.app.viewletmanager')
00039 
00040     storage = queryUtility(IViewletSettingsStorage)
00041 
00042     if storage is None:
00043         logger.info("Nothing to export")
00044         return
00045 
00046     exporter = queryMultiAdapter((storage, context), IBody)
00047     if exporter is None:
00048         logger.warning("Export adapter missing.")
00049         return
00050 
00051     context.writeDataFile('viewlets.xml', exporter.body, exporter.mime_type)
00052     logger.info("Exported.")
00053 
00054 
00055 class ViewletSettingsStorageNodeAdapter(XMLAdapterBase):
00056     __used_for__ = IViewletSettingsStorage
00057 
00058     def _exportNode(self):
00059         """
00060         Export the object as a DOM node.
00061         """
00062         output = self._doc.createElement('object')
00063         for nodename in ('order', 'hidden'):
00064             skins = getattr(self.context, '_'+nodename)
00065             for skin in skins:
00066                 for name in skins[skin]:
00067                     node = self._doc.createElement(nodename)
00068                     node.setAttribute('skinname', skin)
00069                     node.setAttribute('manager', name)
00070                     for viewlet in skins[skin][name]:
00071                         child = self._doc.createElement('viewlet')
00072                         child.setAttribute('name', viewlet)
00073                         node.appendChild(child)
00074                     output.appendChild(node)
00075         return output
00076 
00077     def _importNode(self, node):
00078         """
00079         Import the object from the DOM node.
00080         """
00081         storage = self.context
00082         purge = self.environ.shouldPurge()
00083         if node.getAttribute('purge'):
00084             purge = self._convertToBoolean(node.getAttribute('purge'))
00085         if purge:
00086             self._purgeViewletSettings()
00087         for child in node.childNodes:
00088             nodename = child.nodeName
00089             if nodename not in ('order', 'hidden'):
00090                 continue
00091             purgeChild = False
00092             if child.getAttribute('purge'):
00093                 purgeChild = self._convertToBoolean(
00094                                                   child.getAttribute('purge'))
00095             skinname = child.getAttribute('skinname')
00096             manager = child.getAttribute('manager')
00097             skins = getattr(storage, '_'+nodename)
00098             if skinname == '*':
00099                 for skinname in skins:
00100                     values = []
00101                     if not purgeChild:
00102                         values = list(skins[skinname].get(manager, []))
00103                     values = self._computeValues(values, child)
00104                     if nodename == 'order':
00105                         storage.setOrder(manager, skinname, tuple(values))
00106                     elif nodename == 'hidden':
00107                         storage.setHidden(manager, skinname, tuple(values))
00108 
00109             else:
00110                 values = []
00111                 if skinname in skins and not purgeChild:
00112                     values = list(skins[skinname].get(manager, []))
00113                 basename = child.getAttribute('based-on')
00114                 if basename in skins:
00115                     oldvalues = values
00116                     values = list(skins[basename].get(manager, []))
00117                     for value in oldvalues:
00118                         if value not in values:
00119                             viewlet = self._doc.createElement('viewlet')
00120                             viewlet.setAttribute('name', value)
00121                             if oldvalues.index(value) == 0:
00122                                 viewlet.setAttribute('insert-before', '*')
00123                             else:
00124                                 pos = oldvalues[oldvalues.index(value)-1]
00125                                 viewlet.setAttribute('insert-after', pos)
00126                             child.appendChild(viewlet)
00127                 values = self._computeValues(values, child)
00128                 if nodename == 'order':
00129                     storage.setOrder(manager, skinname, tuple(values))
00130                 elif nodename == 'hidden':
00131                     storage.setHidden(manager, skinname, tuple(values))
00132 
00133                 if child.hasAttribute('make_default'):
00134                     make_default = self._convertToBoolean(
00135                                         child.getAttribute('make_default'))
00136                     if make_default == True:
00137                         storage.setDefault(manager, skinname)
00138 
00139     def _purgeViewletSettings(self):
00140         storage = self.context
00141         for key in storage._order:
00142             storage._order[key].clear()
00143         for key in storage._hidden:
00144             storage._hidden[key].clear()
00145 
00146     def _computeValues(self, values, node):
00147         for child in node.childNodes:
00148             if child.nodeName != 'viewlet':
00149                 continue
00150             viewlet_name = child.getAttribute('name')
00151             if viewlet_name in values:
00152                 values.remove(viewlet_name)
00153 
00154             if child.hasAttribute('insert-before'):
00155                 insert_before = child.getAttribute('insert-before')
00156                 if insert_before == '*':
00157                     values.insert(0, viewlet_name)
00158                     continue
00159                 else:
00160                     try:
00161                         index = values.index(insert_before)
00162                         values.insert(index, viewlet_name)
00163                         continue
00164                     except ValueError:
00165                         pass
00166             elif child.hasAttribute('insert-after'):
00167                 insert_after = child.getAttribute('insert-after')
00168                 if insert_after == '*':
00169                     pass
00170                 else:
00171                     try:
00172                         index = values.index(insert_after)
00173                         values.insert(index+1, viewlet_name)
00174                         continue
00175                     except ValueError:
00176                         pass
00177 
00178             if not child.hasAttribute('remove'):
00179                 values.append(viewlet_name)
00180 
00181         return values
00182