From 0ced8db3831f21f978a35b7491863ba62ef3cdf2 Mon Sep 17 00:00:00 2001
From: Stefano Borini <sborini@enthought.com>
Date: Thu, 17 May 2018 11:02:13 +0100
Subject: [PATCH] Handle id differently to prevent issuing of warn due to bug
 in traits

---
 force_bdss/base_extension_plugin.py           | 48 +++++++++++++++----
 .../tests/test_base_extension_plugin.py       |  3 +-
 2 files changed, 41 insertions(+), 10 deletions(-)

diff --git a/force_bdss/base_extension_plugin.py b/force_bdss/base_extension_plugin.py
index 12017a4..cb4a9d6 100644
--- a/force_bdss/base_extension_plugin.py
+++ b/force_bdss/base_extension_plugin.py
@@ -2,7 +2,7 @@ import logging
 import traceback
 
 from envisage.plugin import Plugin
-from traits.api import List, Unicode, Bool, Type, Either
+from traits.api import List, Unicode, Bool, Type, Either, Instance
 
 from force_bdss.data_sources.base_data_source_factory import \
     BaseDataSourceFactory
@@ -19,8 +19,6 @@ from .mco.i_mco_factory import IMCOFactory
 from .ui_hooks.i_ui_hooks_factory import IUIHooksFactory
 
 
-logger = logging.getLogger(__name__)
-
 
 class BaseExtensionPlugin(Plugin):
     """Base class for extension plugins, that is, plugins that are
@@ -48,7 +46,10 @@ class BaseExtensionPlugin(Plugin):
     broken = Bool(False)
 
     #: The error that have been generated by the instantiations.
-    error = Unicode()
+    error_msg = Unicode()
+
+    #: The error that have been generated by the instantiations.
+    error_tb = Unicode()
 
     #: A list of all the factory classes to export.
     factory_classes = List(
@@ -82,11 +83,31 @@ class BaseExtensionPlugin(Plugin):
         contributes_to=ExtensionPointID.UI_HOOKS_FACTORIES
     )
 
+    #: The logger.
+    _logger = Instance(logging.Logger)
+
     def __init__(self, *args, **kwargs):
+        broken = False
+        error = ""
+
+        if "id" not in kwargs:
+            try:
+                id_ = plugin_id(self.get_producer(), self.get_identifier())
+            except Exception as e:
+                self._logger.exception(e)
+                error = traceback.format_exc()
+                broken = True
+            else:
+                kwargs["id"] = id_
+
         super(BaseExtensionPlugin, self).__init__(*args, **kwargs)
 
+        if broken:
+            self.broken = True
+            self.error = error
+            return
+
         try:
-            self.id = plugin_id(self.get_producer(), self.get_identifier())
             self.factory_classes = self.get_factory_classes()
             self.mco_factories[:] = [
                 cls(self)
@@ -101,12 +122,12 @@ class BaseExtensionPlugin(Plugin):
             ]
             self.ui_hooks_factories[:] = [
                 cls(self)
-                for cls in self._factory_by_type(
-                    BaseUIHooksFactory)
+                for cls in self._factory_by_type(BaseUIHooksFactory)
             ]
         except Exception as e:
-            self.error = traceback.format_exc()
-            logger.exception(e)
+            self._logger.exception(e)
+            self.error_msg = str(e)
+            self.error_tb = traceback.format_exc()
             self.broken = True
             self.mco_factories[:] = []
             self.data_source_factories[:] = []
@@ -140,4 +161,13 @@ class BaseExtensionPlugin(Plugin):
                 self.__class__))
 
     def _factory_by_type(self, type_):
+        """Returns all the factories of the given type"""
         return [cls for cls in self.factory_classes if issubclass(cls, type_)]
+
+    def _id_default(self):
+        """Override for base method that raises a warning we don't want to
+        show"""
+        return '%s.%s' % (type(self).__module__, type(self).__name__)
+
+    def __logger_default(self):
+        return logging.getLogger(self.__class__.__name__)
diff --git a/force_bdss/tests/test_base_extension_plugin.py b/force_bdss/tests/test_base_extension_plugin.py
index 7e078f6..391d8fa 100644
--- a/force_bdss/tests/test_base_extension_plugin.py
+++ b/force_bdss/tests/test_base_extension_plugin.py
@@ -12,4 +12,5 @@ class TestBaseExtensionPlugin(unittest.TestCase):
         self.assertEqual(len(plugin.mco_factories), 1)
         self.assertEqual(len(plugin.ui_hooks_factories), 1)
         self.assertFalse(plugin.broken)
-        self.assertEqual(plugin.error, "")
+        self.assertEqual(plugin.error_msg, "")
+        self.assertEqual(plugin.error_tb, "")
-- 
GitLab