summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorKevin Van Brunt <kmvanbrunt@gmail.com>2019-05-24 14:23:54 -0400
committerKevin Van Brunt <kmvanbrunt@gmail.com>2019-05-24 14:23:54 -0400
commit1178bda4d68608a2cd6e34a6bd60639debc92cf1 (patch)
tree2e609a057d48848503e5c8f21c99144a8f7f7fee
parenta37dd5daf8914ce590440c2ccb2cf45e71a4170b (diff)
downloadcmd2-git-1178bda4d68608a2cd6e34a6bd60639debc92cf1.tar.gz
Since transcript generation actually runs the commands, it now returns whether a command returned stop.
This means the command loop will stop once transcript generation completes if a command did return stop. Otherwise there is a risk in continuing to run if the application's state has already been marked to close.
-rw-r--r--cmd2/cmd2.py14
1 files changed, 10 insertions, 4 deletions
diff --git a/cmd2/cmd2.py b/cmd2/cmd2.py
index 6efdda2c..a4d84adf 100644
--- a/cmd2/cmd2.py
+++ b/cmd2/cmd2.py
@@ -3440,14 +3440,17 @@ class Cmd(cmd.Cmd):
except Exception as e:
self.perror('Saving {!r} - {}'.format(args.output_file, e), traceback_war=False)
elif args.transcript:
- self._generate_transcript(history, args.transcript)
+ return self._generate_transcript(history, args.transcript)
else:
# Display the history items retrieved
for hi in history:
self.poutput(hi.pr(script=args.script, expanded=args.expanded, verbose=args.verbose))
- def _generate_transcript(self, history: List[Union[HistoryItem, str]], transcript_file: str) -> None:
- """Generate a transcript file from a given history of commands."""
+ def _generate_transcript(self, history: List[Union[HistoryItem, str]], transcript_file: str) -> Optional[bool]:
+ """
+ Generate a transcript file from a given history of commands
+ :return: True if running of commands should stop
+ """
import io
# Validate the transcript file path to make sure directory exists and write access is available
transcript_path = os.path.abspath(os.path.expanduser(transcript_file))
@@ -3458,6 +3461,7 @@ class Cmd(cmd.Cmd):
return
commands_run = 0
+ stop = False
try:
with self.sigint_protection:
# Disable echo while we manually redirect stdout to a StringIO buffer
@@ -3527,6 +3531,8 @@ class Cmd(cmd.Cmd):
msg = '{} {} saved to transcript file {!r}'
self.pfeedback(msg.format(commands_run, plural, transcript_file))
+ return stop
+
edit_description = ("Edit a file in a text editor\n"
"\n"
"The editor used is determined by a settable parameter. To set it:\n"
@@ -3614,7 +3620,7 @@ class Cmd(cmd.Cmd):
self._script_dir.append(os.path.dirname(expanded_path))
if args.transcript:
- self._generate_transcript(script_commands, os.path.expanduser(args.transcript))
+ return self._generate_transcript(script_commands, os.path.expanduser(args.transcript))
else:
return self.runcmds_plus_hooks(script_commands)