Skip to content

Commit

Permalink
add topic pub prototype completer
Browse files Browse the repository at this point in the history
Signed-off-by: artivis <jeremie.deray@canonical.com>
  • Loading branch information
artivis committed Jul 17, 2019
1 parent 8841312 commit d56f4ac
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 1 deletion.
24 changes: 24 additions & 0 deletions ros2topic/ros2topic/api/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@
from rclpy.validate_full_topic_name import validate_full_topic_name
from ros2cli.node.strategy import NodeStrategy
from ros2msg.api import message_type_completer
from rosidl_parser.definition import AbstractNestedType
from rosidl_parser.definition import NamespacedType
from rosidl_runtime_py.convert import message_to_yaml
from rosidl_runtime_py.import_message import import_message_from_namespaced_type


def get_topic_names_and_types(*, node, include_hidden_topics=False):
Expand Down Expand Up @@ -137,3 +141,23 @@ def _get_msg_class(node, topic, include_hidden_topics):
return None

return import_message_type(topic, message_type)


class TopicMessagePrototypeCompleter:
"""Callable returning a message prototype."""

def __init__(self, *, topic_type_key=None):
self.topic_type_key = topic_type_key

def __call__(self, prefix, parsed_args, **kwargs):
topic_type = getattr(parsed_args, self.topic_type_key)
try:
parts = topic_type.split('/')
package_name = parts[0]
interface_type = parts[1]
message_type = parts[-1]
except LookupError as e:
return None
message = import_message_from_namespaced_type(
AbstractNestedType(NamespacedType([package_name, interface_type], message_type)))
return [message_to_yaml(message())]
5 changes: 4 additions & 1 deletion ros2topic/ros2topic/verb/pub.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
from ros2topic.api import import_message_type
from ros2topic.api import TopicNameCompleter
from ros2topic.api import TopicTypeCompleter
from ros2topic.api import TopicMessagePrototypeCompleter
from ros2topic.verb import VerbExtension
from rosidl_runtime_py import set_message_fields
import yaml
Expand All @@ -38,11 +39,13 @@ def add_arguments(self, parser, cli_name):
help="Type of the ROS message (e.g. 'std_msgs/String')")
arg.completer = TopicTypeCompleter(
topic_name_key='topic_name')
parser.add_argument(
arg = parser.add_argument(
'values', nargs='?', default='{}',
help='Values to fill the message with in YAML format '
'(e.g. "data: Hello World"), '
'otherwise the message will be published with default values')
arg.completer = TopicMessagePrototypeCompleter(
topic_type_key='message_type')
parser.add_argument(
'-r', '--rate', metavar='N', type=float, default=1.0,
help='Publishing rate in Hz (default: 1)')
Expand Down

0 comments on commit d56f4ac

Please sign in to comment.