def get_msg_text(type_, raw=False, rospack=None): """ Get .msg file for type_ as text :param type_: message type, ``str`` :param raw: if True, include comments and whitespace (default False), ``bool`` :returns: text of .msg file, ``str`` :raises :exc:`ROSMsgException` If type_ is unknown """ if rospack is None: rospack = rospkg.RosPack() search_path = {} for p in rospack.list(): search_path[p] = [os.path.join(rospack.get_path(p), 'msg')] context = genmsg.MsgContext.create_default() try: spec = genmsg.load_msg_by_type(context, type_, search_path) genmsg.load_depends(context, spec, search_path) except Exception as e: raise ROSMsgException("Unable to load msg [%s]: %s"%(type_, e)) if raw: return spec.text else: return spec_to_str(context, spec)
def __init__(self, msg_type, used_by_topic, msg_name=None, is_array=False): self.msg_type = msg_type if not msg_name: self.msg_name = used_by_topic else: self.msg_name = msg_name if self.msg_type in RosMessage._primitive_types: self.is_primitive = True else: self.is_primitive = False self.used_by_topic = used_by_topic self.fields = {} self.is_array = is_array if len(RosMessage._search_path) == 0: rospack = rospkg.RosPack() for p in rospack.list(): package_paths = self.get_package_paths(p, rospack) RosMessage._search_path[p] = [ os.path.join(d, 'msg') for d in package_paths ] if not RosMessage._context: RosMessage._context = genmsg.MsgContext.create_default() if not self.is_primitive: spec = genmsg.load_msg_by_type(RosMessage._context, self.msg_type, RosMessage._search_path) msgs_fields = spec.__dict__ names = msgs_fields["names"] types = msgs_fields["types"] for i in range(0, len(names)): msg_field_name = names[i] msg_field_type = types[i] array = False pattern = r'\[[0-9]*\]' array_parts = re.findall(pattern, msg_field_type) if len(array_parts) > 0: for array_part in array_parts: msg_field_type = msg_field_type.replace(array_part, "") array = True try: msg_field = RosMessage(msg_field_type, self.used_by_topic, msg_field_name, is_array=array) self.fields[msg_field_name] = msg_field except genmsg.msg_loader.MsgNotFound as e: print "Message %s not found" % msg_field_type pass
def get_msg_text(type_, raw=False, rospack=None): """ Get .msg file for type_ as text :param type_: message type, ``str`` :param raw: if True, include comments and whitespace (default False), ``bool`` :returns: text of .msg file, ``str`` :raises :exc:`ROSMsgException` If type_ is unknown """ if rospack is None: rospack = rospkg.RosPack() search_path = {} for p in rospack.list(): search_path[p] = [os.path.join(rospack.get_path(p), 'msg')] context = genmsg.MsgContext.create_default() try: spec = genmsg.load_msg_by_type(context, type_, search_path) genmsg.load_depends(context, spec, search_path) except Exception as e: raise ROSMsgException("Unable to load msg [%s]: %s" % (type_, e)) if raw: return spec.text else: return spec_to_str(context, spec)
def getString(topic): try: val = rosgraph.Master('/rostopic').getTopicTypes() except socket.error: raise ROSTopicIOException("Unable to communicate with master!") # exact match first, followed by prefix match matches = [(t, t_type) for t, t_type in val if t == topic] if not matches: matches = [(t, t_type) for t, t_type in val if topic.startswith(t+'/')] # choose longest match matches.sort(key=itemgetter(0), reverse=True) if matches: msg = matches[0][1] rospack = rospkg.RosPack() search_path = {} for p in rospack.list(): search_path[p] = [os.path.join(rospack.get_path(p), 'msg')] context = genmsg.MsgContext.create_default() try: spec = genmsg.load_msg_by_type(context, msg, search_path) genmsg.load_depends(context, spec, search_path) except Exception as e: raise ROSMsgException("Unable to load msg [%s]: %s"%(msg, e)) str = spec_to_str(context,spec) str = str.replace('\n',' ') str = ' '.join(str.split()) return str
def getString(topic): import rostopic try: val = rosgraph.Master('/rostopic').getTopicTypes() except socket.error: print "Unable to communicate with ROS master. Run roscore before starting the bridge." # exact match first, followed by prefix match matches = [(t, t_type) for t, t_type in val if t == topic] if not matches: matches = [(t, t_type) for t, t_type in val if topic.startswith(t+'/')] # choose longest match if matches: matches.sort(key=itemgetter(0), reverse=True) else: print "A match could not be found for the specified ROS topic. Run rostopic list to check if it does." if matches: msg = matches[0][1] if not msg: print "The ROS message corresponding to the specified ROS topic could not be identified." rospack = rospkg.RosPack() search_path = {} for p in rospack.list(): search_path[p] = [os.path.join(rospack.get_path(p), 'msg')] context = genmsg.MsgContext.create_default() try: spec = genmsg.load_msg_by_type(context, msg, search_path) genmsg.load_depends(context, spec, search_path) except Exception as e: raise ROSMsgException("Unable to load msg [%s]: %s"%(msg, e)) str = spec_to_str(context,spec) str = str.replace('\n',' ') str = ' '.join(str.split()) return str
def test_compute_full_text(): from genmsg import MsgContext, compute_full_text, load_msg_by_type, load_depends msg_context = MsgContext.create_default() search_path = get_search_path() # regression test against values used for cturtle-electric spec = load_msg_by_type(msg_context, 'rosgraph_msgs/Log', search_path) load_depends(msg_context, spec, search_path) val = compute_full_text(msg_context, spec) assert val == log_full_text, "[%s][%s]"%(val, log_full_text) spec = load_msg_by_type(msg_context, 'geometry_msgs/TwistWithCovarianceStamped', search_path) load_depends(msg_context, spec, search_path) val = compute_full_text(msg_context, spec) assert val == twist_with_covariance_stamped_full_text, "[%s][%s]"%(val, twist_with_covariance_stamped_full_text)
def get_spec(msg_type, search_path): context = genmsg.MsgContext.create_default() try: spec = genmsg.load_msg_by_type(context, msg_type, search_path) genmsg.load_depends(context, spec, search_path) except Exception as e: raise ROSMsgException("Unable to load msg [%s]: %s" % (msg_type, e)) return spec
def get_spec(msg_type, search_path): context = genmsg.MsgContext.create_default() try: spec = genmsg.load_msg_by_type(context, msg_type, search_path) genmsg.load_depends(context, spec, search_path) except Exception as e: raise ROSMsgException("Unable to load msg [%s]: %s"%(msg_type, e)) return spec
def __init__(self, msg_type, used_by_topic, msg_name = None, is_array = False): self.msg_type = msg_type if not msg_name: self.msg_name = used_by_topic else: self.msg_name = msg_name if self.msg_type in RosMessage._primitive_types: self.is_primitive = True else: self.is_primitive = False self.used_by_topic = used_by_topic self.fields = {} self.is_array = is_array if len(RosMessage._search_path) == 0: rospack = rospkg.RosPack() for p in rospack.list(): package_paths = self.get_package_paths(p, rospack) RosMessage._search_path[p] = [os.path.join(d, 'msg') for d in package_paths] if not RosMessage._context: RosMessage._context = genmsg.MsgContext.create_default() if not self.is_primitive: spec = genmsg.load_msg_by_type(RosMessage._context, self.msg_type, RosMessage._search_path) msgs_fields = spec.__dict__ names = msgs_fields["names"] types = msgs_fields["types"] for i in range(0,len(names)): msg_field_name = names[i] msg_field_type = types[i] array = False pattern = r'\[[0-9]*\]' array_parts = re.findall(pattern, msg_field_type) if len(array_parts) > 0: for array_part in array_parts: msg_field_type = msg_field_type.replace(array_part,"") array = True try: msg_field = RosMessage(msg_field_type, self.used_by_topic, msg_field_name, is_array=array) self.fields[msg_field_name] = msg_field except genmsg.msg_loader.MsgNotFound as e: print "Message %s not found" % msg_field_type pass