Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018 import __builtin__
00019 import os
00020 import sys
00021
00022 import rospy
00023
00024 from roslib.packages import get_pkg_dir
00025 from xml.etree import ElementTree
00026 import importlib
00027
00028 class SkillProvider(object):
00029
00030 def __init__(self, skill_register_file):
00031
00032 if not os.path.isfile(skill_register_file):
00033 raise Exception("Invalid register file path !")
00034
00035 self._tree = ElementTree.parse(skill_register_file)
00036 self._root = self._tree.getroot()
00037
00038 def load(self, name):
00039
00040 """ Find skill registered by name
00041 <skill name="${name}" pkg="${pkg}" module="${module}" class="${class}"/>
00042 """
00043
00044 skill = self._root.find('skill[@name="%s"]'%name)
00045 if skill is not None:
00046 try:
00047 in_pkg = skill.attrib["pkg"]
00048 in_module = skill.attrib["module"]
00049 skill_class = skill.attrib["class"]
00050 except Exception as ex:
00051 rospy.logerr('[SCXML Interpreter] Error in the file register : %s'%(str(ex)))
00052 raise Exception(ex)
00053 skill_class_ref = None
00054 try:
00055 if(in_module in sys.modules):
00056 module_ref = reload(sys.modules[in_module])
00057 else:
00058 module_ref = importlib.import_module(in_module, in_pkg)
00059 except Exception as ex:
00060 rospy.logerr('[SCXML Interpreter] Error in the "%s" file (pkg %s) : %s'%(in_module, in_pkg, str(ex)))
00061 raise Exception(ex)
00062 try:
00063 skill_class_ref = module_ref.__getattribute__(skill_class)
00064 except Exception as ex:
00065 rospy.logerr('[SCXML Interpreter] Error in the class "%s" (pkg %s, file %s) : %s'%(skill_class,in_pkg,in_module, str(ex)))
00066 raise Exception(ex)
00067 return skill_class_ref
00068 else:
00069 rospy.logerr('[SCXML Interpreter] Skill named "%s" not found in register file !'%name)
00070 return None
00071
00072
00073