34 from rostopic
import get_topic_type
35 from python_qt_binding.QtCore
import qDebug
39 if roslib.msgs.is_valid_constant_type(type_name):
40 if type_name ==
'string':
42 elif type_name ==
'bool':
45 return type(roslib.msgs._convert_val(type_name, 0))
47 return roslib.message.get_message_class(type_name)
52 Get the Python type of a specific field in the given registered topic.
53 If the field is an array, the type of the array's values are returned and the is_array flag is set to True.
54 This is a static type check, so it works for unpublished topics and with empty arrays.
56 :param topic_name: name of field of a registered topic, ``str``, i.e. '/rosout/file'
57 :returns: field_type, is_array
60 topic_type, real_topic_name, _ = get_topic_type(topic_name)
61 if topic_type
is None:
65 message_class = roslib.message.get_message_class(topic_type)
66 if message_class
is None:
67 qDebug(
'topic_helpers.get_field_type(%s): get_message_class(%s) failed' %
68 (topic_name, topic_type))
71 slot_path = topic_name[len(real_topic_name):]
77 Get the Python type of a specific slot in the given message class.
78 If the field is an array, the type of the array's values are returned and the is_array flag is set to True.
79 This is a static type check, so it works for unpublished topics and with empty arrays.
81 :param message_class: message class type, ``type``, usually inherits from genpy.message.Message
82 :param slot_path: path to the slot inside the message class, ``str``, i.e. 'header/seq'
83 :returns: field_type, is_array
86 fields = [f
for f
in slot_path.split(
'/')
if f]
87 for field_name
in fields:
89 field_name, _, field_index = roslib.msgs.parse_type(field_name)
90 except roslib.msgs.MsgSpecException:
92 if field_name
not in getattr(message_class,
'__slots__', []):
96 slot_type = message_class._slot_types[message_class.__slots__.index(field_name)]
97 slot_type, slot_is_array, _ = roslib.msgs.parse_type(slot_type)
98 is_array = slot_is_array
and field_index
is None
101 return message_class, is_array
106 Check is a slot in the given topic is numeric, or an array of numeric values.
107 This is a static type check, so it works for unpublished topics and with empty arrays.
109 :param topic_name: name of field of a registered topic, ``str``, i.e. '/rosout/file'
110 :returns: is_numeric, is_array, description
113 if field_type
in (int, float):
115 message =
'topic "%s" is numeric array: %s[]' % (topic_name, field_type)
117 message =
'topic "%s" is numeric: %s' % (topic_name, field_type)
118 return True, is_array, message
120 return False, is_array,
'topic "%s" is NOT numeric: %s' % (topic_name, field_type)
125 Search inside msg_class for all slots of type slot_type and return their paths.
126 Uses a depth first search.
128 :param msg_class: The class to search in.
129 :param slot_type: The type name or class to search for (e.g. 'float64' or Quaternion).
130 :return: List of paths to slots of type slot_type inside msg_class (e.g. ['header/frame_id']).
133 def _find_slots(msg_class, slot_type):
135 if msg_class == slot_type:
139 for slot_name, slot_type_name
in zip(msg_class.__slots__, msg_class._slot_types):
140 slot_type_name, is_array, _ = roslib.msgs.parse_type(slot_type_name)
143 if roslib.msgs.is_valid_constant_type(slot_type_name):
144 if slot_type_name == slot_type:
145 paths.append([slot_name])
148 slot_class = roslib.message.get_message_class(slot_type_name)
149 if slot_class
is not None:
150 inner_paths = _find_slots(slot_class, slot_type)
151 paths.extend([[slot_name] + path
for path
in inner_paths])
155 return [
'/'.join(path)
for path
in _find_slots(msg_class, slot_type)]
160 Search inside msg_class for all slots of type slot_type and return their paths.
161 Uses a breadth first search, so it will find the most shallow matches first.
163 :param msg_class: The class to search in.
164 :param slot_type: The type name or class to search for (e.g. 'float64' or Quaternion).
165 :return: List of paths to slots of type slot_type inside msg_class (e.g. ['header/frame_id']).
168 queue = [(msg_class, [])]
170 msg_class, path = queue.pop(0)
171 if msg_class == slot_type:
175 for slot_name, slot_type_name
in zip(msg_class.__slots__, msg_class._slot_types):
176 slot_type_name, is_array, _ = roslib.msgs.parse_type(slot_type_name)
179 if roslib.msgs.is_valid_constant_type(slot_type_name):
180 if slot_type_name == slot_type:
181 paths.append(path + [slot_name])
184 slot_class = roslib.message.get_message_class(slot_type_name)
185 if slot_class
is not None:
186 queue.append((slot_class, path + [slot_name]))
188 return [
'/'.join(path)
for path
in paths]