rospkg_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 os
00032 
00033 from python_qt_binding.QtCore import qDebug, qWarning
00034 
00035 from rospkg.common import MANIFEST_FILE, PACKAGE_FILE
00036 from rospkg.manifest import parse_manifest_file, InvalidManifest
00037 
00038 from .ros_plugin_provider import RosPluginProvider
00039 
00040 
00041 class RospkgPluginProvider(RosPluginProvider):
00042 
00043     rospack = None
00044 
00045     """`RosPluginProvider` using rospkg."""
00046 
00047     def __init__(self, export_tag, base_class_type):
00048         super(RospkgPluginProvider, self).__init__(export_tag, base_class_type)
00049         self.setObjectName('RospkgPluginProvider')
00050 
00051         if RospkgPluginProvider.rospack is None:
00052             from rospkg import RosPack
00053             RospkgPluginProvider.rospack = RosPack()
00054 
00055     def _find_plugins(self, export_tag, discovery_data):
00056         crawl = True
00057         if discovery_data:
00058             data = discovery_data.get_settings('rqt_gui.RospkgPluginProvider')
00059             export_data = data.get_settings(export_tag)
00060             crawl = export_tag not in data.child_groups()
00061 
00062         plugins = []
00063         if crawl:
00064             qDebug("RospkgPluginProvider._find_plugins() crawling for plugins of type '%s'" % export_tag)
00065             r = RospkgPluginProvider.rospack
00066             for package_name in r.list():
00067                 package_path = r.get_path(package_name)
00068                 manifest_file_path = os.path.join(package_path, MANIFEST_FILE)
00069                 if os.path.isfile(manifest_file_path):
00070                     try:
00071                         manifest = parse_manifest_file(package_path, MANIFEST_FILE)
00072                     except InvalidManifest as e:
00073                         qWarning('Could not parse manifest "%s":\n%s' % (manifest_file_path, e))
00074                         continue
00075                     exports = manifest.get_export(export_tag, 'plugin')
00076                     for export in exports:
00077                         plugins.append([package_name, str(export)])
00078                     continue
00079 
00080                 package_file_path = os.path.join(package_path, PACKAGE_FILE)
00081                 if os.path.isfile(package_file_path):
00082                     # only try to import catkin if a PACKAGE_FILE is found
00083                     try:
00084                         from catkin_pkg.package import parse_package, InvalidPackage
00085                     except ImportError as e:
00086                         qWarning('Package "%s" has a package file, but import of parser failed:\n%s' % (package_path, e))
00087                         continue
00088                     try:
00089                         package = parse_package(package_file_path)
00090                     except InvalidPackage as e:
00091                         qWarning('Could not parse package file "%s":\n%s' % (package_file_path, e))
00092                         continue
00093                     for export in package.exports:
00094                         if export.tagname != export_tag or 'plugin' not in export.attributes:
00095                             continue
00096                         plugin_xml_path = export.attributes['plugin']
00097                         plugin_xml_path = plugin_xml_path.replace('${prefix}', package_path)
00098                         plugins.append([package_name, plugin_xml_path])
00099                     continue
00100 
00101             # write crawling information to cache
00102             if discovery_data:
00103                 plugins_by_package = {}
00104                 for (package_name, export) in plugins:
00105                     if package_name not in plugins_by_package:
00106                         plugins_by_package[package_name] = []
00107                     plugins_by_package[package_name].append(export)
00108                 for package_name, exports in plugins_by_package.items():
00109                     export_data.set_value(package_name, os.pathsep.join([str(e) for e in exports]))
00110 
00111         else:
00112             # use cached information
00113             for package_name in export_data.all_keys():
00114                 exports = export_data.value(package_name)
00115                 if exports:
00116                     for export in exports.split(os.pathsep):
00117                         plugins.append([package_name, export])
00118 
00119         return plugins


rqt_gui
Author(s): Dirk Thomas
autogenerated on Thu Aug 27 2015 14:56:52