widget_provider.py
Go to the documentation of this file.
1 #!/usr/bin/env python
2 ################################################################################
3 #
4 # Copyright Airbus Group SAS 2015
5 # All rigths reserved.
6 #
7 # File Name : widget_provider.py
8 # Authors : Martin Matignon
9 #
10 # If you find any bug or if you have any question please contact
11 # Adolfo Suarez Roos <adolfo.suarez@airbus.com>
12 # Martin Matignon <martin.matignon.external@airbus.com>
13 #
14 #
15 ################################################################################
16 
17 import roslib; roslib.load_manifest('airbus_cobot_gui')
18 import rospy
19 import uuid
20 import os
21 import sys
22 
23 from airbus_cobot_gui import airbus_cobot_gui_dir
24 from airbus_cobot_gui import CobotGuiException
25 
26 import __builtin__
27 import traceback
28 from xml.etree import ElementTree
29 from roslib.packages import get_pkg_dir
30 
31 from rospkg.common import MANIFEST_FILE, PACKAGE_FILE
32 from rospkg.manifest import parse_manifest_file
33 
34 ## @package: widget_provider
35 ##
36 ## @version 1.1
37 ## @author Matignon Martin
38 ## @date Last modified 18/08/2014
39 
40 ## @class WidgetProvider
41 ## @brief Class for load Python widget package.
43 
44  def __init__(self, xml_register_dir):
45  """! The constructor."""
46 
47  self._label = ""
48 
49  if not os.path.isfile(xml_register_dir):
50  raise CobotGuiException('Widgets register file "%s" in package "airbus_cobot_gui" not found'
51  %(xml_register_dir))
52 
53  self._widget_register = ElementTree.parse(xml_register_dir)
54 
55  def get_widget_registered(self, widget_label):
56  """! Get package name and xml file name by widget label.
57  @param widget_label: widget label.
58  @type widget_label: string.
59 
60  @return widget_desc: package name and xml file name.
61  @type widget_desc: tuple (string, string).
62  """
63 
64  self._label = widget_label
65 
66  root = self._widget_register.getroot()
67 
68  widget_desc = root.find('./widget[@label="%s"]'%widget_label)
69 
70  if widget_desc is None:
71  return None, None
72  else:
73  return widget_desc.attrib['package'], widget_desc.attrib['widget-xml']
74 
75  def load(self, package_name, widget_xml):
76  """! Load Python package.
77  @param package_name: package name.
78  @type package_name: string.
79 
80  @param widget_xml: xml file name.
81  @type widget_xml: string.
82 
83  @return widget_instance: widget instance.
84  @type widget_instance: Widget.
85  """
86 
87  package, config = self._parse_widget_xml(package_name, widget_xml)
88 
89  sys.path.append(package['package_path'])
90 
91  try:
92  module = __builtin__.__import__(package['module_name'],
93  fromlist=[package['class_name']],
94  level=0)
95  except NotImplementedError as e:
96 
97  raise CobotGuiException('WidgetProvider.__import__(%s,%s): raised an exception:\n%s'
98  %(package['module_name'], package['class_name'], e))
99 
100  except Exception as e:
101 
102  raise CobotGuiException('WidgetProvider.__import__(%s, %s): raised an exception:\n%s'
103  %(package['module_name'],
104  package['class_name'],
105  traceback.format_exc()))
106 
107  widget_ref = getattr(module, package['class_name'], None)
108 
109  if widget_ref is None:
110 
111  raise CobotGuiException('Could not find class "%s" in module "%s"'
112  %(package['class_name'], package['module_name']))
113 
114  try:
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)
119 
120  return widget_instance
121 
122  def _parse_widget_xml(self, package_name, widget_xml):
123  """! Read gadegt xml file.
124  @param package_name: python pakage name.
125  @type package_name: string.
126 
127  @param plugin_xml: plugin xml name.
128  @type plugin_xml: string.
129 
130  @return: plugin_desc: package descrition, plugin configuration.
131  @type plugin_desc: tuple (dictonary, dictonary).
132  """
133 
134  package = {'package_path' : None,
135  'module_name' : None,
136  'class_name' : None}
137 
138  config = {'label' : self._label, 'description' : None,
139  'style-sheet' : None, 'restriction': None}
140 
141  pkg_dir = get_pkg_dir(package_name)
142 
143  path = '/'.join([pkg_dir,widget_xml])
144 
145  if not os.path.isfile(path):
146 
147  raise CobotGuiException('WidgetProvider._parse_widget_xml() widget file "%s" in package "%s" not found'
148  %(widget_xml, package_name))
149 
150  try:
151  tree = ElementTree.parse(path)
152  except Exception as e:
153  raise CobotGuiException('WidgetProvider._parse_widget_xml() raised with exception "%s"'%e)
154 
155  path = [pkg_dir,tree.getroot().attrib['path']]
156 
157  package['package_path'] = '/'.join(path)
158 
159  widget_import = tree.getroot().find('./class').attrib['import']
160 
161  # separate module name and class name
162  module_name, class_name = widget_import.rsplit('.', 1)
163  package['module_name'] = module_name
164  package['class_name'] = class_name
165 
166  config['description'] = tree.getroot().find('./description').text
167 
168  config_node = tree.getroot().find('./init')
169 
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
176  else:
177  pass
178 
179  return package, config
180 
181 #End of file
182 
Class for load Python widget package.
def get_widget_registered
Get package name and xml file name by widget label.


airbus_cobot_gui
Author(s):
autogenerated on Thu Dec 17 2015 11:42:05