From 4d3dce6043aa9236936603c04e617ceb421c2bdd Mon Sep 17 00:00:00 2001
From: Yoshinori Okuji <yo@nexedi.com>
Date: Fri, 22 Oct 2010 07:58:41 +0000
Subject: [PATCH] More simplification and safer exception catching.

git-svn-id: https://svn.erp5.org/repos/public/erp5/trunk@39465 20353a03-c40f-0410-a6d1-a30d3c3de9de
---
 product/ERP5Type/dynamic/portal_type_class.py | 58 +++++--------------
 1 file changed, 16 insertions(+), 42 deletions(-)

diff --git a/product/ERP5Type/dynamic/portal_type_class.py b/product/ERP5Type/dynamic/portal_type_class.py
index 0a15778124..487a8bcaed 100644
--- a/product/ERP5Type/dynamic/portal_type_class.py
+++ b/product/ERP5Type/dynamic/portal_type_class.py
@@ -100,60 +100,34 @@ def generatePortalTypeClass(portal_type_name):
   and computes __bases__ and __dict__ for the class that will
   be created to represent this portal type
   """
-  #LOG("ERP5Type.dynamic", 0, "Loading portal type %s..." % portal_type_name)
+  from Products.ERP5.ERP5Site import getSite
+  site = getSite()
 
+  types_tool = site.portal_types
+  portal_type = None
   mixin_list = []
   interface_list = []
   accessor_holder_list = []
 
-  from Products.ERP5.ERP5Site import getSite
-  site = getSite()
-
-  types_tool = site.portal_types
   try:
-    portal_type = getattr(types_tool, portal_type_name)
+    portal_type = types_tool[portal_type_name]
+  except KeyError:
+    # Try to figure out a coresponding document class from the document side.
+    # This can happen when calling newTempAmount for instance:
+    #  Amount has no corresponding Base Type and will never have one
+    #  But the semantic of newTempXXX requires us to create an
+    #  object using the Amount Document, so we promptly do it:
+    type_class = portal_type_name.replace(' ', '')
 
+  if portal_type is not None:
     # type_class has a compatibility getter that should return
     # something even if the field is not set (i.e. Base Type object
     # was not migrated yet)
     type_class = portal_type.getTypeClass()
+    mixin_list = portal_type.getTypeMixinList()
+    interface_list = portal_type.getTypeInterfaceList()
 
-    # But no such getter exists for Mixins and Interfaces:
-    # in reality, we can live with such a failure
-    try:
-      mixin_list = portal_type.getTypeMixinList()
-      interface_list = portal_type.getTypeInterfaceList()
-    except StandardError:
-      # log loudly the error, but it's not _critical_
-      LOG("ERP5Type.dynamic", ERROR,
-          "Could not load interfaces or Mixins for portal type %s" \
-              % portal_type_name)
-  except AttributeError:
-    # Try to figure out a coresponding document class from the document side.
-    # This can happen when calling newTempAmount for instance:
-    #  Amount has no corresponding Base Type and will never have one
-    #  But the semantic of newTempXXX requires us to create an
-    #  object using the Amount Document, so we promptly do it:
-    for name, path in document_class_registry.iteritems():
-      module_path, class_name = path.rsplit('.', 1)
-      if portal_type_name.replace(' ', '') != class_name:
-        continue
-      module = __import__(module_path, {}, {}, (module_path,))
-      try:
-        klass = getattr(module, class_name)
-        try:
-          document_portal_type = getattr(klass, 'portal_type')
-          if document_portal_type == portal_type_name:
-            type_class = name
-            break
-        except AttributeError:
-          pass
-      finally:
-        del klass
-
-  type_class_path = None
-  if type_class is not None:
-    type_class_path = document_class_registry.get(type_class)
+  type_class_path = document_class_registry.get(type_class)
   if type_class_path is None:
     raise AttributeError('Document class is not defined on Portal Type %s' \
             % portal_type_name)
-- 
2.30.9