plugin_manager.py
Go to the documentation of this file.
1 import os
2 import sys, traceback
3 import rospy
4 
5 from rospkg import RosPack
6 from rospkg.common import MANIFEST_FILE, PACKAGE_FILE
7 from subprocess import Popen, PIPE
8 
9 from xml.etree import ElementTree
10 import __builtin__
11 
12 class PluginManager():
13  def __init__(self, package_name):
14  self.package_name = package_name
15  def loadPluginInstances(self, plugins, manager):
16  """
17  plugins := {name: spec, name: spec, ...}
18  spec := {class: class, args: args}
19  """
20  self.plugins = []
21 
22  #for (instance_name, instance_spec) in plugins.items():
23  plugin_keys = plugins.keys()
24  plugin_keys.sort()
25  for instance_name in plugin_keys:
26  instance_spec = plugins[instance_name]
27  instance_class = instance_spec["class"]
28  instance_args = instance_spec["args"]
29  if not self.plugin_defs.has_key(instance_class):
30  rospy.logerr('cannot find %s in plugins for %s' % (instance_class,
31  self.package_name))
32  else:
33  try:
34  module_path = self.plugin_defs[instance_class]
35  module_name, class_from_class_type = module_path.rsplit('.', 1)
36  module = __builtin__.__import__(module_name,
37  fromlist=[class_from_class_type],
38  level=0)
39  class_ref = getattr(module, class_from_class_type, None)
40  if class_ref is None:
41  rospy.logfatal('cannot find %s' % (class_from_class_type))
42  else:
43  self.plugins.append(class_ref(instance_name, instance_args))
44  self.plugins[-1].registerManager(manager)
45  except:
46  rospy.logerr('failed to load %s' % (instance_class))
47  traceback.print_exc(file=sys.stdout)
48  return self.plugins
49  def loadPlugins(self):
50  """
51  load plugins of jsk_teleop_joy defined in the packages.
52  """
53  self.plugin_defs = {}
54  p = Popen(["rospack", 'plugins', '--attrib', 'plugin', self.package_name],
55  stdin=PIPE,
56  stdout=PIPE,
57  stderr=PIPE)
58  p.wait()
59  output = p.stdout.readlines()
60  for output_line in output:
61  package_name = output_line.split(' ')[0]
62  xml_path = output_line.split(' ')[1].strip()
63  if os.path.isfile(xml_path):
64  try:
65  root = ElementTree.parse(xml_path)
66  except:
67  rospy.logerr("failed to open %s" % (xml_path))
68  for library_elem in root.getiterator('library'):
69  for class_elem in library_elem.getiterator('class'):
70  items = class_elem.attrib
71  if not items.has_key('name'):
72  rospy.logerr('class tag of %s does not have name attribute'
73  % (xml_path))
74  else:
75  name = items['name']
76  if not items.has_key('type'):
77  rospy.logerr('%s does not have type attribute' % (name))
78  else:
79  plugin_name = items['type']
80  self.plugin_defs[name] = plugin_name
81 
82 
def loadPluginInstances(self, plugins, manager)


jsk_teleop_joy
Author(s): Ryohei Ueda
autogenerated on Fri May 14 2021 02:52:11