composite_plugin_provider.py
Go to the documentation of this file.
00001 # Copyright (c) 2011, Dirk Thomas, Dorian Scholz, TU Darmstadt
00002 # All rights reserved.
00003 #
00004 # Redistribution and use in source and binary forms, with or without
00005 # modification, are permitted provided that the following conditions
00006 # are met:
00007 #
00008 #   * Redistributions of source code must retain the above copyright
00009 #     notice, this list of conditions and the following disclaimer.
00010 #   * Redistributions in binary form must reproduce the above
00011 #     copyright notice, this list of conditions and the following
00012 #     disclaimer in the documentation and/or other materials provided
00013 #     with the distribution.
00014 #   * Neither the name of the TU Darmstadt nor the names of its
00015 #     contributors may be used to endorse or promote products derived
00016 #     from this software without specific prior written permission.
00017 #
00018 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
00019 # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
00020 # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
00021 # FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
00022 # COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
00023 # INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
00024 # BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
00025 # LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
00026 # CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
00027 # LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
00028 # ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
00029 # POSSIBILITY OF SUCH DAMAGE.
00030 
00031 import traceback
00032 
00033 from python_qt_binding.QtCore import qCritical
00034 
00035 from .plugin_provider import PluginProvider
00036 
00037 
00038 class CompositePluginProvider(PluginProvider):
00039 
00040     """Composite of multiple `PluginProvider`s."""
00041 
00042     def __init__(self, plugin_providers=None):
00043         super(CompositePluginProvider, self).__init__()
00044         self.setObjectName('CompositePluginProvider')
00045 
00046         self._plugin_providers = plugin_providers or []
00047         self._discovered_plugins = {}
00048         self._running_plugins = {}
00049 
00050     def set_plugin_providers(self, plugin_providers):
00051         self._plugin_providers = plugin_providers
00052 
00053     def discover(self, discovery_data):
00054         # discover plugins from all providers
00055         discovered_plugins = []
00056         for plugin_provider in self._plugin_providers:
00057             try:
00058                 plugin_descriptors = plugin_provider.discover(discovery_data)
00059             except Exception:
00060                 qCritical('CompositePluginProvider.discover() could not discover plugins from provider "%s":\n%s' % (type(plugin_provider), traceback.format_exc()))
00061             else:
00062                 self._discovered_plugins[plugin_provider] = plugin_descriptors
00063                 discovered_plugins += plugin_descriptors
00064         return discovered_plugins
00065 
00066     def load(self, plugin_id, plugin_context):
00067         # dispatch load to appropriate provider
00068         for plugin_provider, plugin_descriptors in self._discovered_plugins.items():
00069             for plugin_descriptor in plugin_descriptors:
00070                 if plugin_descriptor.plugin_id() == plugin_id:
00071                     instance = plugin_provider.load(plugin_id, plugin_context)
00072                     self._running_plugins[instance] = plugin_provider
00073                     return instance
00074         raise UserWarning('plugin_id "%s" not found' % plugin_id)
00075 
00076     def unload(self, plugin_instance):
00077         # dispatch unload to appropriate provider
00078         if plugin_instance in self._running_plugins:
00079             self._running_plugins[plugin_instance].unload(plugin_instance)
00080             self._running_plugins.pop(plugin_instance)
00081             return
00082         raise UserWarning('plugin_instance not found')
00083 
00084     def shutdown(self):
00085         for plugin_provider in self._plugin_providers:
00086             plugin_provider.shutdown()


qt_gui
Author(s): Dirk Thomas
autogenerated on Fri Aug 28 2015 12:15:40