ros_pluginlib_plugin_provider.py
Go to the documentation of this file.
00001 # Copyright (c) 2011, Dirk Thomas, 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 from qt_gui.errors import PluginLoadError
00032 from qt_gui.plugin_descriptor import PluginDescriptor
00033 from qt_gui.plugin_provider import PluginProvider
00034 
00035 from .cpp_binding_helper import qt_gui_cpp
00036 
00037 
00038 class RosPluginlibPluginProvider(PluginProvider):
00039 
00040     def __init__(self, plugin_provider):
00041         super(RosPluginlibPluginProvider, self).__init__()
00042         self._plugin_provider = plugin_provider
00043 
00044     def discover(self, discovery_data):
00045         discovered_plugins = self._unfold(self._plugin_provider.discover(discovery_data))
00046         plugin_descriptors = []
00047         for plugin in discovered_plugins.values():
00048             plugin_descriptor = PluginDescriptor(plugin['plugin_id'], plugin['attributes'])
00049 
00050             action_attributes = plugin['action']
00051             plugin_descriptor.set_action_attributes(action_attributes['label'], action_attributes.get('statustip', None), action_attributes.get('icon', None), action_attributes.get('icontype', None))
00052 
00053             groups = plugin.get('groups', {})
00054             for group in groups.values():
00055                 plugin_descriptor.add_group_attributes(group['label'], group['statustip'], group['icon'], group['icontype'])
00056 
00057             plugin_descriptors.append(plugin_descriptor)
00058         return plugin_descriptors
00059 
00060     def load(self, plugin_id, plugin_context):
00061         if qt_gui_cpp is None:
00062             return None
00063         cpp_plugin_context = None
00064         if plugin_context is not None:
00065             cpp_plugin_context = qt_gui_cpp.PluginContext(plugin_context._handler, plugin_context.serial_number(), plugin_context.argv())
00066         bridge = qt_gui_cpp.PluginBridge()  # @UndefinedVariable
00067         loaded = bridge.load_plugin(self._plugin_provider, plugin_id, cpp_plugin_context)
00068         if not loaded:
00069             raise PluginLoadError('RosPluginlibPluginProvider.load() could not load plugin "%s"' % plugin_id)
00070         return bridge
00071 
00072     def unload(self, bridge):
00073         return bridge.unload_plugin()
00074 
00075     def _unfold(self, flat_dict):
00076         dictionary = {}
00077         for key, value in flat_dict.items():
00078             keys = str(key).split('.')
00079             current_level = dictionary
00080             for i in keys[:-1]:
00081                 if i not in current_level:
00082                     current_level[i] = {}
00083                 current_level = current_level[i]
00084             current_level[keys[-1]] = str(value) if value != '' else None
00085         return dictionary
00086 
00087     def shutdown(self):
00088         self._plugin_provider.shutdown()


qt_gui_cpp
Author(s): Dirk Thomas
autogenerated on Mon Oct 6 2014 03:57:56