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 <arksine.code@gmail.com>
This commit is contained in:
Arksine 2020-11-18 09:10:40 -05:00
parent 706d86dd21
commit 116c1e9f29
1 changed files with 14 additions and 0 deletions

View File

@ -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)