ssm_skill_provider.py
Go to the documentation of this file.
00001 #!/usr/bin/env python
00002 #
00003 # Copyright 2015 Airbus
00004 # Copyright 2017 Fraunhofer Institute for Manufacturing Engineering and Automation (IPA)
00005 #
00006 # Licensed under the Apache License, Version 2.0 (the "License");
00007 # you may not use this file except in compliance with the License.
00008 # You may obtain a copy of the License at
00009 #
00010 #   http://www.apache.org/licenses/LICENSE-2.0
00011 #
00012 # Unless required by applicable law or agreed to in writing, software
00013 # distributed under the License is distributed on an "AS IS" BASIS,
00014 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00015 # See the License for the specific language governing permissions and
00016 # limitations under the License.
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     


airbus_ssm_core
Author(s): Ludovic Delval
autogenerated on Thu Jun 6 2019 17:59:28