def test_import_namespaced_type(): fixture_namespaced_type = NamespacedType(['test_msgs', 'msg'], 'Empty') imported_message = import_message_from_namespaced_type(fixture_namespaced_type) assert type(imported_message) == type(Empty) not_namespaced_type = UnboundedSequence(fixture_namespaced_type) assert not isinstance(not_namespaced_type, NamespacedType) assert isinstance(not_namespaced_type, AbstractNestedType) with warnings.catch_warnings(record=True) as w: warnings.simplefilter('always') imported_message2 = import_message_from_namespaced_type(not_namespaced_type) assert len(w) == 1 assert issubclass(w[0].category, DeprecationWarning) assert type(imported_message2) == type(Empty)
def set_message_fields(msg: Any, values: Dict[str, str]) -> None: """ Set the fields of a ROS message. :param msg: The ROS message to populate. :param values: The values to set in the ROS message. The keys of the dictionary represent fields of the message. :raises AttributeError: If the message does not have a field provided in the input dictionary. :raises ValueError: If a message value does not match its field type. """ for field_name, field_value in values.items(): field = getattr(msg, field_name) field_type = type(field) if field_type is array.array: value = field_type(field.typecode, field_value) elif type(field_value) is field_type: value = field_value else: try: value = field_type(field_value) except TypeError: value = field_type() set_message_fields(value, field_value) rosidl_type = get_message_slot_types(msg)[field_name] # Check if field is an array of ROS messages if isinstance(rosidl_type, AbstractNestedType): if isinstance(rosidl_type.value_type, NamespacedType): field_elem_type = import_message_from_namespaced_type( rosidl_type) for n in range(len(value)): submsg = field_elem_type() set_message_fields(submsg, value[n]) value[n] = submsg setattr(msg, field_name, value)
def get_action(identifier: Text): """Get a message from its full name.""" interface = import_message_from_namespaced_type( get_action_namespaced_type(identifier)) if not is_action(interface): raise ValueError( "Expected the full name of an action, got '{}'".format(identifier)) return interface
def get_interface(identifier: Text): """Get an interface from its full name.""" return import_message_from_namespaced_type(get_namespaced_type(identifier))
def test_import_namespaced_type_feedback_message(): feedback_namespaced_type = NamespacedType(['test_msgs', 'action'], 'Fibonacci_FeedbackMessage') imported_message = import_message_from_namespaced_type( feedback_namespaced_type) assert type(imported_message) == type(Fibonacci_FeedbackMessage)
def test_get_namespaced_type_functions(config): message = import_message_from_namespaced_type( config.to_namespaced_type_function(config.path)) assert message is config.object_type assert config.is_function(message)