diff options
author | Eric Lin <anselor@gmail.com> | 2018-05-04 18:05:00 -0400 |
---|---|---|
committer | Eric Lin <anselor@gmail.com> | 2018-05-04 18:05:00 -0400 |
commit | ab8194e92b9c3728d8f86cb9c81de180b6884eee (patch) | |
tree | 581ae19b137f7074960f82ff7de0da29a0c42c0d /cmd2/pyscript_bridge.py | |
parent | c051c84fd44dda4b0c19f6ad01bb14ea970de260 (diff) | |
download | cmd2-git-ab8194e92b9c3728d8f86cb9c81de180b6884eee.tar.gz |
Some more pyscripting tweaks. Fixed issue with capturing ppaged output. Added pyscript bridge to ipy command. Saving progress.
Diffstat (limited to 'cmd2/pyscript_bridge.py')
-rw-r--r-- | cmd2/pyscript_bridge.py | 21 |
1 files changed, 17 insertions, 4 deletions
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 |