utils.py
Go to the documentation of this file.
00001 #!/usr/bin/env python
00002 #
00003 # License: BSD
00004 #   https://raw.github.com/robotics-in-concert/rocon_app_platform/master/rocon_appmanager/LICENSE
00005 #
00006 ##############################################################################
00007 # Imports
00008 ##############################################################################
00009 
00010 import rospy
00011 import roslib
00012 import roslib.names
00013 import concert_msgs.msg as concert_msgs
00014 from .exceptions import NotFoundException, InvalidPlatformTupleException
00015 
00016 ##############################################################################
00017 # Classes
00018 ##############################################################################
00019 
00020 
00021 class PlatformTuple(object):
00022 
00023     def __init__(self, platform_tuple):
00024         '''
00025           Converts a platform tuple string into a structure.
00026           @param platform_tuple platform.system.robot
00027           @type string
00028         '''
00029         platform_tuple_list = platform_tuple.split('.')
00030         if len(platform_tuple_list) != 3:
00031             raise InvalidPlatformTupleException("len('%s') != 3" % platform_tuple)
00032         # also validate against concert_msgs.Constants
00033         self.platform = platform_tuple_list[0]
00034         self.system = platform_tuple_list[1]
00035         self.robot = platform_tuple_list[2]
00036 
00037 
00038 def findResource(resource):
00039     '''
00040       @return: filepath of resource.  Does not validate if filepath actually exists.
00041       @raise ValueError: if resource is not a valid resource name.
00042       @raise roslib.packages.InvalidROSPkgException: if package referred
00043         to in resource name cannot be found.
00044       @raise NotFoundException: if resource does not exist.
00045     '''
00046 
00047     p, a = roslib.names.package_resource_name(resource)
00048 
00049     if not p:
00050         raise ValueError("Resource is missing package name: %s" % (resource))
00051     matches = roslib.packages.find_resource(p, a)
00052 
00053     # TODO: convert ValueError to better type for better error messages
00054     if len(matches) == 1:
00055         return matches[0]
00056     elif not matches:
00057         raise NotFoundException("No resource [%s]" % (resource))
00058     else:
00059         print matches
00060         raise ValueError("Multiple resources named [%s]" % (resource))
00061 
00062 
00063 def find_resource(resource):
00064     '''
00065       Ros style resource finder.
00066 
00067       @param resource is a ros resource (package/name)
00068       @type str
00069       @return full path to the resource
00070       @type str
00071       @raise NotFoundException: if resource does not exist.
00072     '''
00073     p, a = roslib.names.package_resource_name(resource)
00074     if not p:
00075         raise NotFoundException("resource is missing package name [%s]" % (resource))
00076     matches = roslib.packages.find_resource(p, a)
00077     if len(matches) == 1:
00078         return matches[0]
00079     elif not matches:
00080         raise NotFoundException("no resource [%s]" % (resource))
00081     else:
00082         #print matches
00083         raise NotFoundException("multiple resources found [%s]" % (resource))
00084 
00085 
00086 def platform_tuple(platform, system, robot):
00087     '''
00088       Return the platform tuple string identified by the three strings.
00089     '''
00090     return (platform + '.' + system + '.' + robot)
00091 
00092 
00093 def platform_compatible(first_platform_tuple, second_platform_tuple):
00094     '''
00095       Used to check platform compatibility between app manager
00096       and its apps.
00097 
00098       @param first_platform_tuple : platform.system.robot
00099       @type string
00100       @param second_platform_tuple : platform.system.robot
00101       @type string
00102 
00103       @return false or true depending on compatibility result
00104       @rtype bool
00105     '''
00106     try:
00107         platform_one = PlatformTuple(first_platform_tuple)
00108         platform_two = PlatformTuple(second_platform_tuple)
00109     except InvalidPlatformTupleException as e:
00110         rospy.logwarn("App Manager : invalid platform tuple [%s]" % str(e))
00111         return False
00112     if platform_one.platform != concert_msgs.Constants.PLATFORM_ANY and \
00113        platform_two.platform != concert_msgs.Constants.PLATFORM_ANY and \
00114        platform_one.platform != platform_two.platform:
00115         return False
00116     if platform_one.system != platform_two.system:
00117         return False
00118     if platform_one.robot != concert_msgs.Constants.ROBOT_ANY and \
00119        platform_two.robot != concert_msgs.Constants.ROBOT_ANY and \
00120        platform_one.robot != platform_two.robot:
00121         return False
00122     return True
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Friends


rocon_appmanager
Author(s): Jihoon
autogenerated on Tue Jan 22 2013 12:59:53