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 instance_class not in self.plugin_defs:
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 'name' not in items:
72  rospy.logerr('class tag of %s does not have name attribute'
73  % (xml_path))
74  else:
75  name = items['name']
76  if 'type' not in items:
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 
jsk_teleop_joy.plugin_manager.PluginManager
Definition: plugin_manager.py:12
jsk_teleop_joy.plugin_manager.PluginManager.loadPluginInstances
def loadPluginInstances(self, plugins, manager)
Definition: plugin_manager.py:15
jsk_teleop_joy.plugin_manager.PluginManager.plugin_defs
plugin_defs
Definition: plugin_manager.py:53
jsk_teleop_joy.plugin_manager.PluginManager.__init__
def __init__(self, package_name)
Definition: plugin_manager.py:13
jsk_teleop_joy.plugin_manager.PluginManager.loadPlugins
def loadPlugins(self)
Definition: plugin_manager.py:49
jsk_teleop_joy.plugin_manager.PluginManager.plugins
plugins
Definition: plugin_manager.py:20
jsk_teleop_joy.plugin_manager.PluginManager.package_name
package_name
Definition: plugin_manager.py:14


jsk_teleop_joy
Author(s): Ryohei Ueda
autogenerated on Wed Jan 24 2024 04:05:49