Skip to content

Commit e0a307c

Browse files
committed
Updated CHANGELOG
Also: - Removed guard clauses which kmvanbrunt promises will be unecessary with his upcoming change - Moved transcript path validation inside _generate_transcript()
1 parent dcbffdb commit e0a307c

File tree

3 files changed

+13
-16
lines changed

3 files changed

+13
-16
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
* The `with_argparser` decorators now add the Statement object created when parsing the command line to the
1010
`argparse.Namespace` object they pass to the `do_*` methods. It is stored in an attribute called `__statement__`.
1111
This can be useful if a command function needs to know the command line for things like logging.
12+
* Added a `-r` option to the `load` command for automatically generating a transcript based on a script file
1213
* Potentially breaking changes
1314
* The following commands now write to stderr instead of stdout when printing an error. This will make catching
1415
errors easier in pyscript.

cmd2/cmd2.py

Lines changed: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3341,9 +3341,6 @@ def do_history(self, args: argparse.Namespace) -> None:
33413341
except Exception as e:
33423342
self.perror('Saving {!r} - {}'.format(args.output_file, e), traceback_war=False)
33433343
elif args.transcript:
3344-
if self.redirecting:
3345-
self.perror("Redirection not supported while using history -t", traceback_war=False)
3346-
return
33473344
self._generate_transcript(history, args.transcript)
33483345
else:
33493346
# Display the history items retrieved
@@ -3353,6 +3350,16 @@ def do_history(self, args: argparse.Namespace) -> None:
33533350
def _generate_transcript(self, history: List[Union[HistoryItem, str]], transcript_file: str) -> None:
33543351
"""Generate a transcript file from a given history of commands."""
33553352
import io
3353+
# Validate the transcript file path to make sure directory exists and write access is available
3354+
transcript_path = os.path.abspath(os.path.expanduser(transcript_file))
3355+
transcript_dir = os.path.dirname(transcript_path)
3356+
if not os.path.isdir(transcript_dir):
3357+
self.perror("Transcript directory {!r} is not a directory".format(transcript_dir), traceback_war=False)
3358+
return
3359+
if not os.access(transcript_dir, os.W_OK):
3360+
self.perror("No write access for transcript directory {!r}".format(transcript_dir), traceback_war=False)
3361+
return
3362+
33563363
# Disable echo while we manually redirect stdout to a StringIO buffer
33573364
saved_echo = self.echo
33583365
saved_stdout = self.stdout
@@ -3505,17 +3512,6 @@ def do_load(self, args: argparse.Namespace) -> None:
35053512
return
35063513

35073514
if args.record_transcript:
3508-
if self.redirecting:
3509-
self.perror("Redirection not supported while using load -r", traceback_war=False)
3510-
return
3511-
transcript_path = os.path.abspath(os.path.expanduser(args.record_transcript))
3512-
transcript_dir = os.path.dirname(transcript_path)
3513-
if not os.path.isdir(transcript_dir):
3514-
self.perror("{!r} is not a directory".format(transcript_dir), traceback_war=False)
3515-
return
3516-
if not os.access(transcript_dir, os.W_OK):
3517-
self.perror("You do not have write access to directory '{!r}".format(transcript_dir), traceback_war=False)
3518-
return
35193515
self._generate_transcript(script_commands, os.path.expanduser(args.record_transcript))
35203516
return
35213517

tests/test_transcript.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ def test_transcript(request, capsys, filename, feedback_to_output):
136136
assert err.startswith(expected_start)
137137
assert err.endswith(expected_end)
138138

139-
def test_history_transcript(request, capsys):
139+
def test_history_transcript():
140140
app = CmdLineApp()
141141
app.stdout = StdSim(app.stdout)
142142
run_cmd(app, 'orate this is\na /multiline/\ncommand;\n')
@@ -163,7 +163,7 @@ def test_history_transcript(request, capsys):
163163

164164
assert xscript == expected
165165

166-
def test_history_transcript_bad_filename(request, capsys):
166+
def test_history_transcript_bad_filename():
167167
app = CmdLineApp()
168168
app.stdout = StdSim(app.stdout)
169169
run_cmd(app, 'orate this is\na /multiline/\ncommand;\n')

0 commit comments

Comments
 (0)