def _initUtilities(self, node): for child in node.childNodes: if child.nodeName != 'utility': continue provided = _resolveDottedName(child.getAttribute('interface')) name = unicode(str(child.getAttribute('name'))) component = child.getAttribute('component') component = component and _resolveDottedName(component) or None factory = child.getAttribute('factory') factory = factory and _resolveDottedName(factory) or None obj_path = child.getAttribute('object') if obj_path: site = self.environ.getSite() # we support registering aq_wrapped objects only for now if hasattr(site, 'aq_base'): # filter out empty path segments path = [f for f in obj_path.split('/') if f] # support for nested folder obj = self._recurseFolder(site, path) if obj is not None: self.context.registerUtility(obj, provided, name) else: # Log an error, not aq_wrapped self._logger.warning("The object %s was not acquisition " "wrapped. Registering these is not " "supported right now." % obj_path) elif component: self.context.registerUtility(component, provided, name) else: self.context.registerUtility(factory(), provided, name)
def _initUtilities(self, node): site = self._getSite() current_utilities = self.context.registeredUtilities() for child in node.childNodes: if child.nodeName != 'utility': continue provided = _resolveDottedName(child.getAttribute('interface')) name = unicode(str(child.getAttribute('name'))) component = child.getAttribute('component') component = component and _resolveDottedName(component) or None factory = child.getAttribute('factory') factory = factory and _resolveDottedName(factory) or None obj_path = child.getAttribute('object') if not component and not factory and obj_path is not None: # Support for registering the site itself if obj_path in ('', '/'): obj = site else: # BBB: filter out path segments, we did claim to support # nested paths once id_ = [p for p in obj_path.split('/') if p][0] obj = getattr(site, id_, None) if obj is not None: self.context.registerUtility(aq_base(obj), provided, name) else: # Log an error, object not found self._logger.warning("The object %s was not found, while " "trying to register an utility. The " "provided object definition was %s. " "The site used was: %s" % (repr(obj), obj_path, repr(site))) elif component: self.context.registerUtility(component, provided, name) elif factory is not None: current = [ utility for utility in current_utilities if utility.provided == provided and utility.name == name ] assert len(current) <= 1 new_utility = factory() if (current and type(aq_base(current[0].component)) == type(new_utility)): continue self.context.registerUtility(new_utility, provided, name) else: self._logger.warning("Invalid utility registration for " "interface %s" % provided)
def _updateExportStepsRegistry( self, encoding ): """ Update our export steps registry from our profile. """ fq = self._getFullyQualifiedProfileDirectory() f = open( os.path.join( fq, EXPORT_STEPS_XML ), 'r' ) xml = f.read() f.close() info_list = self._export_registry.parseXML( xml, encoding ) for step_info in info_list: id = step_info[ 'id' ] handler = _resolveDottedName( step_info[ 'handler' ] ) title = step_info.get( 'title', id ) description = ''.join( step_info.get( 'description', [] ) ) self._export_registry.registerStep( id=id , handler=handler , title=title , description=description )
def _updateImportStepsRegistry( self, encoding ): """ Update our import steps registry from our profile. """ context = self._getImportContext(self._import_context_id) xml = context.readDataFile(IMPORT_STEPS_XML) info_list = self._import_registry.parseXML( xml, encoding ) for step_info in info_list: id = step_info[ 'id' ] version = step_info[ 'version' ] handler = _resolveDottedName( step_info[ 'handler' ] ) dependencies = tuple( step_info.get( 'dependencies', () ) ) title = step_info.get( 'title', id ) description = ''.join( step_info.get( 'description', [] ) ) self._import_registry.registerStep( id=id , version=version , handler=handler , dependencies=dependencies , title=title , description=description )
def _updateExportStepsRegistry(self, context, encoding): """ Update our export steps registry from our profile. """ if context is None: context = self._getImportContext(self._import_context_id) xml = context.readDataFile(EXPORT_STEPS_XML) if xml is None: return info_list = self._export_registry.parseXML(xml, encoding) for step_info in info_list: id = step_info['id'] handler = _resolveDottedName(step_info['handler']) title = step_info.get('title', id) description = ''.join(step_info.get('description', [])) self._export_registry.registerStep(id=id, handler=handler, title=title, description=description, )
def _updateImportStepsRegistry(self, encoding): """ Update our import steps registry from our profile. """ fq = self._getFullyQualifiedProfileDirectory() f = open(os.path.join(fq, IMPORT_STEPS_XML), 'r') xml = f.read() f.close() info_list = self._import_registry.parseXML(xml, encoding) for step_info in info_list: id = step_info['id'] version = step_info['version'] handler = _resolveDottedName(step_info['handler']) dependencies = tuple(step_info.get('dependencies', ())) title = step_info.get('title', id) description = ''.join(step_info.get('description', [])) self._import_registry.registerStep(id=id, version=version, handler=handler, dependencies=dependencies, title=title, description=description)
def _updateExportStepsRegistry(self, context, encoding): """ Update our export steps registry from our profile. """ if context is None: context = self._getImportContext(self._import_context_id) xml = context.readDataFile(EXPORT_STEPS_XML) if xml is None: return info_list = self._export_registry.parseXML(xml, encoding) for step_info in info_list: id = step_info['id'] handler = _resolveDottedName(step_info['handler']) title = step_info.get('title', id) description = ''.join(step_info.get('description', [])) self._export_registry.registerStep( id=id, handler=handler, title=title, description=description, )
def _updateImportStepsRegistry(self, encoding): """ Update our import steps registry from our profile. """ context = self._getImportContext(self._import_context_id) xml = context.readDataFile(IMPORT_STEPS_XML) if xml is None: return info_list = self._import_registry.parseXML(xml, encoding) for step_info in info_list: id = step_info['id'] version = step_info['version'] handler = _resolveDottedName(step_info['handler']) dependencies = tuple(step_info.get('dependencies', ())) title = step_info.get('title', id) description = ''.join(step_info.get('description', [])) self._import_registry.registerStep(id=id, version=version, handler=handler, dependencies=dependencies, title=title, description=description)
def registerStep(self, id, handler, title=None, description=None): """ Register an export step. o 'id' is the unique identifier for this step o 'handler' is the dottoed name of a handler which should implement IImportPlugin. o 'title' is a one-line UI description for this step. If None, the first line of the documentation string of the step is used, or the id if no docstring can be found. o 'description' is a one-line UI description for this step. If None, the remaining line of the documentation string of the step is used, or default to ''. """ if not isinstance(handler, str): handler = _getDottedName(handler) if title is None or description is None: method = _resolveDottedName(handler) if method is None: t, d = id, "" else: t, d = _extractDocstring(method, id, "") title = title or t description = description or d info = {"id": id, "handler": handler, "title": title, "description": description} self._registered[id] = info
def registerStep(self, id, version, handler, dependencies=(), title=None, description=None): """ Register a setup step. o 'id' is a unique name for this step, o 'version' is a string for comparing versions, it is preferred to be a yyyy/mm/dd-ii formatted string (date plus two-digit ordinal). when comparing two version strings, the version with the lower sort order is considered the older version. - Newer versions of a step supplant older ones. - Attempting to register an older one after a newer one results in a KeyError. o 'handler' is the dottoed name of a handler which should implement IImportPlugin. o 'dependencies' is a tuple of step ids which have to run before this step in order to be able to run at all. Registration of steps that have unmet dependencies are deferred until the dependencies have been registered. o 'title' is a one-line UI description for this step. If None, the first line of the documentation string of the handler is used, or the id if no docstring can be found. o 'description' is a one-line UI description for this step. If None, the remaining line of the documentation string of the handler is used, or default to ''. """ already = self.getStepMetadata(id) if already and already["version"] > version: raise KeyError("Existing registration for step %s, version %s" % (id, already["version"])) if not isinstance(handler, str): handler = _getDottedName(handler) if title is None or description is None: method = _resolveDottedName(handler) if method is None: t, d = id, "" else: t, d = _extractDocstring(method, id, "") title = title or t description = description or d info = { "id": id, "version": version, "handler": handler, "dependencies": dependencies, "title": title, "description": description, } self._registered[id] = info
def _initUtilities(self, node): for child in node.childNodes: if child.nodeName != 'utility': continue provided = _resolveDottedName(child.getAttribute('interface')) name = unicode(str(child.getAttribute('name'))) component = child.getAttribute('component') component = component and _resolveDottedName(component) or None factory = child.getAttribute('factory') factory = factory and _resolveDottedName(factory) or None obj_path = child.getAttribute('object') if not component and not factory and obj_path is not None: # Get the site by either __parent__ or Acquisition site = getattr(self.context, '__parent__', None) if site is None: site = aq_parent(self.context) # Support for registering the site itself if obj_path in ('', '/'): obj = site else: # BBB: filter out path segments, we did claim to support # nested paths once id_ = [p for p in obj_path.split('/') if p][0] obj = getattr(site, id_, None) if obj is not None: self.context.registerUtility(aq_base(obj), provided, name) else: # Log an error, object not found self._logger.warning("The object %s was not found, while " "trying to register an utility. The " "provided object definition was %s. " "The site used was: %s" % (path, obj_path, repr(site))) elif component: self.context.registerUtility(component, provided, name) elif factory is not None: self.context.registerUtility(factory(), provided, name) else: self._logger.warning("Invalid utility registration for " "interface %s" % provided)
def importToolset(context): """ Import required / forbidden tools from XML file. """ site = context.getSite() encoding = context.getEncoding() logger = context.getLogger('toolset') xml = context.readDataFile(TOOLSET_XML) if xml is None: logger.debug('Nothing to import.') return setup_tool = context.getSetupTool() toolset = setup_tool.getToolsetRegistry() toolset.parseXML(xml, encoding) existing_ids = site.objectIds() existing_values = site.objectValues() for tool_id in toolset.listForbiddenTools(): if tool_id in existing_ids: site._delObject(tool_id) for info in toolset.listRequiredToolInfo(): tool_id = str(info['id']) tool_class = _resolveDottedName(info['class']) existing = getattr(aq_base(site), tool_id, None) # Don't even initialize the tool again, if it already exists. if existing is None: try: new_tool = tool_class() except TypeError: new_tool = tool_class(tool_id) else: try: new_tool._setId(tool_id) except (ConflictError, KeyboardInterrupt): raise except: # XXX: ImmutableId raises result of calling MessageDialog pass site._setObject(tool_id, new_tool) else: unwrapped = aq_base(existing) # don't use isinstance here as a subclass may need removing if type(unwrapped) != tool_class: site._delObject(tool_id) site._setObject(tool_id, tool_class()) logger.info('Toolset imported.')
def _initAdapters(self, node): for child in node.childNodes: if child.nodeName != 'adapter': continue factory = _resolveDottedName(child.getAttribute('factory')) provided = _resolveDottedName(child.getAttribute('provides')) name = unicode(str(child.getAttribute('name'))) for_ = child.getAttribute('for_') required = [] for interface in for_.split(u' '): if interface: required.append(_resolveDottedName(interface)) self.context.registerAdapter(factory, required=required, provided=provided, name=name)
def importToolset(context): """ Import required / forbidden tools from XML file. """ site = context.getSite() encoding = context.getEncoding() logger = context.getLogger('toolset') xml = context.readDataFile(TOOLSET_XML) if xml is None: logger.info('Nothing to import.') return setup_tool = context.getSetupTool() toolset = setup_tool.getToolsetRegistry() toolset.parseXML(xml, encoding) existing_ids = site.objectIds() existing_values = site.objectValues() for tool_id in toolset.listForbiddenTools(): if tool_id in existing_ids: site._delObject(tool_id) for info in toolset.listRequiredToolInfo(): tool_id = str(info['id']) tool_class = _resolveDottedName(info['class']) existing = getattr(aq_base(site), tool_id, None) # Don't even initialize the tool again, if it already exists. if existing is None: try: new_tool = tool_class() except TypeError: new_tool = tool_class(tool_id) else: try: new_tool._setId(tool_id) except (ConflictError, KeyboardInterrupt): raise except: # XXX: ImmutableId raises result of calling MessageDialog pass site._setObject(tool_id, new_tool) else: unwrapped = aq_base(existing) if not isinstance(unwrapped, tool_class): site._delObject(tool_id) site._setObject(tool_id, tool_class()) logger.info('Toolset imported.')
def importToolset(context): """ Import required / forbidden tools from XML file. """ site = context.getSite() encoding = context.getEncoding() logger = context.getLogger('toolset') xml = context.readDataFile(TOOLSET_XML) if xml is None: logger.info('Nothing to import.') return setup_tool = context.getSetupTool() toolset = setup_tool.getToolsetRegistry() toolset.parseXML(xml, encoding) existing_ids = site.objectIds() existing_values = site.objectValues() for tool_id in toolset.listForbiddenTools(): if tool_id in existing_ids: site._delObject(tool_id) for info in toolset.listRequiredToolInfo(): tool_id = str(info['id']) tool_class = _resolveDottedName(info['class']) existing = getattr(aq_base(site), tool_id, None) try: new_tool = tool_class() except TypeError: new_tool = tool_class(tool_id) else: try: new_tool._setId(tool_id) except: # XXX: ImmutableId raises result of calling MessageDialog pass if existing is None: site._setObject(tool_id, new_tool) else: unwrapped = aq_base(existing) if not isinstance(unwrapped, tool_class): site._delObject(tool_id) site._setObject(tool_id, tool_class()) logger.info('Toolset imported.')
def getStep(self, key, default=None): """ Return the IExportPlugin registered for 'key'. o Return 'default' if no such step is registered. """ marker = object() info = self._registered.get(key, marker) if info is marker: return default return _resolveDottedName(info['handler'])
def getStep( self, key, default=None ): """ Return the IExportPlugin registered for 'key'. o Return 'default' if no such step is registered. """ marker = object() info = self._registered.get( key, marker ) if info is marker: return default return _resolveDottedName( info[ 'handler' ] )
def getStepMetadata(self, key, default=None): """ Return a mapping of metadata for the step identified by 'key'. o Return 'default' if no such step is registered. o The 'handler' metadata is available via 'getStep'. """ info = self._registered.get(key) if info is None: return default result = info.copy() result['invalid'] = _resolveDottedName(result['handler']) is None return result
def importToolset(context): """ Import required / forbidden tools from XML file. """ site = context.getSite() encoding = context.getEncoding() xml = context.readDataFile(TOOLSET_XML) if xml is None: return 'Toolset: Nothing to import.' setup_tool = context.getSetupTool() toolset = setup_tool.getToolsetRegistry() toolset.parseXML(xml, encoding) existing_ids = site.objectIds() existing_values = site.objectValues() for tool_id in toolset.listForbiddenTools(): if tool_id in existing_ids: site._delObject(tool_id) for info in toolset.listRequiredToolInfo(): tool_id = str(info['id']) tool_class = _resolveDottedName(info['class']) existing = getattr(site, tool_id, None) new_tool = tool_class() # XXX: new_tool = mapply(tool_class, info) try: new_tool._setId(tool_id) except: # XXX: ImmutableId raises result of calling MessageDialog pass if existing is None: site._setObject(tool_id, new_tool) else: unwrapped = aq_base(existing) if not isinstance(unwrapped, tool_class): site._delObject(tool_id) site._setObject(tool_id, tool_class()) return 'Toolset imported.'
def getStepMetadata( self, key, default=None ): """ Return a mapping of metadata for the step identified by 'key'. o Return 'default' if no such step is registered. o The 'handler' metadata is available via 'getStep'. """ info = self._registered.get( key ) if info is None: return default result = info.copy() result['invalid'] = _resolveDottedName( result[ 'handler' ] ) is None return result
def importToolset( context ): """ Import required / forbidden tools from XML file. """ site = context.getSite() encoding = context.getEncoding() xml = context.readDataFile(TOOLSET_XML) if xml is None: return 'Toolset: Nothing to import.' setup_tool = getToolByName( site, 'portal_setup' ) toolset = setup_tool.getToolsetRegistry() toolset.parseXML(xml, encoding) existing_ids = site.objectIds() existing_values = site.objectValues() for tool_id in toolset.listForbiddenTools(): if tool_id in existing_ids: site._delObject( tool_id ) for info in toolset.listRequiredToolInfo(): tool_id = str( info[ 'id' ] ) tool_class = _resolveDottedName( info[ 'class' ] ) existing = getToolByName( site, tool_id, None ) new_tool = tool_class() if not isinstance( new_tool, ImmutableId ): new_tool._setId( tool_id ) if existing is None: site._setObject( tool_id, new_tool ) else: unwrapped = aq_base( existing ) if not isinstance( unwrapped, tool_class ): site._delObject( tool_id ) site._setObject( tool_id, new_tool ) return 'Toolset imported.'
def _makeInstance(self, instance_id, type_name, subdir, import_context): context = self.context class _OldStyleClass: pass if '.' in type_name: factory = _resolveDottedName(type_name) if getattr(factory, '__bases__', None) is not None: def _factory(instance_id, container=self.context, klass=factory): try: instance = klass(instance_id) except (TypeError, ValueError): instance = klass() instance._setId(instance_id) container._setObject(instance_id, instance) return instance factory = _factory else: factory = zapi.queryAdapter( self.context, IContentFactory, name=type_name, ) if factory is None: return None try: instance = factory(instance_id) except ValueError: # invalid type return None if context._getOb(instance_id, None) is None: context._setObject(instance_id, instance) return context._getOb(instance_id)
def _makeInstance(self, instance_id, type_name, subdir, import_context): context = self.context class _OldStyleClass: pass if '.' in type_name: factory = _resolveDottedName(type_name) if getattr(factory, '__bases__', None) is not None: def _factory(instance_id, container=self.context, klass=factory): try: instance = klass(instance_id) except (TypeError, ValueError): instance = klass() instance._setId(instance_id) container._setObject(instance_id, instance) return instance factory = _factory else: factory = queryNamedAdapter(self.context, IContentFactory, name=type_name, ) if factory is None: return None try: instance = factory(instance_id) except ValueError: # invalid type return None if context._getOb(instance_id, None) is None: context._setObject(instance_id, instance) return context._getOb(instance_id)
def registerStep(self, id, handler, title=None, description=None): """ Register an export step. o 'id' is the unique identifier for this step o 'handler' is the dottoed name of a handler which should implement IImportPlugin. o 'title' is a one-line UI description for this step. If None, the first line of the documentation string of the step is used, or the id if no docstring can be found. o 'description' is a one-line UI description for this step. If None, the remaining line of the documentation string of the step is used, or default to ''. """ if not isinstance(handler, str): handler = _getDottedName(handler) if title is None or description is None: method = _resolveDottedName(handler) if method is None: t, d = id, '' else: t, d = _extractDocstring(method, id, '') title = title or t description = description or d info = { 'id': id, 'handler': handler, 'title': title, 'description': description } self._registered[id] = info
def registerStep(self, id, version, handler, dependencies=(), title=None, description=None): """ Register a setup step. o 'id' is a unique name for this step, o 'version' is a string for comparing versions, it is preferred to be a yyyy/mm/dd-ii formatted string (date plus two-digit ordinal). when comparing two version strings, the version with the lower sort order is considered the older version. - Newer versions of a step supplant older ones. - Attempting to register an older one after a newer one results in a KeyError. o 'handler' is the dottoed name of a handler which should implement IImportPlugin. o 'dependencies' is a tuple of step ids which have to run before this step in order to be able to run at all. Registration of steps that have unmet dependencies are deferred until the dependencies have been registered. o 'title' is a one-line UI description for this step. If None, the first line of the documentation string of the handler is used, or the id if no docstring can be found. o 'description' is a one-line UI description for this step. If None, the remaining line of the documentation string of the handler is used, or default to ''. """ already = self.getStepMetadata(id) if already and already['version'] > version: raise KeyError('Existing registration for step %s, version %s' % (id, already['version'])) if not isinstance(handler, str): handler = _getDottedName(handler) if title is None or description is None: method = _resolveDottedName(handler) if method is None: t, d = id, '' else: t, d = _extractDocstring(method, id, '') title = title or t description = description or d info = { 'id': id, 'version': version, 'handler': handler, 'dependencies': dependencies, 'title': title, 'description': description } self._registered[id] = info