Skip to content

Commit

Permalink
add utility to import complex message and add support for nested arra…
Browse files Browse the repository at this point in the history
…y in set_message

Signed-off-by: Mikael Arguedas <[email protected]>
  • Loading branch information
mikaelarguedas committed Mar 28, 2019
1 parent ed01243 commit 4bc65c0
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 0 deletions.
24 changes: 24 additions & 0 deletions rosidl_runtime_py/rosidl_runtime_py/import_message.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# Copyright 2019 Mikael Arguedas.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import importlib
from typing import Any

from rosidl_parser.definition import NamespacedType


def import_message_from_namespaced_type(message_type: NamespacedType) -> Any:
module = importlib.import_module(
'.'.join(message_type.basetype.namespaces))
return getattr(module, message_type.basetype.name)
13 changes: 13 additions & 0 deletions rosidl_runtime_py/rosidl_runtime_py/set_message.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@
from typing import Any
from typing import Dict

from rosidl_parser.definition import NamespacedType
from rosidl_runtime_py.convert import slot_types_dict_from_message
from rosidl_runtime_py.import_message import import_message_from_namespaced_type


def set_message_fields(msg: Any, values: Dict[str, str]) -> None:
"""
Expand All @@ -33,4 +37,13 @@ def set_message_fields(msg: Any, values: Dict[str, str]) -> None:
except TypeError:
value = field_type()
set_message_fields(value, field_value)
rosidl_type = slot_types_dict_from_message(msg)[field_name]
# Check if field is an array of ROS messages
if isinstance(field_type(), list):
if isinstance(rosidl_type.basetype, 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)

0 comments on commit 4bc65c0

Please sign in to comment.