From 2fa7eb24245a242baf0b4f05f1700522b06614ce Mon Sep 17 00:00:00 2001
From: Nicolas Dumazet <nicolas.dumazet@nexedi.com>
Date: Mon, 31 Jan 2011 12:55:48 +0000
Subject: [PATCH] AccessorHolderType meta class, and clear fromPropertyHolder
 factory

git-svn-id: https://svn.erp5.org/repos/public/erp5/trunk@42809 20353a03-c40f-0410-a6d1-a30d3c3de9de
---
 product/ERP5Type/Tool/PropertySheetTool.py  |   6 +-
 product/ERP5Type/dynamic/accessor_holder.py | 127 ++++++++++----------
 2 files changed, 67 insertions(+), 66 deletions(-)

diff --git a/product/ERP5Type/Tool/PropertySheetTool.py b/product/ERP5Type/Tool/PropertySheetTool.py
index 70a04b6bff..73d8377343 100644
--- a/product/ERP5Type/Tool/PropertySheetTool.py
+++ b/product/ERP5Type/Tool/PropertySheetTool.py
@@ -37,7 +37,7 @@ from Products.ERP5Type.Base import PropertyHolder
 from Products.CMFCore.utils import getToolByName
 from Products.CMFCore.Expression import Expression
 
-from Products.ERP5Type.dynamic.accessor_holder import _createAccessorHolderFromPropertyHolder
+from Products.ERP5Type.dynamic.accessor_holder import AccessorHolderType
 
 from zLOG import LOG, ERROR, INFO
 
@@ -191,7 +191,7 @@ class PropertySheetTool(BaseTool):
     property_holder._categories = getattr(property_sheet, '_categories', [])
     property_holder._constraints = getattr(property_sheet, '_constraints', [])
 
-    return _createCommonPropertySheetAccessorHolder(
+    return AccessorHolderType.fromPropertyHolder(
       self.getPortalObject(),
       property_holder,
       'erp5.filesystem_accessor_holder')
@@ -215,7 +215,7 @@ class PropertySheetTool(BaseTool):
       property_holder._categories, \
       property_holder._constraints = definition_tuple
 
-    return _createAccessorHolderFromPropertyHolder(
+    return AccessorHolderType.fromPropertyHolder(
       self.getPortalObject(),
       property_holder,
       'erp5.accessor_holder')
diff --git a/product/ERP5Type/dynamic/accessor_holder.py b/product/ERP5Type/dynamic/accessor_holder.py
index 4636890886..9db3ddb2bf 100644
--- a/product/ERP5Type/dynamic/accessor_holder.py
+++ b/product/ERP5Type/dynamic/accessor_holder.py
@@ -29,8 +29,7 @@
 This module should include most code related to the generation of
 Accessor Holders, that is, generation of methods for ERP5
 
-* Ideally, PropertyHolder class should be defined here, as well
-as a base class for all erp5.accessor_holder Accessor Holders.
+* Ideally, PropertyHolder class should be defined here
 * Utils, Property Sheet Tool can be probably be cleaned up as well by
 moving specialized code here.
 """
@@ -43,65 +42,67 @@ from Products.ERP5Type.Globals import InitializeClass
 
 from zLOG import LOG, ERROR, INFO
 
-def _createAccessorHolderFromPropertyHolder(property_holder,
-                                            portal,
-                                            accessor_holder_module_name):
-  """
-  Create a new accessor holder class from the given Property Holder
-  within the given accessor holder module (when the migration will
-  be finished, there should only be one accessor holder module)
-  """
-  property_sheet_id = property_holder.__name__
-  setDefaultClassProperties(property_holder)
-
-  try:
-    setDefaultProperties(property_holder,
-                         object=portal,
-                         portal=portal)
-  except:
-    LOG("Tool.PropertySheetTool", ERROR,
-        "Could not generate accessor holder class for %s (module=%s)" % \
-        (property_sheet_id, accessor_holder_module_name),
-        error=sys.exc_info())
-
-    raise
-
-  # Create the new accessor holder class and set its module properly
-  accessor_holder_class = type(property_sheet_id, (object,), dict(
-    __module__ = accessor_holder_module_name,
-    constraints = property_holder.constraints,
-    # The following attributes have been defined only because they
-    # are being used in ERP5Type.Utils when getting all the
-    # property_sheets of the property_holder (then, they are added
-    # to the local properties, categories and constraints lists)
-    _properties = property_holder._properties,
-    # Necessary for getBaseCategoryList
-    _categories = property_holder._categories,
-    _constraints = property_holder._constraints,
-    security = property_holder.security
-    ))
-
-  # Set all the accessors (defined by a tuple) from the Property
-  # Holder to the new accessor holder class (code coming from
-  # createAccessor in Base.PropertyHolder)
-  for id, fake_accessor in property_holder._getItemList():
-    if not isinstance(fake_accessor, tuple):
-      continue
-
-    if fake_accessor is PropertyHolder.WORKFLOW_METHOD_MARKER:
-      # Case 1 : a workflow method only
-      accessor = Base._doNothing
-    else:
-      # Case 2 : a workflow method over an accessor
-      (accessor_class, accessor_args, key) = fake_accessor
-      accessor = accessor_class(id, key, *accessor_args)
-
-    # Add the accessor to the accessor holder
-    setattr(accessor_holder_class, id, accessor)
-
-  property_holder.security.apply(accessor_holder_class)
-  InitializeClass(accessor_holder_class)
-  return accessor_holder_class
+class AccessorHolderType(type):
+  @classmethod
+  def fromPropertyHolder(meta_type,
+                         property_holder,
+                         portal=None,
+                         accessor_holder_module_name=None):
+    """
+    Create a new accessor holder class from the given Property Holder
+    within the given accessor holder module
+    """
+    property_sheet_id = property_holder.__name__
+    setDefaultClassProperties(property_holder)
+
+    try:
+      setDefaultProperties(property_holder,
+                           object=portal,
+                           portal=portal)
+    except:
+      LOG("Tool.PropertySheetTool", ERROR,
+          "Could not generate accessor holder class for %s (module=%s)" % \
+          (property_sheet_id, accessor_holder_module_name),
+          error=sys.exc_info())
+
+      raise
+
+    # Create the new accessor holder class and set its module properly
+    accessor_holder_class = meta_type(property_sheet_id, (object,), dict(
+      __module__ = accessor_holder_module_name,
+      constraints = property_holder.constraints,
+      # The following attributes have been defined only because they
+      # are being used in ERP5Type.Utils when getting all the
+      # property_sheets of the property_holder (then, they are added
+      # to the local properties, categories and constraints lists)
+      _properties = property_holder._properties,
+      # Necessary for getBaseCategoryList
+      _categories = property_holder._categories,
+      _constraints = property_holder._constraints,
+      security = property_holder.security
+      ))
+
+    # Set all the accessors (defined by a tuple) from the Property
+    # Holder to the new accessor holder class (code coming from
+    # createAccessor in Base.PropertyHolder)
+    for id, fake_accessor in property_holder._getItemList():
+      if not isinstance(fake_accessor, tuple):
+        continue
+
+      if fake_accessor is PropertyHolder.WORKFLOW_METHOD_MARKER:
+        # Case 1 : a workflow method only
+        accessor = Base._doNothing
+      else:
+        # Case 2 : a workflow method over an accessor
+        (accessor_class, accessor_args, key) = fake_accessor
+        accessor = accessor_class(id, key, *accessor_args)
+
+      # Add the accessor to the accessor holder
+      setattr(accessor_holder_class, id, accessor)
+
+    property_holder.security.apply(accessor_holder_class)
+    InitializeClass(accessor_holder_class)
+    return accessor_holder_class
 
 
 generating_base_accessors = False
@@ -136,7 +137,7 @@ def _generateBaseAccessorHolder(portal,
                          econtext,
                          base_category_list)
 
-  accessor_holder = _createAccessorHolderFromPropertyHolder(
+  accessor_holder = AccessorHolderType.fromPropertyHolder(
                       property_holder,
                       portal,
                       'erp5.accessor_holder',
@@ -172,7 +173,7 @@ def _generatePreferenceToolAccessorHolder(portal, accessor_holder_list,
           if read_permission:
             property_holder.declareProtected(read_permission, attribute_name)
 
-  accessor_holder = _createAccessorHolderFromPropertyHolder(
+  accessor_holder = AccessorHolderType.fromPropertyHolder(
                       property_holder,
                       portal,
                       'erp5.accessor_holder',
-- 
2.30.9