Updated site upgrade code to use factory interfaces for shared tools
authorThierry Florac <thierry.florac@onf.fr>
Thu, 22 Mar 2018 14:48:33 +0100
changeset 490 30fcd0659601
parent 489 dcdb0ce828f8
child 491 828f9c9303cf
Updated site upgrade code to use factory interfaces for shared tools
src/pyams_content/generations/__init__.py
--- a/src/pyams_content/generations/__init__.py	Tue Mar 20 13:51:32 2018 +0100
+++ b/src/pyams_content/generations/__init__.py	Thu Mar 22 14:48:33 2018 +0100
@@ -22,6 +22,11 @@
 from pyams_content.interfaces import IBaseContent
 from pyams_content.root.interfaces import ISiteRootToolsConfiguration
 from pyams_content.shared.common.interfaces import IWfSharedContent
+from pyams_content.shared.form.interfaces import IFormsManagerFactory
+from pyams_content.shared.imagemap.interfaces import IImageMapManagerFactory
+from pyams_content.shared.logo.interfaces import ILogosManagerFactory
+from pyams_content.shared.news.interfaces import INewsManagerFactory
+from pyams_content.shared.view.interfaces import IViewsManagerFactory
 from pyams_utils.interfaces.site import ISiteGenerations
 from pyams_utils.interfaces.traversing import IPathElements
 from pyams_workflow.interfaces import IWorkflowState, IWorkflowPublicationInfo
@@ -35,11 +40,6 @@
 from pyams_content.reference import ReferencesManager
 from pyams_content.reference.pictograms import PictogramTable
 from pyams_content.shared.common.manager import SharedToolContainer
-from pyams_content.shared.form.manager import FormsManager
-from pyams_content.shared.imagemap.manager import ImageMapsManager
-from pyams_content.shared.logo.manager import LogosManager
-from pyams_content.shared.news.manager import NewsManager
-from pyams_content.shared.view.manager import ViewsManager
 from pyams_i18n.index import I18nTextIndexWithInterface
 from pyams_security.index import PrincipalsRoleIndex
 from pyams_utils.registry import utility_config, get_global_registry
@@ -52,6 +52,13 @@
     return Lexicon(NltkFullTextProcessor(language=language))
 
 
+RENAMED_CLASSES = {
+    'pyams_content.shared.common.review ReviewComment': 'pyams_content.features.review ReviewComment',
+    'pyams_content.shared.common.review ReviewCommentsContainer':
+        'pyams_content.features.review ReviewCommentsContainer'
+}
+
+
 REQUIRED_UTILITIES = ()
 
 REQUIRED_TABLES = (
@@ -59,11 +66,11 @@
 )
 
 REQUIRED_TOOLS = [
-    ('views', ViewsManager),
-    ('logos', LogosManager),
-    ('imagemaps', ImageMapsManager),
-    ('forms', FormsManager),
-    ('news', NewsManager)
+    ('views', IViewsManagerFactory),
+    ('logos', ILogosManagerFactory),
+    ('imagemaps', IImageMapManagerFactory),
+    ('forms', IFormsManagerFactory),
+    ('news', INewsManagerFactory)
 ]
 
 REQUIRED_INDEXES = [
@@ -111,7 +118,20 @@
 #
 
 def check_required_tables(site, tables=REQUIRED_TABLES, registry=None):
-    """Check for required reference tables"""
+    """Check for required reference tables
+
+    :param site: site root to check for tables
+    :param tables: iterable of reference tables to check or create; each occurrence is a tuple of three elements
+        containing configuration attribute name, default table name and table factory
+    :param registry: optional registry object
+
+    For each required table, the checking process is:
+    - get table name from current site configuration, or from configuration settings using
+        'pyams_content.config.{table_name}' parameter where 'table_name' is the attribute name given as first
+        tuple argument
+    - if table doesn't exists, create table using given factory and given table name, and store this name into
+        current site configuration
+    """
 
     def get_tables_manager():
         # check references tables manager
@@ -142,7 +162,7 @@
         tables_manager = get_tables_manager()
         for attr, name, factory in tables:
             table_name = getattr(tools_configuration, attr, None) or \
-                         registry.settings.get('pyams_config.config.{0}'.format(attr), name)
+                         registry.settings.get('pyams_content.config.{0}'.format(attr), name)
             if table_name not in tables_manager:
                 table = factory()
                 logger.info("Creating table {0!r}...".format(table))
@@ -156,7 +176,25 @@
 #
 
 def check_required_tools(site, config_interface, tools):
-    """Check for required shared tools"""
+    """Check for required shared tools
+
+    :param site: site root to check for tools
+    :param config_interface: site configuration interface; configuration attributes are updated
+        when tools are created
+    :param tools: iterable of shared tools to check or create; each occurrence is a tuple of two elements
+        containing the tool name and it's factory interface.
+
+    For each required tool, the checking process is:
+    - get factory name from settings; the settings parameter name is 'pyams_content.config.{name}_tool_factory',
+        where 'name' is the first element of tool's tuple configuration
+    - if factory name is set as 'None' or '--', tool checking and creation is skipped
+    - otherwise, we look for requested tool
+    - if there is no shared tool with given name, we look for factory: this can be given as dotted Python name in
+        configuration settings, as a named adapter from site manager to given factory interface (with name set as
+        requested tool name), as an anonymous adapter from site manager to given factory interface, or as a utility
+        providing given factory interface
+    - if factory is found, the shared tool is created and it's name is stored into current site configuration
+    """
 
     def get_tools_manager(site, config, registry=None):
         """Check for shared tools manager"""
@@ -189,7 +227,7 @@
         """Create required shared tools"""
         if registry is None:
             registry = get_global_registry()
-        for name, default_factory in tools:
+        for name, factory_interface in tools:
             factory = registry.settings.get('pyams_content.config.{name}_tool_factory'.format(name=name))
             if (factory is None) or (factory.upper() not in ('NONE', '--')):
                 attr_name = '{name}_tool_name'.format(name=name)
@@ -198,12 +236,17 @@
                 if tool_name not in manager:
                     if factory is not None:
                         factory = DottedNameResolver().resolve(factory)
-                    else:
-                        factory = default_factory
-                    tool = factory()
-                    registry.notify(ObjectCreatedEvent(tool))
-                    manager[tool_name] = tool
-                    setattr(config, attr_name, tool_name)
+                    if factory is None:
+                        factory = registry.queryAdapter(manager, factory_interface, name=name)
+                        if factory is None:
+                            factory = registry.queryAdapter(manager, factory_interface)
+                            if factory is None:
+                                factory = registry.queryUtility(factory_interface)
+                    if factory is not None:
+                        tool = factory()
+                        registry.notify(ObjectCreatedEvent(tool))
+                        manager[tool_name] = tool
+                        setattr(config, attr_name, tool_name)
 
     configuration = config_interface(site, None)
     if configuration is not None: