Back to index

plone3  3.1.7
resourceregistry.py
Go to the documentation of this file.
00001 from zope.app import zapi
00002 
00003 from Products.CMFCore.utils import getToolByName
00004 
00005 from Products.GenericSetup.interfaces import IBody
00006 #from Products.GenericSetup.utils import I18NURI
00007 from Products.GenericSetup.utils import XMLAdapterBase
00008 
00009 
00010 def importResRegistry(context, reg_id, reg_title, filename):
00011     """
00012     Import resource registry.
00013     """
00014     site = context.getSite()
00015     logger = context.getLogger('resourceregistry')
00016 
00017     body = context.readDataFile(filename)
00018     if body is None:
00019         logger.info("%s: Nothing to import" % reg_title)
00020         return
00021 
00022     res_reg = getToolByName(site, reg_id)
00023 
00024     importer = zapi.queryMultiAdapter((res_reg, context), IBody)
00025     if importer is None:
00026         logger.warning("%s: Import adapter missing." % reg_title)
00027         return
00028 
00029     importer.body = body
00030     logger.info("%s imported." % reg_title)
00031 
00032 def exportResRegistry(context, reg_id, reg_title, filename):
00033     """
00034     Export resource registry.
00035     """
00036     site = context.getSite()
00037     logger = context.getLogger('resourceregistry')
00038     res_reg = getToolByName(site, reg_id, None)
00039     if res_reg is None:
00040         logger.info("%s: Nothing to export." % reg_title)
00041         return
00042 
00043     exporter = zapi.queryMultiAdapter((res_reg, context), IBody)
00044     if exporter is None:
00045         logger.warning("%s: Export adapter missing." % reg_title)
00046         return
00047 
00048     context.writeDataFile(filename, exporter.body, exporter.mime_type)
00049     logger.info("%s exported" % reg_title)
00050 
00051 
00052 class ResourceRegistryNodeAdapter(XMLAdapterBase):
00053 
00054     unregister_method = 'unregisterResource'
00055 
00056     def _exportNode(self):
00057         """
00058         Export the object as a DOM node.
00059         """
00060         node = self._getObjectNode('object')
00061         registry = getToolByName(self.context, self.registry_id)
00062         node.setAttribute('autogroup', str(registry.getAutoGroupingMode()))
00063         #node.setAttribute('xmlns:i18n', I18NURI)
00064         child = self._extractResourceInfo()
00065         node.appendChild(child)
00066         return node
00067 
00068     def _importNode(self, node):
00069         """
00070         Import the object from the DOM node.
00071         """
00072         registry = getToolByName(self.context, self.registry_id)
00073         if self.environ.shouldPurge():
00074             registry.clearResources()
00075         
00076         for key, value in node.attributes.items():
00077             key = str(key)
00078             value = value.lower().strip()
00079             if key == 'autogroup':
00080                 registry.setAutoGroupingMode(value in [ 'true', 'yes', '1'])
00081 
00082         self._initResources(node)
00083 
00084     def _extractResourceInfo(self):
00085         """
00086         Extract the information for each of the registered resources.
00087         """
00088         fragment = self._doc.createDocumentFragment()
00089         registry = getToolByName(self.context, self.registry_id)
00090         resources = registry.getResources()
00091         for resource in resources:
00092             data = resource._data.copy()
00093             child = self._doc.createElement(self.resource_type)
00094             for key, value in data.items():
00095                 if type(value) == type(True) or type(value) == type(0):
00096                     value = str(value)
00097                 child.setAttribute(key, value)
00098             fragment.appendChild(child)
00099         return fragment
00100 
00101     def _initResources(self, node):
00102         """
00103         Initialize the registered resources based on the contents of
00104         the provided DOM node.
00105         """
00106         registry = getToolByName(self.context, self.registry_id)
00107         reg_method = getattr(registry, self.register_method)
00108         unreg_method = getattr(registry, self.unregister_method)
00109         update_method = getattr(registry, self.update_method)
00110         for child in node.childNodes:
00111             if child.nodeName != self.resource_type:
00112                 continue
00113 
00114             data = {}
00115             method = reg_method
00116             position = None
00117             for key, value in child.attributes.items():
00118                 key = str(key)
00119                 if key == 'update':
00120                     method = update_method
00121                     continue
00122                 if key == 'remove':
00123                     method = unreg_method
00124                     break
00125                 if key in ('position-before', 'insert-before'):
00126                     position = ('Before', value)
00127                     continue
00128                 if key in ('position-after', 'insert-after'):
00129                     position = ('After', value)
00130                     continue
00131                 if key in ('position-top', 'insert-top'):
00132                     position = ('ToTop',)
00133                     continue
00134                 if key in ('position-bottom', 'insert-bottom'):
00135                     position = ('ToBottom',)
00136                     continue
00137                 if key == 'id':
00138                     res_id = str(value)
00139                 elif value.lower() == 'false':
00140                     data[key] = False
00141                 elif value.lower() == 'true':
00142                     data[key] = True
00143                 else:
00144                     try:
00145                         data[key] = int(value)
00146                     except ValueError:
00147                         data[key] = str(value)
00148 
00149             # unreg_method doesn't expect any keyword arguments
00150             # and has to be called separately (this feels dirty..)
00151             if method == unreg_method:
00152                 method(res_id)
00153             elif method == reg_method:
00154                 try:
00155                     data['skipCooking'] = True
00156                     method(res_id, **data)
00157                     del data['skipCooking']
00158                 except ValueError, e:
00159                     # this feels a bit dirty too, but we always want to update
00160                     # if the resource already exists (in which case 'ValueError:
00161                     # Duplicate id ...' is raised.
00162                     method=update_method
00163                     del data['skipCooking']
00164             if method == update_method:
00165                 method(res_id, **data)
00166             if position is not None:
00167                 moveMethod = getattr(registry, 'moveResource' + position[0])
00168                 moveMethod(res_id, *position[1:])
00169 
00170         registry.cookResources()