Skip to content

Commit ab8194e

Browse files
committed
Some more pyscripting tweaks. Fixed issue with capturing ppaged output. Added pyscript bridge to ipy command. Saving progress.
1 parent c051c84 commit ab8194e

File tree

2 files changed

+32
-7
lines changed

2 files changed

+32
-7
lines changed

cmd2/cmd2.py

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2927,9 +2927,21 @@ def do_ipy(self, arg):
29272927
Run python code from external files with ``run filename.py``
29282928
End with ``Ctrl-D`` (Unix) / ``Ctrl-Z`` (Windows), ``quit()``, '`exit()``.
29292929
"""
2930-
banner = 'Entering an embedded IPython shell type quit() or <Ctrl>-d to exit ...'
2931-
exit_msg = 'Leaving IPython, back to {}'.format(sys.argv[0])
2932-
embed(banner1=banner, exit_msg=exit_msg)
2930+
from .pyscript_bridge import PyscriptBridge
2931+
bridge = PyscriptBridge(self)
2932+
2933+
if self.locals_in_py:
2934+
def load_ipy(self, app):
2935+
banner = 'Entering an embedded IPython shell type quit() or <Ctrl>-d to exit ...'
2936+
exit_msg = 'Leaving IPython, back to {}'.format(sys.argv[0])
2937+
embed(banner1=banner, exit_msg=exit_msg)
2938+
load_ipy(self, bridge)
2939+
else:
2940+
def load_ipy(app):
2941+
banner = 'Entering an embedded IPython shell type quit() or <Ctrl>-d to exit ...'
2942+
exit_msg = 'Leaving IPython, back to {}'.format(sys.argv[0])
2943+
embed(banner1=banner, exit_msg=exit_msg)
2944+
load_ipy(bridge)
29332945

29342946
history_parser = argparse.ArgumentParser(formatter_class=argparse.RawTextHelpFormatter)
29352947
history_parser_group = history_parser.add_mutually_exclusive_group()

cmd2/pyscript_bridge.py

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -55,22 +55,35 @@ def read(self):
5555
def clear(self):
5656
self.buffer = ''
5757

58+
def __getattr__(self, item):
59+
if item in self.__dict__:
60+
return self.__dict__[item]
61+
else:
62+
return getattr(self.inner_stream, item)
63+
5864

5965
def _exec_cmd(cmd2_app, func):
6066
"""Helper to encapsulate executing a command and capturing the results"""
6167
copy_stdout = CopyStream(sys.stdout)
6268
copy_stderr = CopyStream(sys.stderr)
6369

70+
copy_cmd_stdout = CopyStream(cmd2_app.stdout)
71+
6472
cmd2_app._last_result = None
6573

66-
with redirect_stdout(copy_stdout):
67-
with redirect_stderr(copy_stderr):
68-
func()
74+
try:
75+
cmd2_app.stdout = copy_cmd_stdout
76+
with redirect_stdout(copy_stdout):
77+
with redirect_stderr(copy_stderr):
78+
func()
79+
finally:
80+
cmd2_app.stdout = copy_cmd_stdout.inner_stream
6981

7082
# if stderr is empty, set it to None
7183
stderr = copy_stderr if copy_stderr.buffer else None
7284

73-
result = CommandResult(stdout=copy_stdout.buffer, stderr=stderr, data=cmd2_app._last_result)
85+
outbuf = copy_cmd_stdout.buffer if copy_cmd_stdout.buffer else copy_stdout.buffer
86+
result = CommandResult(stdout=outbuf, stderr=stderr, data=cmd2_app._last_result)
7487
return result
7588

7689

0 commit comments

Comments
 (0)