Newer
Older
import logging
import traceback
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, 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__)
"""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 reimplement the methods as from example::
class MyPlugin(BaseExtensionPlugin):
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),
#: A list of available Multi Criteria Optimizers this plugin exports.
contributes_to=ExtensionPointID.MCO_FACTORIES
#: A list of the available Data Sources this plugin exports.
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)
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)
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
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_)]