Skip to content
Snippets Groups Projects
Unverified Commit cfd9b288 authored by Stefano Borini's avatar Stefano Borini Committed by GitHub
Browse files

Merge pull request #129 from force-h2020/safer-plugin-import

Safer plugin import
parents e9a7fc37 a6d1aa67
No related branches found
No related tags found
No related merge requests found
from .base_extension_plugin import BaseExtensionPlugin # noqa
from .ids import factory_id, plugin_id, mco_parameter_id # noqa
from .ids import factory_id, mco_parameter_id # noqa
from .core.data_value import DataValue # noqa
from .core.workflow import Workflow # noqa
......
import logging
import traceback
from envisage.plugin import Plugin
from traits.trait_types import List
from traits.api import List, Unicode, Bool, Type, Either
from force_bdss.data_sources.base_data_source_factory import \
BaseDataSourceFactory
from force_bdss.mco.base_mco_factory import BaseMCOFactory
from force_bdss.notification_listeners.base_notification_listener_factory \
import \
BaseNotificationListenerFactory
from force_bdss.ui_hooks.base_ui_hooks_factory import BaseUIHooksFactory
from .notification_listeners.i_notification_listener_factory import \
INotificationListenerFactory
from .ids import ExtensionPointID
from .ids import ExtensionPointID, plugin_id
from .data_sources.i_data_source_factory import IDataSourceFactory
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
provided by external contributors.
It provides a set of slots to be populated that end up contributing
to the application extension points. To use the class, simply inherit it
in your plugin, and then define the trait default initializer for the
specific trait you want to populate. For example::
in your plugin, and reimplement the methods as from example::
class MyPlugin(BaseExtensionPlugin):
def _data_source_factories_default(self):
return [MyDataSourceFactory1(),
MyDataSourceFactory2()]
def get_producer(self):
return "enthought"
def get_identifier(self):
return "myplugin"
def get_factory_classes(self):
return [
MyDataSourceFactory1,
MyDataSourceFactory2,
MyMCOFactory
]
"""
#: Reports if the plugin loaded its factories successfully or not.
broken = Bool(False)
#: The error that have been generated by the instantiations.
error = Unicode()
#: A list of all the factory classes to export.
factory_classes = List(
Either(Type(BaseDataSourceFactory),
Type(BaseMCOFactory),
Type(BaseNotificationListenerFactory),
Type(BaseUIHooksFactory))
)
#: A list of available Multi Criteria Optimizers this plugin exports.
mco_factories = List(
......@@ -36,12 +70,74 @@ class BaseExtensionPlugin(Plugin):
contributes_to=ExtensionPointID.DATA_SOURCE_FACTORIES
)
#: A list of the available notification listeners this plugin exports
notification_listener_factories = List(
INotificationListenerFactory,
contributes_to=ExtensionPointID.NOTIFICATION_LISTENER_FACTORIES
)
#: A list of the available ui hooks this plugin exports
ui_hooks_factories = List(
IUIHooksFactory,
contributes_to=ExtensionPointID.UI_HOOKS_FACTORIES
)
def __init__(self, *args, **kwargs):
super(BaseExtensionPlugin, self).__init__(*args, **kwargs)
try:
self.id = plugin_id(self.get_producer(), self.get_identifier())
self.factory_classes = self.get_factory_classes()
self.mco_factories[:] = [
cls(self)
for cls in self._factory_by_type(BaseMCOFactory)]
self.data_source_factories[:] = [
cls(self)
for cls in self._factory_by_type(BaseDataSourceFactory)]
self.notification_listener_factories[:] = [
cls(self)
for cls in self._factory_by_type(
BaseNotificationListenerFactory)
]
self.ui_hooks_factories[:] = [
cls(self)
for cls in self._factory_by_type(
BaseUIHooksFactory)
]
except Exception as e:
self.error = traceback.format_exc()
logger.exception(e)
self.broken = True
self.mco_factories[:] = []
self.data_source_factories[:] = []
self.notification_listener_factories[:] = []
self.ui_hooks_factories[:] = []
def get_producer(self):
"""Must be reimplemented to return a string with the name of the
company producing this plugin. Examples are "enthought", "itwm" etc.
"""
raise NotImplementedError(
"get_producer was not implemented in plugin {}".format(
self.__class__))
def get_identifier(self):
"""Must return a string with the name of the plugin the producer
is releasing. The name must be unique and is responsibility of
the producer to guarantee this name is not conflicting with
another already existing plugin
"""
raise NotImplementedError(
"get_identifier was not implemented in plugin {}".format(
self.__class__))
def get_factory_classes(self):
"""Must return a list of factory classes that this plugin exports.
"""
raise NotImplementedError(
"get_factory_classes was not implemented in plugin {}".format(
self.__class__))
def _factory_by_type(self, type_):
return [cls for cls in self.factory_classes if issubclass(cls, type_)]
from force_bdss.base_extension_plugin import BaseExtensionPlugin
from force_bdss.tests.probe_classes.data_source import ProbeDataSourceFactory
from force_bdss.tests.probe_classes.mco import ProbeMCOFactory
from force_bdss.tests.probe_classes.notification_listener import \
ProbeNotificationListenerFactory
from force_bdss.tests.probe_classes.ui_hooks import ProbeUIHooksFactory
class ProbeExtensionPlugin(BaseExtensionPlugin):
def get_producer(self):
return "enthought"
def get_identifier(self):
return "test"
def get_factory_classes(self):
return [
ProbeDataSourceFactory,
ProbeMCOFactory,
ProbeNotificationListenerFactory,
ProbeUIHooksFactory,
]
import unittest
from force_bdss.tests.probe_classes.probe_extension_plugin import \
ProbeExtensionPlugin
class TestBaseExtensionPlugin(unittest.TestCase):
def test_basic_init(self):
plugin = ProbeExtensionPlugin()
self.assertEqual(len(plugin.data_source_factories), 1)
self.assertEqual(len(plugin.notification_listener_factories), 1)
self.assertEqual(len(plugin.mco_factories), 1)
self.assertEqual(len(plugin.ui_hooks_factories), 1)
self.assertFalse(plugin.broken)
self.assertEqual(plugin.error, "")
......@@ -3,11 +3,15 @@ import warnings
from force_bdss.base_extension_plugin import (
BaseExtensionPlugin)
from force_bdss.data_sources.base_data_source_factory import \
BaseDataSourceFactory
from force_bdss.ids import factory_id, mco_parameter_id
from force_bdss.mco.base_mco_factory import BaseMCOFactory
from force_bdss.mco.parameters.base_mco_parameter_factory import \
BaseMCOParameterFactory
from force_bdss.notification_listeners.i_notification_listener_factory import \
INotificationListenerFactory
from force_bdss.notification_listeners.base_notification_listener_factory \
import \
BaseNotificationListenerFactory
try:
import mock
......@@ -17,8 +21,6 @@ except ImportError:
from envisage.application import Application
from force_bdss.factory_registry_plugin import FactoryRegistryPlugin
from force_bdss.data_sources.i_data_source_factory import IDataSourceFactory
from force_bdss.mco.i_mco_factory import IMCOFactory
class TestFactoryRegistry(unittest.TestCase):
......@@ -33,29 +35,44 @@ class TestFactoryRegistry(unittest.TestCase):
self.assertEqual(self.plugin.data_source_factories, [])
class MySuperPlugin(BaseExtensionPlugin):
def _mco_factories_default(self):
class MCOFactory(BaseMCOFactory):
id = factory_id("enthought", "mco1")
def parameter_factories(self):
return [
mock.Mock(
spec=IMCOFactory,
id=factory_id("enthought", "mco1"),
parameter_factories=mock.Mock(return_value=[
mock.Mock(
spec=BaseMCOParameterFactory,
id=mco_parameter_id("enthought", "mco1", "ranged")
)
]),
)]
def _data_source_factories_default(self):
return [mock.Mock(spec=IDataSourceFactory,
id=factory_id("enthought", "ds1")),
mock.Mock(spec=IDataSourceFactory,
id=factory_id("enthought", "ds2"))]
def _notification_listener_factories_default(self):
return [mock.Mock(spec=INotificationListenerFactory,
id=factory_id("enthought", "nl1"))]
spec=BaseMCOParameterFactory,
id=mco_parameter_id("enthought", "mco1", "ranged")
)
]
class DataSourceFactory1(BaseDataSourceFactory):
id = factory_id("enthought", "ds1")
class DataSourceFactory2(BaseDataSourceFactory):
id = factory_id("enthought", "ds2")
class NotificationListenerFactory(BaseNotificationListenerFactory):
id = factory_id("enthought", "nl1")
class MySuperPlugin(BaseExtensionPlugin):
def get_producer(self):
return "enthought"
def get_identifier(self):
return "test"
def get_factory_classes(self):
return [
MCOFactory,
DataSourceFactory1,
DataSourceFactory2,
NotificationListenerFactory
]
class TestFactoryRegistryWithContent(unittest.TestCase):
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment