Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

support multiple fields in ros2topic echo #964

Merged
merged 4 commits into from
Jan 24, 2025
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
support multiple fields in ros2topic echo
Signed-off-by: Sangtaek Lee <[email protected]>
Signed-off-by: Sangtaek Lee <[email protected]>
sangteak601 committed Jan 24, 2025

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
commit 8955194c6d5901be3f482c290978d5a776e5a2b0
83 changes: 44 additions & 39 deletions ros2topic/ros2topic/verb/echo.py
Original file line number Diff line number Diff line change
@@ -69,7 +69,7 @@ def add_arguments(self, parser, cli_name):
)
)
parser.add_argument(
'--field', type=str, default=None,
'--field', action='append', type=str, default=None,
help='Echo a selected field of a message. '
"Use '.' to select sub-fields. "
'For example, to echo the position field of a nav_msgs/msg/Odometry message: '
@@ -180,11 +180,10 @@ def main(self, *, args):
self.csv = args.csv

# Validate field selection
self.field = args.field
if self.field is not None:
self.field = list(filter(None, self.field.split('.')))
if not self.field:
raise RuntimeError(f"Invalid field value '{args.field}'")
self.fields_list = []
for field in args.field:
if field is not None:
self.fields_list.append(list(filter(None, field.split('.'))))

self.truncate_length = args.truncate_length if not args.full_length else None
self.no_arr = args.no_arr
@@ -272,17 +271,23 @@ def _timed_out(self):
self.future.set_result(True)

def _subscriber_callback(self, msg, info):
submsg = msg
if self.field is not None:
for field in self.field:
submsgs = []
for fields in self.fields_list:
submsg = msg
for field in fields:
try:
submsg = getattr(submsg, field)
except AttributeError as ex:
raise RuntimeError(f"Invalid field '{'.'.join(self.field)}': {ex}")
raise RuntimeError(f"Invalid field '{'.'.join(field)}': {ex}")
submsgs.append(submsg)

# Evaluate the current msg against the supplied expression
if self.filter_fn is not None and not self.filter_fn(submsg):
return
if self.filter_fn is not None:
for submsg in submsgs:
if not self.filter_fn(submsg):
submsgs.remove(submsg)
if not submsgs:
return

if self.future is not None and self.once:
self.future.set_result(True)
@@ -291,34 +296,34 @@ def _subscriber_callback(self, msg, info):
if self.clear_screen:
clear_terminal()

if not hasattr(submsg, '__slots__'):
# raw
for submsg in submsgs:
if not hasattr(submsg, '__slots__'):
# raw
if self.include_message_info:
print('---Got new message, message info:---')
print(info)
print('---Message data:---')
print(submsg, end='\n---\n')
continue

if self.csv:
to_print = message_to_csv(
submsg,
truncate_length=self.truncate_length,
no_arr=self.no_arr,
no_str=self.no_str)
if self.include_message_info:
to_print = f'{",".join(str(x) for x in info.values())},{to_print}'
print(to_print)
continue
# yaml
if self.include_message_info:
print('---Got new message, message info:---')
print(info)
print('---Message data:---')
print(submsg, end='\n---\n')
return

if self.csv:
to_print = message_to_csv(
submsg,
truncate_length=self.truncate_length,
no_arr=self.no_arr,
no_str=self.no_str)
if self.include_message_info:
to_print = f'{",".join(str(x) for x in info.values())},{to_print}'
print(to_print)
return
# yaml
if self.include_message_info:
print(yaml.dump(info), end='---\n')
print(
message_to_yaml(
submsg, truncate_length=self.truncate_length,
no_arr=self.no_arr, no_str=self.no_str, flow_style=self.flow_style),
end='---\n')

print(yaml.dump(info), end='---\n')
print(
message_to_yaml(
submsg, truncate_length=self.truncate_length,
no_arr=self.no_arr, no_str=self.no_str, flow_style=self.flow_style),
end='---\n')

def _expr_eval(expr):
def eval_fn(m):