composite_plugin_provider.py
Go to the documentation of this file.
1 # Copyright (c) 2011, Dirk Thomas, Dorian Scholz, TU Darmstadt
2 # All rights reserved.
3 #
4 # Redistribution and use in source and binary forms, with or without
5 # modification, are permitted provided that the following conditions
6 # are met:
7 #
8 # * Redistributions of source code must retain the above copyright
9 # notice, this list of conditions and the following disclaimer.
10 # * Redistributions in binary form must reproduce the above
11 # copyright notice, this list of conditions and the following
12 # disclaimer in the documentation and/or other materials provided
13 # with the distribution.
14 # * Neither the name of the TU Darmstadt nor the names of its
15 # contributors may be used to endorse or promote products derived
16 # from this software without specific prior written permission.
17 #
18 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20 # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
21 # FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
22 # COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
23 # INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
24 # BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
25 # LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
26 # CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 # LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
28 # ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 # POSSIBILITY OF SUCH DAMAGE.
30 
31 import traceback
32 
33 from python_qt_binding.QtCore import qCritical
34 
35 from qt_gui.plugin_provider import PluginProvider
36 
37 
39  """Composite of multiple `PluginProvider`s."""
40 
41  def __init__(self, plugin_providers=None):
42  super(CompositePluginProvider, self).__init__()
43  self.setObjectName('CompositePluginProvider')
44 
45  self._plugin_providers = plugin_providers or []
47  self._running_plugins = {}
48 
49  def set_plugin_providers(self, plugin_providers):
50  self._plugin_providers = plugin_providers
51 
52  def discover(self, discovery_data):
53  # discover plugins from all providers
54  discovered_plugins = []
55  for plugin_provider in self._plugin_providers:
56  try:
57  plugin_descriptors = plugin_provider.discover(discovery_data)
58  except Exception:
59  qCritical(
60  'CompositePluginProvider.discover() could not discover plugins from '
61  'provider "%s":\n%s' % (type(plugin_provider), traceback.format_exc()))
62  else:
63  self._discovered_plugins[plugin_provider] = plugin_descriptors
64  discovered_plugins += plugin_descriptors
65  return discovered_plugins
66 
67  def load(self, plugin_id, plugin_context):
68  # dispatch load to appropriate provider
69  for plugin_provider, plugin_descriptors in self._discovered_plugins.items():
70  for plugin_descriptor in plugin_descriptors:
71  if plugin_descriptor.plugin_id() == plugin_id:
72  instance = plugin_provider.load(plugin_id, plugin_context)
73  self._running_plugins[instance] = plugin_provider
74  return instance
75  raise UserWarning('plugin_id "%s" not found' % plugin_id)
76 
77  def unload(self, plugin_instance):
78  # dispatch unload to appropriate provider
79  if plugin_instance in self._running_plugins:
80  self._running_plugins[plugin_instance].unload(plugin_instance)
81  self._running_plugins.pop(plugin_instance)
82  return
83  raise UserWarning('plugin_instance not found')
84 
85  def shutdown(self):
86  for plugin_provider in self._plugin_providers:
87  plugin_provider.shutdown()


qt_gui
Author(s): Dirk Thomas
autogenerated on Fri Jun 24 2022 02:42:37