summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--CHANGELOG.md1
-rw-r--r--cmd2/cmd2.py24
-rw-r--r--tests/test_transcript.py4
3 files changed, 13 insertions, 16 deletions
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 694d2786..32cebfc5 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -9,6 +9,7 @@
* The `with_argparser` decorators now add the Statement object created when parsing the command line to the
`argparse.Namespace` object they pass to the `do_*` methods. It is stored in an attribute called `__statement__`.
This can be useful if a command function needs to know the command line for things like logging.
+ * Added a `-r` option to the `load` command for automatically generating a transcript based on a script file
* Potentially breaking changes
* The following commands now write to stderr instead of stdout when printing an error. This will make catching
errors easier in pyscript.
diff --git a/cmd2/cmd2.py b/cmd2/cmd2.py
index b66869c1..46984326 100644
--- a/cmd2/cmd2.py
+++ b/cmd2/cmd2.py
@@ -3341,9 +3341,6 @@ class Cmd(cmd.Cmd):
except Exception as e:
self.perror('Saving {!r} - {}'.format(args.output_file, e), traceback_war=False)
elif args.transcript:
- if self.redirecting:
- self.perror("Redirection not supported while using history -t", traceback_war=False)
- return
self._generate_transcript(history, args.transcript)
else:
# Display the history items retrieved
@@ -3353,6 +3350,16 @@ class Cmd(cmd.Cmd):
def _generate_transcript(self, history: List[Union[HistoryItem, str]], transcript_file: str) -> None:
"""Generate a transcript file from a given history of commands."""
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))
+ transcript_dir = os.path.dirname(transcript_path)
+ if not os.path.isdir(transcript_dir):
+ self.perror("Transcript directory {!r} is not a directory".format(transcript_dir), traceback_war=False)
+ return
+ if not os.access(transcript_dir, os.W_OK):
+ self.perror("No write access for transcript directory {!r}".format(transcript_dir), traceback_war=False)
+ return
+
# Disable echo while we manually redirect stdout to a StringIO buffer
saved_echo = self.echo
saved_stdout = self.stdout
@@ -3505,17 +3512,6 @@ class Cmd(cmd.Cmd):
return
if args.record_transcript:
- if self.redirecting:
- self.perror("Redirection not supported while using load -r", traceback_war=False)
- return
- transcript_path = os.path.abspath(os.path.expanduser(args.record_transcript))
- transcript_dir = os.path.dirname(transcript_path)
- if not os.path.isdir(transcript_dir):
- self.perror("{!r} is not a directory".format(transcript_dir), traceback_war=False)
- return
- if not os.access(transcript_dir, os.W_OK):
- self.perror("You do not have write access to directory '{!r}".format(transcript_dir), traceback_war=False)
- return
self._generate_transcript(script_commands, os.path.expanduser(args.record_transcript))
return
diff --git a/tests/test_transcript.py b/tests/test_transcript.py
index 709648fc..6c9b8a20 100644
--- a/tests/test_transcript.py
+++ b/tests/test_transcript.py
@@ -136,7 +136,7 @@ def test_transcript(request, capsys, filename, feedback_to_output):
assert err.startswith(expected_start)
assert err.endswith(expected_end)
-def test_history_transcript(request, capsys):
+def test_history_transcript():
app = CmdLineApp()
app.stdout = StdSim(app.stdout)
run_cmd(app, 'orate this is\na /multiline/\ncommand;\n')
@@ -163,7 +163,7 @@ this is a \/multiline\/ command
assert xscript == expected
-def test_history_transcript_bad_filename(request, capsys):
+def test_history_transcript_bad_filename():
app = CmdLineApp()
app.stdout = StdSim(app.stdout)
run_cmd(app, 'orate this is\na /multiline/\ncommand;\n')