Package roslib :: Module resources
[frames] | no frames]

Source Code for Module roslib.resources

  1  # Software License Agreement (BSD License) 
  2  # 
  3  # Copyright (c) 2008, Willow Garage, Inc. 
  4  # All rights reserved. 
  5  # 
  6  # Redistribution and use in source and binary forms, with or without 
  7  # modification, are permitted provided that the following conditions 
  8  # are met: 
  9  # 
 10  #  * Redistributions of source code must retain the above copyright 
 11  #    notice, this list of conditions and the following disclaimer. 
 12  #  * Redistributions in binary form must reproduce the above 
 13  #    copyright notice, this list of conditions and the following 
 14  #    disclaimer in the documentation and/or other materials provided 
 15  #    with the distribution. 
 16  #  * Neither the name of Willow Garage, Inc. nor the names of its 
 17  #    contributors may be used to endorse or promote products derived 
 18  #    from this software without specific prior written permission. 
 19  # 
 20  # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 
 21  # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 
 22  # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 
 23  # FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE 
 24  # COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 
 25  # INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 
 26  # BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 
 27  # LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 
 28  # CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 
 29  # LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN 
 30  # ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 
 31  # POSSIBILITY OF SUCH DAMAGE. 
 32  # 
 33   
 34  """ 
 35  Warning: do not use this library.  It is unstable and most of the routines 
 36  here have been superceded by other libraries (e.g. rospkg).  These 
 37  routines will likely be *deleted* in future releases. 
 38  """ 
 39   
 40  import os 
 41   
 42  import roslib.manifest 
 43  import roslib.names 
 44  import roslib.packages 
 45   
46 -def _get_manifest_by_dir(package_dir):
47 """ 48 Helper routine for loading Manifest instances 49 @param package_dir: package directory location 50 @type package_dir: str 51 @return: manifest for package 52 @rtype: Manifest 53 """ 54 f = os.path.join(package_dir, roslib.manifest.MANIFEST_FILE) 55 if f: 56 return roslib.manifest.parse_file(f) 57 else: 58 return None
59
60 -def list_package_resources_by_dir(package_dir, include_depends, subdir, rfilter=os.path.isfile):
61 """ 62 List resources in a package directory within a particular 63 subdirectory. This is useful for listing messages, services, etc... 64 @param package_dir: package directory location 65 @type package_dir: str 66 @param subdir: name of subdirectory 67 @type subdir: str 68 @param include_depends: if True, include resources in dependencies as well 69 @type include_depends: bool 70 @param rfilter: resource filter function that returns true if filename is the desired resource type 71 @type rfilter: fn(filename)->bool 72 """ 73 package = os.path.basename(package_dir) 74 resources = [] 75 dir = roslib.packages._get_pkg_subdir_by_dir(package_dir, subdir, False) 76 if os.path.isdir(dir): 77 resources = [roslib.names.resource_name(package, f, my_pkg=package) \ 78 for f in os.listdir(dir) if rfilter(os.path.join(dir, f))] 79 else: 80 resources = [] 81 if include_depends: 82 depends = _get_manifest_by_dir(package_dir).depends 83 dirs = [roslib.packages.get_pkg_subdir(d.package, subdir, False) for d in depends] 84 for (dep, dir_) in zip(depends, dirs): #py3k 85 if not dir_ or not os.path.isdir(dir_): 86 continue 87 resources.extend(\ 88 [roslib.names.resource_name(dep.package, f, my_pkg=package) \ 89 for f in os.listdir(dir_) if rfilter(os.path.join(dir_, f))]) 90 return resources
91
92 -def list_package_resources(package, include_depends, subdir, rfilter=os.path.isfile):
93 """ 94 List resources in a package within a particular subdirectory. This is useful for listing 95 messages, services, etc... 96 @param package: package name 97 @type package: str 98 @param subdir: name of subdirectory 99 @type subdir: str 100 @param include_depends: if True, include resources in dependencies as well 101 @type include_depends: bool 102 @param rfilter: resource filter function that returns true if filename is the desired resource type 103 @type rfilter: fn(filename)->bool 104 """ 105 package_dir = roslib.packages.get_pkg_dir(package) 106 return list_package_resources_by_dir(package_dir, include_depends, subdir, rfilter)
107