vendor/CMF/1.6.3/CMFCore

view exportimport/typeinfo.py @ 0:587011552858

import CMF 1.6.3
author bdelbosc
date Mon, 23 Apr 2007 13:58:01 +0000
parents
children
line source
1 ##############################################################################
2 #
3 # Copyright (c) 2005 Zope Corporation and Contributors. All Rights Reserved.
4 #
5 # This software is subject to the provisions of the Zope Public License,
6 # Version 2.1 (ZPL). A copy of the ZPL should accompany this distribution.
7 # THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
8 # WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
9 # WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
10 # FOR A PARTICULAR PURPOSE.
11 #
12 ##############################################################################
13 """Types tool xml adapters and setup handlers.
15 $Id$
16 """
18 from xml.dom.minidom import parseString
20 import Products
22 from Products.GenericSetup.utils import exportObjects
23 from Products.GenericSetup.utils import I18NURI
24 from Products.GenericSetup.utils import importObjects
25 from Products.GenericSetup.utils import ObjectManagerHelpers
26 from Products.GenericSetup.utils import PropertyManagerHelpers
27 from Products.GenericSetup.utils import XMLAdapterBase
29 from Products.CMFCore.interfaces import ITypeInformation
30 from Products.CMFCore.interfaces import ITypesTool
31 from Products.CMFCore.utils import getToolByName
34 class TypeInformationXMLAdapter(XMLAdapterBase, PropertyManagerHelpers):
36 """XML im- and exporter for TypeInformation.
37 """
39 __used_for__ = ITypeInformation
41 _LOGGER_ID = 'types'
43 def _exportNode(self):
44 """Export the object as a DOM node.
45 """
46 node = self._getObjectNode('object')
47 node.setAttribute('xmlns:i18n', I18NURI)
48 node.appendChild(self._extractProperties())
49 node.appendChild(self._extractAliases())
50 node.appendChild(self._extractActions())
52 self._logger.info('%r type info exported.' % self.context.getId())
53 return node
55 def _importNode(self, node):
56 """Import the object from the DOM node.
57 """
58 if self.environ.shouldPurge():
59 self._purgeProperties()
60 self._purgeAliases()
61 self._purgeActions()
63 self._initOldstyleProperties(node)
64 self._initProperties(node)
65 self._initAliases(node)
66 self._initActions(node)
68 obj_id = str(node.getAttribute('name'))
69 if not obj_id:
70 # BBB: for CMF 1.5 profiles
71 obj_id = str(node.getAttribute('id'))
72 self._logger.info('%r type info imported.' % obj_id)
74 def _extractAliases(self):
75 fragment = self._doc.createDocumentFragment()
76 aliases = self.context.getMethodAliases().items()
77 aliases.sort()
78 for k, v in aliases:
79 child = self._doc.createElement('alias')
80 child.setAttribute('from', k)
81 child.setAttribute('to', v)
82 fragment.appendChild(child)
83 return fragment
85 def _purgeAliases(self):
86 self.context.setMethodAliases({})
88 def _initAliases(self, node):
89 aliases = self.context.getMethodAliases()
90 for child in node.childNodes:
91 # BBB: for CMF 1.5 profiles
92 # 'alias' nodes moved one level up.
93 if child.nodeName == 'aliases':
94 for sub in child.childNodes:
95 if sub.nodeName != 'alias':
96 continue
97 k = str(sub.getAttribute('from'))
98 v = str(sub.getAttribute('to'))
99 aliases[k] = v
101 if child.nodeName != 'alias':
102 continue
103 k = str(child.getAttribute('from'))
104 v = str(child.getAttribute('to'))
105 aliases[k] = v
106 self.context.setMethodAliases(aliases)
108 def _extractActions(self):
109 fragment = self._doc.createDocumentFragment()
110 actions = self.context.listActions()
111 for ai in actions:
112 ai_info = ai.getMapping()
113 child = self._doc.createElement('action')
114 child.setAttribute('title', ai_info['title'])
115 child.setAttribute('action_id', ai_info['id'])
116 child.setAttribute('category', ai_info['category'])
117 child.setAttribute('condition_expr', ai_info['condition'])
118 child.setAttribute('url_expr', ai_info['action'])
119 child.setAttribute('visible', str(bool(ai_info['visible'])))
120 for permission in ai_info['permissions']:
121 sub = self._doc.createElement('permission')
122 sub.setAttribute('value', permission)
123 child.appendChild(sub)
124 fragment.appendChild(child)
125 return fragment
127 def _purgeActions(self):
128 self.context._actions = ()
130 def _initActions(self, node):
131 for child in node.childNodes:
132 if child.nodeName != 'action':
133 continue
134 title = str(child.getAttribute('title'))
135 id = str(child.getAttribute('action_id'))
136 category = str(child.getAttribute('category'))
137 condition = str(child.getAttribute('condition_expr'))
138 action = str(child.getAttribute('url_expr'))
139 visible = self._convertToBoolean(child.getAttribute('visible'))
140 permissions = []
141 for sub in child.childNodes:
142 if sub.nodeName != 'permission':
143 continue
144 permission = sub.getAttribute('value')
145 # BBB: for CMF 1.5 profiles
146 # Permission name moved from node text to 'value'.
147 if not permission:
148 permission = self._getNodeText(sub)
149 permissions.append(permission)
150 action_obj = self.context.getActionObject(category+'/'+id)
151 if action_obj is None:
152 self.context.addAction(id, title, action, condition,
153 tuple(permissions), category, visible)
154 else:
155 action_obj.edit(title=title, action=action,
156 condition=condition,
157 permissions=tuple(permissions),
158 visible=visible)
160 def _initOldstyleProperties(self, node):
161 if not node.hasAttribute('title'):
162 return
163 # BBB: for CMF 1.5 profiles
164 obj = self.context
166 title = node.getAttribute('title')
167 description = ''
168 content_meta_type = node.getAttribute('meta_type')
169 content_icon = node.getAttribute('icon')
170 immediate_view = node.getAttribute('immediate_view')
171 global_allow = self._convertToBoolean(node.getAttribute(
172 'global_allow'))
173 filter_content_types = self._convertToBoolean(node.getAttribute(
174 'filter_content_types'))
175 allowed_content_types = []
176 allow_discussion = self._convertToBoolean(node.getAttribute(
177 'allow_discussion'))
178 for child in node.childNodes:
179 if child.nodeName == 'description':
180 description += self._getNodeText(child)
181 elif child.nodeName == 'allowed_content_type':
182 allowed_content_types.append(self._getNodeText(child))
183 obj._updateProperty('title', title)
184 obj._updateProperty('description', description)
185 obj._updateProperty('content_meta_type', content_meta_type)
186 obj._updateProperty('content_icon', content_icon)
187 obj._updateProperty('immediate_view', immediate_view)
188 obj._updateProperty('global_allow', global_allow)
189 obj._updateProperty('filter_content_types', filter_content_types)
190 obj._updateProperty('allowed_content_types', allowed_content_types)
191 obj._updateProperty('allow_discussion', allow_discussion)
193 if node.getAttribute('kind') == 'Factory-based Type Information':
194 product = node.getAttribute('product')
195 factory = node.getAttribute('factory')
196 obj._updateProperty('product', product)
197 obj._updateProperty('factory', factory)
198 else:
199 constructor_path = node.getAttribute('constructor_path')
200 permission = node.getAttribute('permission')
201 obj._updateProperty('constructor_path', constructor_path)
202 obj._updateProperty('permission', permission)
205 class TypesToolXMLAdapter(XMLAdapterBase, ObjectManagerHelpers,
206 PropertyManagerHelpers):
208 """XML im- and exporter for TypesTool.
209 """
211 __used_for__ = ITypesTool
213 _LOGGER_ID = 'types'
215 name = 'types'
217 def _exportNode(self):
218 """Export the object as a DOM node.
219 """
220 node = self._getObjectNode('object')
221 node.appendChild(self._extractProperties())
222 node.appendChild(self._extractObjects())
224 self._logger.info('Types tool exported.')
225 return node
227 def _importNode(self, node):
228 """Import the object from the DOM node.
229 """
230 if self.environ.shouldPurge():
231 self._purgeProperties()
232 self._purgeObjects()
234 self._initProperties(node)
235 self._initObjects(node)
236 self._initBBBObjects(node)
238 self._logger.info('Types tool imported.')
240 def _initBBBObjects(self, node):
241 for child in node.childNodes:
242 if child.nodeName != 'type':
243 continue
244 parent = self.context
246 obj_id = str(child.getAttribute('id'))
247 if obj_id not in parent.objectIds():
248 filename = str(child.getAttribute('filename'))
249 if not filename:
250 filename = 'types/%s.xml' % obj_id.replace(' ', '_')
251 # cheating here for BBB: readDataFile is no interface method
252 body = self.environ.readDataFile(filename)
253 if body is None:
254 break
255 root = parseString(body).documentElement
256 if root.getAttribute('name') != obj_id:
257 if root.getAttribute('id') != obj_id:
258 break
259 meta_type = str(root.getAttribute('kind'))
260 if not meta_type:
261 meta_type = str(root.getAttribute('meta_type'))
262 for mt_info in Products.meta_types:
263 if mt_info['name'] == meta_type:
264 parent._setObject(obj_id, mt_info['instance'](obj_id))
265 break
268 def importTypesTool(context):
269 """Import types tool and content types from XML files.
270 """
271 site = context.getSite()
272 tool = getToolByName(site, 'portal_types')
274 importObjects(tool, '', context)
276 def exportTypesTool(context):
277 """Export types tool content types as a set of XML files.
278 """
279 site = context.getSite()
280 tool = getToolByName(site, 'portal_types', None)
281 if tool is None:
282 logger = context.getLogger('types')
283 logger.info('Nothing to export.')
284 return
286 exportObjects(tool, '', context)