packages.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 rospy
00019 from roslib.packages import get_pkg_dir, ROSPkgException
00020 import os
00021 import sys
00022 import __builtin__
00023 
00024 from python_qt_binding.QtGui import *
00025 from python_qt_binding.QtCore import *
00026 from python_qt_binding import loadUi
00027 from roslib.packages import get_pkg_dir
00028 from xml.etree import ElementTree
00029 
00030 # RE
00031 RE_PREFIXED_TAG = "$"
00032 RE_PREFIXED_BEGIN_TAG = "${"
00033 RE_PREFIXED_END_TAG = "}"
00034 
00035 # Default prefix name
00036 DEFAULT_PREFIX_NAME    = "prefix"
00037 ROS_WS_PREFIX_NAME     = "rosws"
00038 ROS_WS_SRC_PREFIX_NAME = "rossrc"
00039 
00040 # Provide the path of package prefixed 
00041 # Example:
00042 #   - "${PAKAGE_NAME}/../../..."
00043 # The prefixed ${PAKAGE_NAME} pkg is remplaced by get_pkg_dir(PAKAGE_NAME)
00044 def get_pkg_dir_from_prefix(path, ros_package_path=None):
00045     
00046     if RE_PREFIXED_TAG not in path:
00047         return path
00048     
00049     splitpath = path.split(RE_PREFIXED_END_TAG)
00050     
00051     after_prefix_path = splitpath[-1]
00052     
00053     prefix = splitpath[0].split(RE_PREFIXED_BEGIN_TAG)[-1]
00054     
00055     if prefix == DEFAULT_PREFIX_NAME:
00056         if ros_package_path is not None:
00057             rpath = ros_package_path+after_prefix_path
00058         else:
00059             raise ROSPkgException("Unknown prefixed ${prefix} path ! not associated to ros package path !")
00060     elif prefix == ROS_WS_PREFIX_NAME:
00061         rpath = get_ros_workspace_dir()+after_prefix_path
00062     elif prefix == ROS_WS_SRC_PREFIX_NAME:
00063         print get_ros_workspace_src_dir(),after_prefix_path
00064         rpath = get_ros_workspace_src_dir()+after_prefix_path
00065     else:       
00066         rpath = get_pkg_dir(prefix)+after_prefix_path
00067     
00068     return rpath
00069 
00070 def get_ros_workspace_dir():
00071     return os.environ["ROS_WORKSPACE"]
00072 
00073 def get_ros_workspace_src_dir():
00074     return os.path.join(os.environ["ROS_WORKSPACE"],'src')
00075 
00076 class PyModule:
00077     
00078     def __init__(self, module_name):
00079         
00080         rospy.logdebug("Try to import module %s ..."%module_name)
00081         
00082         self._module = None
00083 
00084         try:
00085             self._module = __import__(module_name)
00086         except Exception as ex:
00087             rospy.logerr(str(ex))
00088             raise ex
00089 
00090         rospy.logdebug("Module %s imported."%str(self._module)) 
00091     
00092     def __import__(self, py_class):
00093         rospy.logdebug("  - Try to class %s ..."%py_class) 
00094         class_ref = getattr(self._module, py_class, None)
00095         rospy.logdebug("  - Class %s imported."%str(class_ref))
00096         return class_ref
00097     
00098 class PyPkg:
00099     
00100     def __init__(self, pkg):
00101         self._pkg_dir = get_pkg_dir(pkg)
00102     
00103     def __module__(self, _module):
00104         
00105         _module = _module.replace('.','/')
00106         
00107         fullpath = self._pkg_dir+"/"+_module
00108         
00109         fullpath_split  = fullpath.split('/')
00110         module_dir      = '/'.join(fullpath_split[:-1])
00111         module_name     = fullpath_split[-1]
00112         
00113         sys.path.append(str(module_dir))
00114         
00115         return PyModule(str(module_name))
00116 
00117 class QAgiPackages:
00118     
00119     def __init__(self, pkg):
00120         return QAgiPackages.__pkg__(pkg)
00121     
00122     @staticmethod
00123     def get_pkg_dir(exp):
00124         return get_pkg_dir_from_prefix(exp)
00125     
00126     @staticmethod
00127     def getDirByName(pkg_name):
00128         return get_pkg_dir(pkg_name)
00129     
00130     def getRessources(self):
00131         return None
00132     
00133     def getRessourcesById(self, category, resid):
00134         return None
00135     
00136     @staticmethod
00137     def __ws_dir__():
00138         return os.environ[ROS_PACKAGE_PATH].split(':')[0]
00139     
00140     @staticmethod
00141     def __src_dir__():
00142         return os.environ[ROS_PACKAGE_PATH].split(':')[1]
00143     
00144     @staticmethod
00145     def __pkg__(pkg):
00146         return PyPkg(pkg)
00147     


airbus_pyqt_extend
Author(s): Martin Matignon
autogenerated on Thu Jun 6 2019 17:59:16