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