Back to index

plone3  3.1.7
contenttyperegistry.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 """Content type registry xml adapters and setup handlers.
00014 
00015 $Id: contenttyperegistry.py 76996 2007-06-24 00:18:49Z hannosch $
00016 """
00017 
00018 from zope.component import adapts
00019 
00020 from Products.GenericSetup.interfaces import ISetupEnviron
00021 from Products.GenericSetup.utils import exportObjects
00022 from Products.GenericSetup.utils import importObjects
00023 from Products.GenericSetup.utils import XMLAdapterBase
00024 
00025 from Products.CMFCore.interfaces import IContentTypeRegistry
00026 from Products.CMFCore.utils import getToolByName
00027 
00028 
00029 class ContentTypeRegistryXMLAdapter(XMLAdapterBase):
00030 
00031     """XML im- and exporter for ContentTypeRegistry.
00032     """
00033 
00034     adapts(IContentTypeRegistry, ISetupEnviron)
00035 
00036     _LOGGER_ID = 'contenttypes'
00037 
00038     name = 'contenttyperegistry'
00039 
00040     def _exportNode(self):
00041         """Export the object as a DOM node.
00042         """
00043         node = self._getObjectNode('object')
00044         node.appendChild(self._extractPredicates())
00045 
00046         self._logger.info('Content type registry exported.')
00047         return node
00048 
00049     def _importNode(self, node):
00050         """Import the object from the DOM node.
00051         """
00052         if self.environ.shouldPurge():
00053             self._purgePredicates()
00054 
00055         self._initPredicates(node)
00056 
00057         self._logger.info('Content type registry imported.')
00058 
00059     def _extractPredicates(self):
00060         fragment = self._doc.createDocumentFragment()
00061         for predicate_id, info in self.context.listPredicates():
00062             child = self._doc.createElement('predicate')
00063             child.setAttribute('name', predicate_id)
00064             child.setAttribute('predicate_type', info[0].PREDICATE_TYPE)
00065             child.setAttribute('content_type_name', info[1])
00066             for argument in self._crackArgs(info[0]):
00067                 sub = self._doc.createElement('argument')
00068                 sub.setAttribute('value', argument)
00069                 child.appendChild(sub)
00070             fragment.appendChild(child)
00071         return fragment
00072 
00073     def _purgePredicates(self):
00074         self.context.__init__()
00075 
00076     def _initPredicates(self, node):
00077         for child in node.childNodes:
00078             if child.nodeName != 'predicate':
00079                 continue
00080             parent = self.context
00081 
00082             predicate_id = str(child.getAttribute('name'))
00083             predicate_type = str(child.getAttribute('predicate_type'))
00084             content_type_name = str(child.getAttribute('content_type_name'))
00085             if predicate_id not in parent.predicate_ids:
00086                 parent.addPredicate(predicate_id, predicate_type)
00087 
00088             arguments = []
00089             for sub in child.childNodes:
00090                 if sub.nodeName != 'argument':
00091                     continue
00092                 arguments.append(str(sub.getAttribute('value')))
00093             parent.getPredicate(predicate_id).edit(*arguments)
00094             parent.assignTypeName(predicate_id, content_type_name)
00095 
00096     _KNOWN_PREDICATE_TYPES = {
00097         'major_minor': lambda x: (','.join(x.major or ()),
00098                                   ','.join(x.minor or ())),
00099         'extension': lambda x: (','.join(x.extensions or ()),),
00100         'mimetype_regex': lambda x: (x.pattern and x.pattern.pattern or '',),
00101         'name_regex': lambda x: (x.pattern and x.pattern.pattern or '',),
00102     }
00103 
00104     def _crackArgs(self, predicate):
00105         cracker = self._KNOWN_PREDICATE_TYPES.get(predicate.PREDICATE_TYPE)
00106         if cracker is not None:
00107             return cracker(predicate)
00108         return ()  # XXX:  raise?
00109 
00110 
00111 def importContentTypeRegistry(context):
00112     """Import content type registry settings from an XML file.
00113     """
00114     site = context.getSite()
00115     tool = getToolByName(site, 'content_type_registry')
00116 
00117     importObjects(tool, '', context)
00118 
00119 def exportContentTypeRegistry(context):
00120     """Export content type registry settings as an XML file.
00121     """
00122     site = context.getSite()
00123     tool = getToolByName(site, 'content_type_registry', None)
00124     if tool is None:
00125         logger = context.getLogger('contenttypes')
00126         logger.info('Nothing to export.')
00127         return
00128 
00129     exportObjects(tool, '', context)