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

Source Code for Module roslib.message

  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  # Revision $Id$ 
 34   
 35  """ 
 36  Support library for Python autogenerated message files. This defines 
 37  the Message base class used by genmsg_py as well as support 
 38  libraries for type checking and retrieving message classes by type 
 39  name. 
 40  """ 
 41   
 42  import os 
 43  import sys 
 44  import rospkg 
 45  import roslib 
 46   
 47  import genmsg 
 48  import genpy.message #for wrapping get_message_class, get_service_class 
 49   
 50  # forward a bunch of old symbols from genpy for backwards compat 
 51  from genpy import Message, DeserializationError, SerializationError, \ 
 52       Time, Duration, TVal 
 53  from genpy.message import get_printable_message_args, fill_message_args 
 54  from genpy.message import check_type, strify_message 
 55   
56 -def _get_message_or_service_class(type_str, message_type, reload_on_error=False):
57 ## parse package and local type name for import 58 package, base_type = genmsg.package_resource_name(message_type) 59 if not package: 60 if base_type == 'Header': 61 package = 'std_msgs' 62 else: 63 raise ValueError("message type is missing package name: %s"%str(message_type)) 64 pypkg = val = None 65 try: 66 # bootstrap our sys.path 67 roslib.launcher.load_manifest(package) 68 # import the package and return the class 69 pypkg = __import__('%s.%s'%(package, type_str)) 70 val = getattr(getattr(pypkg, type_str), base_type) 71 except rospkg.ResourceNotFound: 72 val = None 73 except ImportError: 74 val = None 75 except AttributeError: 76 val = None 77 78 # this logic is mainly to support rosh, so that a user doesn't 79 # have to exit a shell just because a message wasn't built yet 80 if val is None and reload_on_error: 81 try: 82 if pypkg: 83 reload(pypkg) 84 val = getattr(getattr(pypkg, type_str), base_type) 85 except: 86 val = None 87 return val
88 89 ## cache for get_message_class 90 _message_class_cache = {} 91 92 ## cache for get_service_class 93 _service_class_cache = {} 94
95 -def get_message_class(message_type, reload_on_error=False):
96 if message_type in _message_class_cache: 97 return _message_class_cache[message_type] 98 # try w/o bootstrapping 99 cls = genpy.message.get_message_class(message_type, reload_on_error=reload_on_error) 100 if cls is None: 101 # try old loader w/ bootstrapping 102 cls = _get_message_or_service_class('msg', message_type, reload_on_error=reload_on_error) 103 if cls: 104 _message_class_cache[message_type] = cls 105 return cls
106
107 -def get_service_class(service_type, reload_on_error=False):
108 if service_type in _service_class_cache: 109 return _service_class_cache[service_type] 110 cls = genpy.message.get_service_class(service_type, reload_on_error=reload_on_error) 111 # try w/o bootstrapping 112 if cls is None: 113 # try old loader w/ bootstrapping 114 cls = _get_message_or_service_class('srv', service_type, reload_on_error=reload_on_error) 115 if cls: 116 _service_class_cache[service_type] = cls 117 return cls
118