Skip to content

Commit b1873c3

Browse files
authored
Merge pull request #795 from python-cmd2/history_edit
do_history no longer calls do_edit
2 parents 406f017 + 1aa8d45 commit b1873c3

File tree

3 files changed

+21
-27
lines changed

3 files changed

+21
-27
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
* Enhancements
66
* Send all startup script paths to run_script. Previously we didn't do this if the file was empty, but that
77
showed no record of the run_script command in history.
8+
* Made it easier for developers to override `edit` command by having `do_history` no longer call `do_edit`. This
9+
also removes the need to exclude `edit` command from history list.
810

911
## 0.9.19 (October 14, 2019)
1012
* Bug Fixes

cmd2/cmd2.py

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -433,7 +433,7 @@ def __init__(self, completekey: str = 'tab', stdin=None, stdout=None, *,
433433
self._initialize_history(persistent_history_file)
434434

435435
# Commands to exclude from the history command
436-
self.exclude_from_history = '''history edit eof'''.split()
436+
self.exclude_from_history = ['eof', 'history']
437437

438438
# Dictionary of macro names and their values
439439
self.macros = dict()
@@ -3520,14 +3520,9 @@ def do_history(self, args: argparse.Namespace) -> Optional[bool]:
35203520
else:
35213521
fobj.write('{}\n'.format(command.raw))
35223522
try:
3523-
# Handle potential edge case where the temp file needs to be quoted on the command line
3524-
quoted_fname = utils.quote_string(fname)
3525-
3526-
# noinspection PyTypeChecker
3527-
self.do_edit(quoted_fname)
3528-
3523+
self._run_editor(fname)
35293524
# noinspection PyTypeChecker
3530-
self.do_run_script(quoted_fname)
3525+
self.do_run_script(utils.quote_string(fname))
35313526
finally:
35323527
os.remove(fname)
35333528
elif args.output_file:
@@ -3719,25 +3714,33 @@ def _generate_transcript(self, history: List[Union[HistoryItem, str]], transcrip
37193714
msg = '{} {} saved to transcript file {!r}'
37203715
self.pfeedback(msg.format(commands_run, plural, transcript_file))
37213716

3722-
edit_description = ("Edit a file in a text editor\n"
3717+
edit_description = ("Run a text editor and optionally open a file with it\n"
37233718
"\n"
37243719
"The editor used is determined by a settable parameter. To set it:\n"
37253720
"\n"
37263721
" set editor (program-name)")
37273722

37283723
edit_parser = Cmd2ArgumentParser(description=edit_description)
37293724
edit_parser.add_argument('file_path', nargs=argparse.OPTIONAL,
3730-
help="path to a file to open in editor", completer_method=path_complete)
3725+
help="optional path to a file to open in editor", completer_method=path_complete)
37313726

37323727
@with_argparser(edit_parser)
37333728
def do_edit(self, args: argparse.Namespace) -> None:
3734-
"""Edit a file in a text editor"""
3729+
"""Run a text editor and optionally open a file with it"""
3730+
self._run_editor(args.file_path)
3731+
3732+
def _run_editor(self, file_path: Optional[str]) -> None:
3733+
"""
3734+
Run a text editor and optionally open a file with it
3735+
:param file_path: optional path of the file to edit
3736+
:raises EnvironmentError if self.editor is not set
3737+
"""
37353738
if not self.editor:
37363739
raise EnvironmentError("Please use 'set editor' to specify your text editing program of choice.")
37373740

37383741
command = utils.quote_string(os.path.expanduser(self.editor))
3739-
if args.file_path:
3740-
command += " " + utils.quote_string(os.path.expanduser(args.file_path))
3742+
if file_path:
3743+
command += " " + utils.quote_string(os.path.expanduser(file_path))
37413744

37423745
# noinspection PyTypeChecker
37433746
self.do_shell(command)

tests/test_history.py

Lines changed: 3 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -476,9 +476,9 @@ def test_history_edit(base_app, monkeypatch):
476476
# going to call it due to the mock
477477
base_app.editor = 'fooedit'
478478

479-
# Mock out the edit call so we don't actually open an editor
480-
edit_mock = mock.MagicMock(name='do_edit')
481-
monkeypatch.setattr("cmd2.Cmd.do_edit", edit_mock)
479+
# Mock out the _run_editor call so we don't actually open an editor
480+
edit_mock = mock.MagicMock(name='_run_editor')
481+
monkeypatch.setattr("cmd2.Cmd._run_editor", edit_mock)
482482

483483
# Mock out the run_script call since the mocked edit won't produce a file
484484
run_script_mock = mock.MagicMock(name='do_run_script')
@@ -590,17 +590,6 @@ def test_base_help_history(base_app):
590590
assert out == normalize(HELP_HISTORY)
591591

592592
def test_exclude_from_history(base_app, monkeypatch):
593-
# Set a fake editor just to make sure we have one. We aren't
594-
# really going to call it due to the mock
595-
base_app.editor = 'fooedit'
596-
597-
# Mock out the subprocess.Popen call so we don't actually open an editor
598-
m = mock.MagicMock(name='Popen')
599-
monkeypatch.setattr("subprocess.Popen", m)
600-
601-
# Run edit command
602-
run_cmd(base_app, 'edit')
603-
604593
# Run history command
605594
run_cmd(base_app, 'history')
606595

0 commit comments

Comments
 (0)