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, manager):
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 self.plugins[-1].registerManager(manager)
00045 except:
00046 rospy.logerr('failed to load %s' % (instance_class))
00047 traceback.print_exc(file=sys.stdout)
00048 return self.plugins
00049 def loadPlugins(self):
00050 """
00051 load plugins of jsk_teleop_joy defined in the packages.
00052 """
00053 self.plugin_defs = {}
00054 p = Popen(["rospack", 'plugins', '--attrib', 'plugin', self.package_name],
00055 stdin=PIPE,
00056 stdout=PIPE,
00057 stderr=PIPE)
00058 p.wait()
00059 output = p.stdout.readlines()
00060 for output_line in output:
00061 package_name = output_line.split(' ')[0]
00062 xml_path = output_line.split(' ')[1].strip()
00063 if os.path.isfile(xml_path):
00064 try:
00065 root = ElementTree.parse(xml_path)
00066 except:
00067 rospy.logerr("failed to open %s" % (xml_path))
00068 for library_elem in root.getiterator('library'):
00069 for class_elem in library_elem.getiterator('class'):
00070 items = class_elem.attrib
00071 if not items.has_key('name'):
00072 rospy.logerr('class tag of %s does not have name attribute'
00073 % (xml_path))
00074 else:
00075 name = items['name']
00076 if not items.has_key('type'):
00077 rospy.logerr('%s does not have type attribute' % (name))
00078 else:
00079 plugin_name = items['type']
00080 self.plugin_defs[name] = plugin_name
00081
00082