Skip to content

Commit 5a58199

Browse files
committed
Merge branch 'master' into set_prog
# Conflicts: # CHANGELOG.md
2 parents b3edff1 + b1873c3 commit 5a58199

File tree

3 files changed

+26
-29
lines changed

3 files changed

+26
-29
lines changed

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,12 @@
11
## 0.9.20 (TBD, 2019)
2+
* Bug Fixes
3+
* Fixed bug where setting `use_ipython` to False removed ipy command from the entire `cmd2.Cmd` class instead of
4+
just the instance being created
25
* Enhancements
36
* Send all startup script paths to run_script. Previously we didn't do this if the file was empty, but that
47
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.
510
* It is no longer necessary to set the `prog` attribute of an argparser with subcommands. cmd2 now automatically
611
sets the prog value of it and all its subparsers so that all usage statements contain the top level command name
712
and not sys.argv[0].

cmd2/cmd2.py

Lines changed: 18 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -390,10 +390,10 @@ def __init__(self, completekey: str = 'tab', stdin=None, stdout=None, *,
390390
:param shortcuts: dictionary containing shortcuts for commands. If not supplied, then defaults to
391391
constants.DEFAULT_SHORTCUTS.
392392
"""
393-
# If use_ipython is False, make sure the do_ipy() method doesn't exit
393+
# If use_ipython is False, make sure the ipy command isn't available in this instance
394394
if not use_ipython:
395395
try:
396-
del Cmd.do_ipy
396+
self.do_ipy = None
397397
except AttributeError:
398398
pass
399399

@@ -455,7 +455,7 @@ def __init__(self, completekey: str = 'tab', stdin=None, stdout=None, *,
455455
self._initialize_history(persistent_history_file)
456456

457457
# Commands to exclude from the history command
458-
self.exclude_from_history = '''history edit eof'''.split()
458+
self.exclude_from_history = ['eof', 'history']
459459

460460
# Dictionary of macro names and their values
461461
self.macros = dict()
@@ -3542,14 +3542,9 @@ def do_history(self, args: argparse.Namespace) -> Optional[bool]:
35423542
else:
35433543
fobj.write('{}\n'.format(command.raw))
35443544
try:
3545-
# Handle potential edge case where the temp file needs to be quoted on the command line
3546-
quoted_fname = utils.quote_string(fname)
3547-
3548-
# noinspection PyTypeChecker
3549-
self.do_edit(quoted_fname)
3550-
3545+
self._run_editor(fname)
35513546
# noinspection PyTypeChecker
3552-
self.do_run_script(quoted_fname)
3547+
self.do_run_script(utils.quote_string(fname))
35533548
finally:
35543549
os.remove(fname)
35553550
elif args.output_file:
@@ -3741,25 +3736,33 @@ def _generate_transcript(self, history: List[Union[HistoryItem, str]], transcrip
37413736
msg = '{} {} saved to transcript file {!r}'
37423737
self.pfeedback(msg.format(commands_run, plural, transcript_file))
37433738

3744-
edit_description = ("Edit a file in a text editor\n"
3739+
edit_description = ("Run a text editor and optionally open a file with it\n"
37453740
"\n"
37463741
"The editor used is determined by a settable parameter. To set it:\n"
37473742
"\n"
37483743
" set editor (program-name)")
37493744

37503745
edit_parser = Cmd2ArgumentParser(description=edit_description)
37513746
edit_parser.add_argument('file_path', nargs=argparse.OPTIONAL,
3752-
help="path to a file to open in editor", completer_method=path_complete)
3747+
help="optional path to a file to open in editor", completer_method=path_complete)
37533748

37543749
@with_argparser(edit_parser)
37553750
def do_edit(self, args: argparse.Namespace) -> None:
3756-
"""Edit a file in a text editor"""
3751+
"""Run a text editor and optionally open a file with it"""
3752+
self._run_editor(args.file_path)
3753+
3754+
def _run_editor(self, file_path: Optional[str]) -> None:
3755+
"""
3756+
Run a text editor and optionally open a file with it
3757+
:param file_path: optional path of the file to edit
3758+
:raises EnvironmentError if self.editor is not set
3759+
"""
37573760
if not self.editor:
37583761
raise EnvironmentError("Please use 'set editor' to specify your text editing program of choice.")
37593762

37603763
command = utils.quote_string(os.path.expanduser(self.editor))
3761-
if args.file_path:
3762-
command += " " + utils.quote_string(os.path.expanduser(args.file_path))
3764+
if file_path:
3765+
command += " " + utils.quote_string(os.path.expanduser(file_path))
37633766

37643767
# noinspection PyTypeChecker
37653768
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)