Go to the documentation of this file.00001 import os
00002 import sys, traceback
00003 import rospy
00004
00005 from rospkg import RosPack
00006 from rospkg.common import MANIFEST_FILE, PACKAGE_FILE
00007 from subprocess import Popen, PIPE
00008
00009 from xml.etree import ElementTree
00010 import __builtin__
00011
00012 class PluginManager():
00013 def __init__(self, package_name):
00014 self.package_name = package_name
00015 def loadPluginInstances(self, plugins):
00016 """
00017 plugins := {name: spec, name: spec, ...}
00018 spec := {class: class, args: args}
00019 """
00020 self.plugins = []
00021
00022
00023 plugin_keys = plugins.keys()
00024 plugin_keys.sort()
00025 for instance_name in plugin_keys:
00026 instance_spec = plugins[instance_name]
00027 instance_class = instance_spec["class"]
00028 instance_args = instance_spec["args"]
00029 if not self.plugin_defs.has_key(instance_class):
00030 rospy.logerr('cannot find %s in plugins for %s' % (instance_class,
00031 self.package_name))
00032 else:
00033 try:
00034 module_path = self.plugin_defs[instance_class]
00035 module_name, class_from_class_type = module_path.rsplit('.', 1)
00036 module = __builtin__.__import__(module_name,
00037 fromlist=[class_from_class_type],
00038 level=0)
00039 class_ref = getattr(module, class_from_class_type, None)
00040 if class_ref is None:
00041 rospy.logfatal('cannot find %s' % (class_from_class_type))
00042 else:
00043 self.plugins.append(class_ref(instance_name, instance_args))
00044 except:
00045 rospy.logerr('failed to load %s' % (instance_class))
00046 traceback.print_exc(file=sys.stdout)
00047 return self.plugins
00048 def loadPlugins(self):
00049 """
00050 load plugins of jsk_teleop_joy defined in the packages.
00051 """
00052 self.plugin_defs = {}
00053 p = Popen(["rospack", 'plugins', '--attrib', 'plugin', self.package_name],
00054 stdin=PIPE,
00055 stdout=PIPE,
00056 stderr=PIPE)
00057 p.wait()
00058 output = p.stdout.readlines()
00059 for output_line in output:
00060 package_name = output_line.split(' ')[0]
00061 xml_path = output_line.split(' ')[1].strip()
00062 if os.path.isfile(xml_path):
00063 try:
00064 root = ElementTree.parse(xml_path)
00065 except:
00066 rospy.logerr("failed to open %s" % (xml_path))
00067 for library_elem in root.getiterator('library'):
00068 for class_elem in library_elem.getiterator('class'):
00069 items = class_elem.attrib
00070 if not items.has_key('name'):
00071 rospy.logerr('class tag of %s does not have name attribute'
00072 % (xml_path))
00073 else:
00074 name = items['name']
00075 if not items.has_key('type'):
00076 rospy.logerr('%s does not have type attribute' % (name))
00077 else:
00078 plugin_name = items['type']
00079 self.plugin_defs[name] = plugin_name
00080
00081