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 genmsg 
 43   
 44  import genpy.message  # for wrapping get_message_class, get_service_class 
 45  # forward a bunch of old symbols from genpy for backwards compat 
 46  from genpy import DeserializationError  # noqa: F401 
 47  from genpy import Duration  # noqa: F401 
 48  from genpy import Message  # noqa: F401 
 49  from genpy import SerializationError  # noqa: F401 
 50  from genpy import TVal  # noqa: F401 
 51  from genpy import Time  # noqa: F401 
 52  from genpy.message import check_type  # noqa: F401 
 53  from genpy.message import fill_message_args  # noqa: F401 
 54  from genpy.message import get_printable_message_args  # noqa: F401 
 55  from genpy.message import strify_message  # noqa: F401 
 56   
 57  import roslib 
 58   
 59  import rospkg 
 60   
 61   
62 -def _get_message_or_service_class(type_str, message_type, reload_on_error=False):
63 # parse package and local type name for import 64 package, base_type = genmsg.package_resource_name(message_type) 65 if not package: 66 if base_type == 'Header': 67 package = 'std_msgs' 68 else: 69 raise ValueError('message type is missing package name: %s' % str(message_type)) 70 pypkg = val = None 71 try: 72 # bootstrap our sys.path 73 roslib.launcher.load_manifest(package) 74 # import the package and return the class 75 pypkg = __import__('%s.%s' % (package, type_str)) 76 val = getattr(getattr(pypkg, type_str), base_type) 77 except rospkg.ResourceNotFound: 78 val = None 79 except ImportError: 80 val = None 81 except AttributeError: 82 val = None 83 84 # this logic is mainly to support rosh, so that a user doesn't 85 # have to exit a shell just because a message wasn't built yet 86 if val is None and reload_on_error: 87 try: 88 reload # Python 2 89 except NameError: 90 from importlib import reload # Python 3 91 try: 92 if pypkg: 93 reload(pypkg) 94 val = getattr(getattr(pypkg, type_str), base_type) 95 except Exception: 96 val = None 97 return val
98 99 100 # cache for get_message_class 101 _message_class_cache = {} 102 103 104 # cache for get_service_class 105 _service_class_cache = {} 106 107
108 -def get_message_class(message_type, reload_on_error=False):
109 if message_type in _message_class_cache: 110 return _message_class_cache[message_type] 111 # try w/o bootstrapping 112 cls = genpy.message.get_message_class(message_type, reload_on_error=reload_on_error) 113 if cls is None: 114 # try old loader w/ bootstrapping 115 cls = _get_message_or_service_class('msg', message_type, reload_on_error=reload_on_error) 116 if cls: 117 _message_class_cache[message_type] = cls 118 return cls
119 120
121 -def get_service_class(service_type, reload_on_error=False):
122 if service_type in _service_class_cache: 123 return _service_class_cache[service_type] 124 cls = genpy.message.get_service_class(service_type, reload_on_error=reload_on_error) 125 # try w/o bootstrapping 126 if cls is None: 127 # try old loader w/ bootstrapping 128 cls = _get_message_or_service_class('srv', service_type, reload_on_error=reload_on_error) 129 if cls: 130 _service_class_cache[service_type] = cls 131 return cls
132