From 116c1e9f29ccabda2fb8fe9fc30fd559db0bc933 Mon Sep 17 00:00:00 2001 From: Arksine Date: Wed, 18 Nov 2020 09:10:40 -0500 Subject: [PATCH] shell_command: add run_with_response() method This method collects the entire response and returns it with the call. Suitable for commands that produce little output. Signed-off-by: Eric Callahan --- moonraker/plugins/shell_command.py | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/moonraker/plugins/shell_command.py b/moonraker/plugins/shell_command.py index d1916b2..04d3e7e 100644 --- a/moonraker/plugins/shell_command.py +++ b/moonraker/plugins/shell_command.py @@ -93,6 +93,20 @@ class ShellCommand: self.io_loop.remove_handler(fd) return complete + async def run_with_response(self, timeout=2.): + result = [] + + def cb(data): + data = data.strip() + if data: + result.append(data.decode()) + prev_cb = self.output_cb + self.output_cb = cb + await self.run(timeout) + self.output_cb = prev_cb + return "\n".join(result) + + class ShellCommandFactory: def build_shell_command(self, cmd, callback): return ShellCommand(cmd, callback)