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

[ros2interface] Allow stdin input for 'ros2 interface show' #387

Merged
merged 8 commits into from May 27, 2020
Merged

[ros2interface] Allow stdin input for 'ros2 interface show' #387

merged 8 commits into from May 27, 2020

Conversation

ghost
Copy link

@ghost ghost commented Oct 25, 2019

Implements #349.

Interface types can now be piped directly into show, like this:
$ ros2 topic type /chatter | ros2 interface show

@jacobperron jacobperron self-requested a review October 25, 2019 20:43
Copy link
Member

@dirk-thomas dirk-thomas left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think the actually read data from stdin should be the default value. When you call --help the default value would also be different.

Btw calling ros2 interface show results in an exception atm.

Also the usage / help doesn't give any indicator that the type can be piped in.

@dirk-thomas dirk-thomas added enhancement New feature or request in progress Actively being worked on (Kanban column) requires-changes labels Oct 25, 2019
@ghost
Copy link
Author

ghost commented Oct 26, 2019

I've attached an action to add_argument that reads from stdin if - was passed. Requiring - preserves the parser's required argument check on type since we don't have to allow type to be missing. This requires explicitly running

$ ros2 topic type /chatter | ros2 interface show -

rather than

$ ros2 topic type /chatter | ros2 interface show

This also replaces reading stdin directly for the default value and fixes the resulting exceptions.

I've also added more information in the help but I'm not sure if it's written clearly enough, or if there's a convention that I'm breaking.

@dirk-thomas
Copy link
Member

Thanks for the updates.

I applies a few changes. Please have a look:

  • 0134dd0 Move all the help text into the help attribute. That avoids potentially overwriting the epilog if set externally. Also the textwrap.dedent seems unnecessary.
  • a085e27 Fix the import order to pass the linters.
  • f14c359 Simplify the logic in the custom action to avoid duplicate parts.
  • 3e7241a Handle the case where the piped command fails / doesn't output anything more gracefully.

@dirk-thomas dirk-thomas added in review Waiting for review (Kanban column) and removed in progress Actively being worked on (Kanban column) requires-changes labels Nov 14, 2019
@dirk-thomas
Copy link
Member

dirk-thomas commented Nov 14, 2019

@sharminramli It would be great if you could add a unit test to cover this new feature to avoid it regressing in the future.

@dirk-thomas
Copy link
Member

It would be great if you could add a unit test to cover this new feature to avoid it regressing in the future.

@sharminramli Friendly ping.

@jacobperron jacobperron removed their request for review November 18, 2019 23:08
@ghost
Copy link
Author

ghost commented Nov 19, 2019

@dirk-thomas Sorry about the delay. Thanks for the changes, they look great.

I started working on the unit tests, but I ran into some issues piping stdin to the test process. I am patching a mock stdin to the test function with

    @patch('sys.stdin', new_callable=StringIO)
    def test_show_message_from_stdin(self, mocked_stdin):
        mocked_stdin.write('std_msgs/msg/String')
        mocked_stdin.seek(0)
        with self.launch_interface_command(
            arguments=['show', '-']
        ) as interface_command:
            assert interface_command.wait_for_shutdown(timeout=2)
        assert interface_command.exit_code == launch_testing.asserts.EXIT_OK
        assert launch_testing.tools.expect_output(
            expected_lines=[
                'string data'
            ],
            text=interface_command.output,
            strict=True
        )

but it appears to hang at
values = sys.stdin.readline().strip() of show.py. Is there something I'm missing?

@hidmic
Copy link
Contributor

hidmic commented Nov 20, 2019

@sharminramli you won't be able to pipe data into the launched process like that. launch does not expose the stdin pipe either, so your best bet right now is to use shell=True and pipe text. For that, you'll have to change the test fixture a bit to be able to launch an action like:

command_action = ExecuteProcess(
  cmd=[sys.executable, '-c', "print('std_msgs/msg/String')", '|', 'ros2', 'interface', 'show', '-'],
  shell=True,
  output='screen'
)

Alternatively, you can propose a reasonable way to expose the stdin pipe for launched processes in general (back in the launch repo).

@dirk-thomas
Copy link
Member

@sharminramli Friendly ping.

@ghost
Copy link
Author

ghost commented Feb 15, 2020

@dirk-thomas Sorry for the wait. I've added the test as test_show_stdin in test_cli.py.
@hidmic Thanks for the help. I'm not familiar with the launch repo as of now, so I've gone with the first option and modified launch_interface_command to accept the shell option and a list of arguments to be prepended.

Copy link
Contributor

@hidmic hidmic left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM !

  • Linux Build Status
  • Linux-aarch64 Build Status
  • macOS Build Status
  • Windows Build Status

@ghost
Copy link
Author

ghost commented May 22, 2020

Rebased and changed the test to use test_msgs/msg/Nested rather than std_msgs/msg/String (ros2/common_interfaces#89). The help message should also be updated but it's being modified in #516 as well.

Copy link
Member

@audrow audrow left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've tested this branch on my computer and it works well - however, test_show_message fails because of the comments added to the message file in Foxy.

@hidmic
Copy link
Contributor

hidmic commented May 22, 2020

however, test_show_message fails because of the comments added to the message file in Foxy.

That was fixed in #516. As a result, this now needs a rebase.

sharminramli and others added 7 commits May 23, 2020 12:55
Signed-off-by: Nursharmin Ramli <nursharminramli@gmail.com>
Signed-off-by: Dirk Thomas <dirk-thomas@users.noreply.github.com>
Signed-off-by: Dirk Thomas <dirk-thomas@users.noreply.github.com>
Signed-off-by: Dirk Thomas <dirk-thomas@users.noreply.github.com>
Signed-off-by: Dirk Thomas <dirk-thomas@users.noreply.github.com>
Signed-off-by: Nursharmin Ramli <nursharminramli@gmail.com>
Signed-off-by: Nursharmin Ramli <nursharminramli@gmail.com>
Signed-off-by: Nursharmin Ramli <nursharminramli@gmail.com>
@ghost
Copy link
Author

ghost commented May 23, 2020

Rebased and changed the help message to refer to example_interfaces rather than std_msgs. I'm not sure why the Jenkins build is failing though.

@audrow
Copy link
Member

audrow commented May 26, 2020

  • Linux Build Status
  • Linux-aarch64 Build Status
  • macOS Build Status
  • Windows Build Status

@hidmic
Copy link
Contributor

hidmic commented May 27, 2020

Thanks for re-running CI @audrow !

@hidmic hidmic requested a review from jacobperron May 27, 2020 12:58
@hidmic
Copy link
Contributor

hidmic commented May 27, 2020

Alright, thank you for contribution and your patience @sharminramli ! Merging.

@hidmic hidmic merged commit 29cdf68 into ros2:master May 27, 2020
craigh92 pushed a commit to craigh92/ros2cli that referenced this pull request Jun 11, 2020
* Allow stdin input for 'ros2 interface show'

Signed-off-by: Nursharmin Ramli <nursharminramli@gmail.com>

* just use help for all the information

Signed-off-by: Dirk Thomas <dirk-thomas@users.noreply.github.com>

* fix import order

Signed-off-by: Dirk Thomas <dirk-thomas@users.noreply.github.com>

* simplify logic

Signed-off-by: Dirk Thomas <dirk-thomas@users.noreply.github.com>

* catch empty values in case stdin doesn't contain output

Signed-off-by: Dirk Thomas <dirk-thomas@users.noreply.github.com>

* Add test for 'ros2 interface show' with stdin

Signed-off-by: Nursharmin Ramli <nursharminramli@gmail.com>

* Use test_msgs instead of std_msgs for stdin test

Signed-off-by: Nursharmin Ramli <nursharminramli@gmail.com>

* Use example_interfaces in help for show

Signed-off-by: Nursharmin Ramli <nursharminramli@gmail.com>

Co-authored-by: Dirk Thomas <dirk-thomas@users.noreply.github.com>
Signed-off-by: Craig <CraigUkaea@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request in review Waiting for review (Kanban column)
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants