From ab8194e92b9c3728d8f86cb9c81de180b6884eee Mon Sep 17 00:00:00 2001 From: Eric Lin Date: Fri, 4 May 2018 18:05:00 -0400 Subject: Some more pyscripting tweaks. Fixed issue with capturing ppaged output. Added pyscript bridge to ipy command. Saving progress. --- cmd2/pyscript_bridge.py | 21 +++++++++++++++++---- 1 file changed, 17 insertions(+), 4 deletions(-) (limited to 'cmd2/pyscript_bridge.py') diff --git a/cmd2/pyscript_bridge.py b/cmd2/pyscript_bridge.py index 055ae4ae..ecd2b622 100644 --- a/cmd2/pyscript_bridge.py +++ b/cmd2/pyscript_bridge.py @@ -55,22 +55,35 @@ class CopyStream(object): def clear(self): self.buffer = '' + def __getattr__(self, item): + if item in self.__dict__: + return self.__dict__[item] + else: + return getattr(self.inner_stream, item) + def _exec_cmd(cmd2_app, func): """Helper to encapsulate executing a command and capturing the results""" copy_stdout = CopyStream(sys.stdout) copy_stderr = CopyStream(sys.stderr) + copy_cmd_stdout = CopyStream(cmd2_app.stdout) + cmd2_app._last_result = None - with redirect_stdout(copy_stdout): - with redirect_stderr(copy_stderr): - func() + try: + cmd2_app.stdout = copy_cmd_stdout + with redirect_stdout(copy_stdout): + with redirect_stderr(copy_stderr): + func() + finally: + cmd2_app.stdout = copy_cmd_stdout.inner_stream # if stderr is empty, set it to None stderr = copy_stderr if copy_stderr.buffer else None - result = CommandResult(stdout=copy_stdout.buffer, stderr=stderr, data=cmd2_app._last_result) + outbuf = copy_cmd_stdout.buffer if copy_cmd_stdout.buffer else copy_stdout.buffer + result = CommandResult(stdout=outbuf, stderr=stderr, data=cmd2_app._last_result) return result -- cgit v1.2.1