summaryrefslogtreecommitdiff
path: root/cmd2/pyscript_bridge.py
diff options
context:
space:
mode:
authorKevin Van Brunt <kmvanbrunt@gmail.com>2019-06-14 18:12:39 -0400
committerKevin Van Brunt <kmvanbrunt@gmail.com>2019-06-14 18:12:39 -0400
commit85dbe035114330a15abd5af6e098ef6948fa6cb0 (patch)
treee087dfa160eba882dcbd623609cab763efbd1ebc /cmd2/pyscript_bridge.py
parent3329e5fec21f2cc4479bb3358170e2226885ab3b (diff)
parentddd07f9cd6d72baca1232ae98856cf3b3d564706 (diff)
downloadcmd2-git-85dbe035114330a15abd5af6e098ef6948fa6cb0.tar.gz
Merge branch 'master' into history_fixes
Diffstat (limited to 'cmd2/pyscript_bridge.py')
-rw-r--r--cmd2/pyscript_bridge.py27
1 files changed, 16 insertions, 11 deletions
diff --git a/cmd2/pyscript_bridge.py b/cmd2/pyscript_bridge.py
index 1c720cf9..e5633173 100644
--- a/cmd2/pyscript_bridge.py
+++ b/cmd2/pyscript_bridge.py
@@ -2,9 +2,6 @@
"""
Bridges calls made inside of pyscript with the Cmd2 host app while maintaining a reasonable
degree of isolation between the two
-
-Copyright 2018 Eric Lin <anselor@gmail.com>
-Released under MIT license, see LICENSE file
"""
import sys
@@ -20,17 +17,18 @@ else:
from contextlib import redirect_stdout, redirect_stderr
-class CommandResult(namedtuple_with_defaults('CommandResult', ['stdout', 'stderr', 'data'])):
- """Encapsulates the results from a command.
+class CommandResult(namedtuple_with_defaults('CommandResult', ['stdout', 'stderr', 'stop', 'data'])):
+ """Encapsulates the results from a cmd2 app command
Named tuple attributes
----------------------
- stdout: str - Output captured from stdout while this command is executing
- stderr: str - Output captured from stderr while this command is executing. None if no error captured.
- data - Data returned by the command.
+ stdout: str - output captured from stdout while this command is executing
+ stderr: str - output captured from stderr while this command is executing. None if no error captured.
+ stop: bool - return value of onecmd_plus_hooks after it runs the given command line.
+ data - possible data populated by the command.
Any combination of these fields can be used when developing a scripting API for a given command.
- By default stdout and stderr will be captured for you. If there is additional command specific data,
+ By default stdout, stderr, and stop will be captured for you. If there is additional command specific data,
then write that to cmd2's _last_result member. That becomes the data member of this tuple.
In some cases, the data member may contain everything needed for a command and storing stdout
@@ -67,6 +65,9 @@ class PyscriptBridge(object):
self._cmd2_app = cmd2_app
self.cmd_echo = False
+ # Tells if any of the commands run via __call__ returned True for stop
+ self.stop = False
+
def __dir__(self):
"""Return a custom set of attribute names"""
attributes = []
@@ -95,16 +96,20 @@ class PyscriptBridge(object):
self._cmd2_app._last_result = None
+ stop = False
try:
self._cmd2_app.stdout = copy_cmd_stdout
with redirect_stdout(copy_cmd_stdout):
with redirect_stderr(copy_stderr):
- self._cmd2_app.onecmd_plus_hooks(command, pyscript_bridge_call=True)
+ stop = self._cmd2_app.onecmd_plus_hooks(command, pyscript_bridge_call=True)
finally:
- self._cmd2_app.stdout = copy_cmd_stdout.inner_stream
+ with self._cmd2_app.sigint_protection:
+ self._cmd2_app.stdout = copy_cmd_stdout.inner_stream
+ self.stop = stop or self.stop
# Save the output. If stderr is empty, set it to None.
result = CommandResult(stdout=copy_cmd_stdout.getvalue(),
stderr=copy_stderr.getvalue() if copy_stderr.getvalue() else None,
+ stop=stop,
data=self._cmd2_app._last_result)
return result