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 | |
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.
-rwxr-xr-x | cmd2/cmd2.py | 18 | ||||
-rw-r--r-- | cmd2/pyscript_bridge.py | 21 |
2 files changed, 32 insertions, 7 deletions
diff --git a/cmd2/cmd2.py b/cmd2/cmd2.py index f4f30bd4..63a3cbe3 100755 --- a/cmd2/cmd2.py +++ b/cmd2/cmd2.py @@ -2927,9 +2927,21 @@ Paths or arguments that contain spaces must be enclosed in quotes Run python code from external files with ``run filename.py`` End with ``Ctrl-D`` (Unix) / ``Ctrl-Z`` (Windows), ``quit()``, '`exit()``. """ - banner = 'Entering an embedded IPython shell type quit() or <Ctrl>-d to exit ...' - exit_msg = 'Leaving IPython, back to {}'.format(sys.argv[0]) - embed(banner1=banner, exit_msg=exit_msg) + from .pyscript_bridge import PyscriptBridge + bridge = PyscriptBridge(self) + + if self.locals_in_py: + def load_ipy(self, app): + banner = 'Entering an embedded IPython shell type quit() or <Ctrl>-d to exit ...' + exit_msg = 'Leaving IPython, back to {}'.format(sys.argv[0]) + embed(banner1=banner, exit_msg=exit_msg) + load_ipy(self, bridge) + else: + def load_ipy(app): + banner = 'Entering an embedded IPython shell type quit() or <Ctrl>-d to exit ...' + exit_msg = 'Leaving IPython, back to {}'.format(sys.argv[0]) + embed(banner1=banner, exit_msg=exit_msg) + load_ipy(bridge) history_parser = argparse.ArgumentParser(formatter_class=argparse.RawTextHelpFormatter) history_parser_group = history_parser.add_mutually_exclusive_group() 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 |