17 import roslib; roslib.load_manifest(
'airbus_cobot_gui')
23 from airbus_cobot_gui
import airbus_cobot_gui_dir
24 from airbus_cobot_gui
import CobotGuiException
28 from xml.etree
import ElementTree
29 from roslib.packages
import get_pkg_dir
31 from rospkg.common
import MANIFEST_FILE, PACKAGE_FILE
32 from rospkg.manifest
import parse_manifest_file
45 """! The constructor."""
49 if not os.path.isfile(xml_register_dir):
50 raise CobotGuiException(
'Widgets register file "%s" in package "airbus_cobot_gui" not found'
56 """! Get package name and xml file name by widget label.
57 @param widget_label: widget label.
58 @type widget_label: string.
60 @return widget_desc: package name and xml file name.
61 @type widget_desc: tuple (string, string).
66 root = self._widget_register.getroot()
68 widget_desc = root.find(
'./widget[@label="%s"]'%widget_label)
70 if widget_desc
is None:
73 return widget_desc.attrib[
'package'], widget_desc.attrib[
'widget-xml']
75 def load(self, package_name, widget_xml):
76 """! Load Python package.
77 @param package_name: package name.
78 @type package_name: string.
80 @param widget_xml: xml file name.
81 @type widget_xml: string.
83 @return widget_instance: widget instance.
84 @type widget_instance: Widget.
89 sys.path.append(package[
'package_path'])
92 module = __builtin__.__import__(package[
'module_name'],
93 fromlist=[package[
'class_name']],
95 except NotImplementedError
as e:
97 raise CobotGuiException(
'WidgetProvider.__import__(%s,%s): raised an exception:\n%s'
98 %(package[
'module_name'], package[
'class_name'], e))
100 except Exception
as e:
102 raise CobotGuiException(
'WidgetProvider.__import__(%s, %s): raised an exception:\n%s'
103 %(package[
'module_name'],
104 package[
'class_name'],
105 traceback.format_exc()))
107 widget_ref = getattr(module, package[
'class_name'],
None)
109 if widget_ref
is None:
111 raise CobotGuiException(
'Could not find class "%s" in module "%s"'
112 %(package[
'class_name'], package[
'module_name']))
115 widget_instance = widget_ref()
116 widget_instance.install(config)
117 except Exception
as e:
118 raise CobotGuiException(
'WidgetProvider.widget_instance raised with exception %s'%e)
120 return widget_instance
123 """! Read gadegt xml file.
124 @param package_name: python pakage name.
125 @type package_name: string.
127 @param plugin_xml: plugin xml name.
128 @type plugin_xml: string.
130 @return: plugin_desc: package descrition, plugin configuration.
131 @type plugin_desc: tuple (dictonary, dictonary).
134 package = {
'package_path' :
None,
135 'module_name' :
None,
138 config = {
'label' : self.
_label,
'description' :
None,
139 'style-sheet' :
None,
'restriction':
None}
141 pkg_dir = get_pkg_dir(package_name)
143 path =
'/'.join([pkg_dir,widget_xml])
145 if not os.path.isfile(path):
147 raise CobotGuiException(
'WidgetProvider._parse_widget_xml() widget file "%s" in package "%s" not found'
148 %(widget_xml, package_name))
151 tree = ElementTree.parse(path)
152 except Exception
as e:
153 raise CobotGuiException(
'WidgetProvider._parse_widget_xml() raised with exception "%s"'%e)
155 path = [pkg_dir,tree.getroot().attrib[
'path']]
157 package[
'package_path'] =
'/'.join(path)
159 widget_import = tree.getroot().find(
'./class').attrib[
'import']
162 module_name, class_name = widget_import.rsplit(
'.', 1)
163 package[
'module_name'] = module_name
164 package[
'class_name'] = class_name
166 config[
'description'] = tree.getroot().find(
'./description').text
168 config_node = tree.getroot().find(
'./init')
170 for child
in config_node:
171 if child.tag ==
'style-sheet':
172 if child.text !=
'none':
173 config[
'style-sheet'] = child.text.replace(
'${prefix}',pkg_dir)
174 elif child.tag ==
'access-rights':
175 config[
'restriction'] = child.text
179 return package, config