Package rosh :: Package impl :: Module msg
[frames] | no frames]

Source Code for Module rosh.impl.msg

  1  # Software License Agreement (BSD License) 
  2  # 
  3  # Copyright (c) 2010, 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: msg.py 11427 2010-10-06 23:39:08Z kwc $ 
 34   
 35  from __future__ import with_statement 
 36   
 37  import sys 
 38   
 39  import roslib.packages 
 40  import rospy 
 41   
 42  import rosmsg 
 43   
 44  from rosh.impl.namespace import Namespace, Concept 
 45  from rosh.impl.bagy import create 
 46       
47 -class MsgPackage(Namespace):
48
49 - def __init__(self, name, config):
50 """ 51 ctor. 52 @param config: Namespace configuration instance with additional 'listener' attribute. 53 @type config: L{NamespaceConfig} 54 """ 55 super(MsgPackage, self).__init__(name, config) 56 57 # an artifact of reusing the Namespace class is that we have to strip slashes 58 if self._ns == '/': 59 self._ns = '' 60 61 self._msg_cache = {}
62
63 - def _list(self):
64 """ 65 Override Namespace._list() 66 """ 67 try: 68 if not self._ns: 69 return rosmsg.list_packages(mode=self._config.mode) 70 else: 71 return rosmsg.list_types(self._name, mode=self._config.mode) 72 except: 73 return []
74
75 - def _getAttributeNames(self):
76 return list(set([s[len(self._ns):].split('/')[0] for s in self._list()]))
77
78 - def __repr__(self):
79 return self.__str__()
80
81 - def __str__(self):
82 return self._name
83
84 - def __getitem__(self, key):
85 if not self._ns: 86 return super(MsgPackage, self).__getitem__(key) 87 elif key in self._msg_cache: 88 return self._msg_cache[key] 89 else: 90 if self._config.mode == roslib.msgs.EXT: 91 v = self._type = get_message_class(self._ns + key) 92 else: 93 v = self._type = get_service_class(self._ns + key) 94 if v is not None: 95 self._msg_cache[key] = v 96 return v
97
98 -def get_message_class(type_name):
99 val = roslib.message.get_message_class(type_name, reload_on_error=True) 100 if not val: 101 pkg, base_type = roslib.names.package_resource_name(type_name) 102 if '(' not in type_name: 103 # ipython has an introspection bug where it forwards the 104 # actual method call to the getattr. 105 print >> sys.stderr, """Cannot retrieve type [%s]. 106 Please type 'rosmake %s' 107 """%(type_name, pkg) 108 return None 109 else: 110 return val
111
112 -def get_service_class(type_name):
113 val = roslib.message.get_service_class(type_name, reload_on_error=True) 114 if not val: 115 pkg, base_type = roslib.names.package_resource_name(type_name) 116 if '(' not in type_name: 117 # ipython has an introspection bug where it forwards the 118 # actual method call to the getattr. 119 print >> sys.stderr, """Cannot retrieve type [%s]. 120 Please type 'rosmake %s' 121 """%(type_name, pkg) 122 return None 123 else: 124 return val
125
126 -class Msgs(Concept):
127
128 - def __init__(self, ctx, lock):
129 super(Msgs, self).__init__(ctx, lock, MsgPackage) 130 self._config.mode = roslib.msgs.EXT
131
132 - def __contains__(self, key):
133 """ 134 Test if the key (package name) has msgs. 135 """ 136 # right now only supports key tests 137 return key in self._root._list()
138
139 - def __call__(self, msg_repr, msg_class):
140 """ 141 Instantiate msg from a dictionary or YAML representation. 142 """ 143 return create(msg_repr, msg_class)
144
145 -class Srvs(Concept):
146
147 - def __init__(self, ctx, lock):
148 super(Srvs, self).__init__(ctx, lock, MsgPackage) 149 self._config.mode = roslib.srvs.EXT
150
151 - def __contains__(self, key):
152 """ 153 Test if the key (package name) has srvs. 154 """ 155 # right now only supports key tests 156 return key in self._root._list()
157