Back to index

plone3  3.1.7
exportimport.py
Go to the documentation of this file.
00001 from zope.interface import implements
00002 
00003 from zope.component import adapts
00004 from zope.component import getSiteManager
00005 from zope.component import queryMultiAdapter
00006 
00007 from zope.component.interfaces import IComponentRegistry
00008 
00009 from plone.browserlayer.interfaces import ILocalBrowserLayerType
00010 from plone.browserlayer.utils import register_layer
00011 from plone.browserlayer.utils import unregister_layer
00012 
00013 from Products.GenericSetup.interfaces import IBody
00014 from Products.GenericSetup.interfaces import ISetupEnviron
00015 
00016 from Products.GenericSetup.utils import XMLAdapterBase
00017 from Products.GenericSetup.utils import _getDottedName
00018 from Products.GenericSetup.utils import _resolveDottedName
00019 
00020 def dummyGetId():
00021     return ''
00022 
00023 class BrowserLayerXMLAdapter(XMLAdapterBase):
00024     """Im- and exporter for local browser layers
00025     """
00026     implements(IBody)
00027     adapts(IComponentRegistry, ISetupEnviron)
00028     
00029     name = 'browserlayer'
00030     _LOGGER_ID = 'browserlayer'
00031     
00032     def _exportNode(self):
00033         # hack around an issue where _getObjectNode expects to have the context
00034         # a meta_type and a getId method, which isn't the case for a component
00035         # registry
00036         if IComponentRegistry.providedBy(self.context):
00037             self.context.meta_type = 'ComponentRegistry'
00038             self.context.getId = dummyGetId
00039         node = self._getObjectNode('layers')
00040         if IComponentRegistry.providedBy(self.context):
00041             del(self.context.meta_type)
00042             del(self.context.getId)
00043         node.appendChild(self._extractLayers())
00044         self._logger.info('Browser layers exported')
00045         return node
00046 
00047     def _importNode(self, node):
00048         self._initProvider(node)
00049         self._logger.info('Browser layers imported')
00050 
00051     def _initProvider(self, node):
00052         if self.environ.shouldPurge():
00053             self._purgeLayers()
00054         self._initLayers(node)
00055 
00056     def _purgeLayers(self):
00057         registeredLayers = [r.name for r in self.context.registeredUtilities() 
00058                             if r.provided == ILocalBrowserLayerType]
00059         for name in registeredLayers:
00060             unregister_layer(name, site_manager=self.context)
00061 
00062     def _initLayers(self, node):
00063         for child in node.childNodes:
00064             if child.nodeName.lower() == 'layer':
00065                 name = child.getAttribute('name')
00066                 interface = _resolveDottedName(child.getAttribute('interface'))
00067                 register_layer(interface, name, site_manager=self.context)
00068     
00069     def _extractLayers(self):
00070         fragment = self._doc.createDocumentFragment()
00071         
00072         registrations = [r for r in self.context.registeredUtilities() 
00073                             if r.provided == ILocalBrowserLayerType]
00074                             
00075         for r in registrations:
00076             child = self._doc.createElement('layer')
00077             child.setAttribute('name', r.name)
00078             child.setAttribute('interface', _getDottedName(r.component))
00079             fragment.appendChild(child)
00080             
00081         return fragment
00082 
00083 def importLayers(context):
00084     """Import local browser layers
00085     """
00086     sm = getSiteManager(context.getSite())
00087     if sm is None or not IComponentRegistry.providedBy(sm):
00088         logger = context.getLogger('browserlayer')
00089         logger.info("Can not register components - no site manager found.")
00090         return
00091 
00092     importer = queryMultiAdapter((sm, context), IBody, name=u'plone.browserlayer')
00093     if importer is not None:
00094         filename = '%s%s' % (importer.name, importer.suffix)
00095         body = context.readDataFile(filename)
00096         if body is not None:
00097             importer.filename = filename # for error reporting
00098             importer.body = body
00099 
00100 def exportLayers(context):
00101     """Export local browser layers
00102     """
00103     sm = getSiteManager(context.getSite())
00104     if sm is None or not IComponentRegistry.providedBy(sm):
00105         logger = context.getLogger('browserlayer')
00106         logger.info("Can not register components - no site manager found.")
00107         return
00108 
00109     exporter = queryMultiAdapter((sm, context), IBody, name=u'plone.browserlayer')
00110     if exporter is not None:
00111         filename = '%s%s' % (exporter.name, exporter.suffix)
00112         body = exporter.body
00113         if body is not None:
00114             context.writeDataFile(filename, body, exporter.mime_type)