Skip to content

Commit fd5750f

Browse files
committed
do_edit() no longer edits history, just files #252
1 parent d8ade12 commit fd5750f

File tree

4 files changed

+5
-153
lines changed

4 files changed

+5
-153
lines changed

cmd2.py

Lines changed: 5 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -553,7 +553,6 @@ class Cmd(cmd.Cmd):
553553

554554
# Attributes which ARE dynamically settable at runtime
555555
abbrev = False # Abbreviated commands recognized
556-
autorun_on_edit = False # Should files automatically run after editing (doesn't apply to commands)
557556
colors = (platform.system() != 'Windows')
558557
continuation_prompt = '> '
559558
debug = False
@@ -575,7 +574,6 @@ class Cmd(cmd.Cmd):
575574
# To make an attribute settable with the "do_set" command, add it to this ...
576575
# This starts out as a dictionary but gets converted to an OrderedDict sorted alphabetically by key
577576
settable = {'abbrev': 'Accept abbreviated commands',
578-
'autorun_on_edit': 'Automatically run files after editing',
579577
'colors': 'Colorized output (*nix only)',
580578
'continuation_prompt': 'On 2nd+ line of input',
581579
'debug': 'Show full error stack on error',
@@ -1773,95 +1771,25 @@ def do_history(self, args):
17731771
else:
17741772
self.poutput(hi.pr())
17751773

1776-
def _last_matching(self, arg):
1777-
"""Return the last item from the history list that matches arg. Or if arg not provided, return last item.
1778-
1779-
If not match is found, return None.
1780-
1781-
:param arg: str - text to search for in history
1782-
:return: str - last match, last item, or None, depending on arg.
1783-
"""
1784-
try:
1785-
if arg:
1786-
return self.history.get(arg)[-1]
1787-
else:
1788-
return self.history[-1]
1789-
except IndexError:
1790-
return None
17911774

17921775
@with_argument_list
17931776
def do_edit(self, arglist):
17941777
"""Edit a file or command in a text editor.
17951778
1796-
Usage: edit [N]|[file_path]
1779+
Usage: edit [file_path]
17971780
Where:
1798-
* N - Number of command (from history), or `*` for all commands in history
1799-
(default: last command)
18001781
* file_path - path to a file to open in editor
18011782
18021783
The editor used is determined by the ``editor`` settable parameter.
18031784
"set editor (program-name)" to change or set the EDITOR environment variable.
1804-
1805-
The optional arguments are mutually exclusive. Either a command number OR a file name can be supplied.
1806-
If neither is supplied, the most recent command in the history is edited.
1807-
1808-
Edited commands are always run after the editor is closed.
1809-
1810-
Edited files are run on close if the ``autorun_on_edit`` settable parameter is True.
18111785
"""
18121786
if not self.editor:
18131787
raise EnvironmentError("Please use 'set editor' to specify your text editing program of choice.")
1814-
filename = None
1815-
if arglist and arglist[0]:
1816-
try:
1817-
# Try to convert argument to an integer
1818-
history_idx = int(arglist[0])
1819-
except ValueError:
1820-
# Argument passed is not convertible to an integer, so treat it as a file path
1821-
filename = arglist[0]
1822-
history_item = ''
1823-
else:
1824-
# Argument passed IS convertible to an integer, so treat it as a history index
1825-
1826-
# Save off original index for pringing
1827-
orig_indx = history_idx
1828-
1829-
# Convert negative index into equivalent positive one
1830-
if history_idx < 0:
1831-
history_idx += len(self.history) + 1
1832-
1833-
# Make sure the index is actually within the history
1834-
if 1 <= history_idx <= len(self.history):
1835-
history_item = self._last_matching(history_idx)
1836-
else:
1837-
self.perror('index {!r} does not exist within the history'.format(orig_indx), traceback_war=False)
1838-
return
1839-
1788+
filename = arglist[0] if arglist else ''
1789+
if filename:
1790+
os.system('"{}" "{}"'.format(self.editor, filename))
18401791
else:
1841-
try:
1842-
history_item = self.history[-1]
1843-
except IndexError:
1844-
self.perror('edit must be called with argument if history is empty', traceback_war=False)
1845-
return
1846-
1847-
delete_tempfile = False
1848-
if history_item:
1849-
if filename is None:
1850-
fd, filename = tempfile.mkstemp(suffix='.txt', text=True)
1851-
os.close(fd)
1852-
delete_tempfile = True
1853-
1854-
f = open(os.path.expanduser(filename), 'w')
1855-
f.write(history_item or '')
1856-
f.close()
1857-
1858-
os.system('"{}" "{}"'.format(self.editor, filename))
1859-
1860-
if self.autorun_on_edit or history_item:
1861-
self.do_load(filename)
1862-
1863-
if delete_tempfile:
1864-
os.remove(filename)
1792+
os.system('"{}"'.format(self.editor))
18651793

18661794
@property
18671795
def _current_script_dir(self):

tests/conftest.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,6 @@
5353
expect_colors = False
5454
# Output from the show command with default settings
5555
SHOW_TXT = """abbrev: False
56-
autorun_on_edit: False
5756
colors: {}
5857
continuation_prompt: >
5958
debug: False
@@ -72,7 +71,6 @@
7271
color_str = 'False'
7372
SHOW_LONG = """
7473
abbrev: False # Accept abbreviated commands
75-
autorun_on_edit: False # Automatically run files after editing
7674
colors: {} # Colorized output (*nix only)
7775
continuation_prompt: > # On 2nd+ line of input
7876
debug: False # Show full error stack on error

tests/test_cmd2.py

Lines changed: 0 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -780,84 +780,11 @@ def test_edit_blank(base_app, monkeypatch):
780780
m = mock.MagicMock(name='system')
781781
monkeypatch.setattr("os.system", m)
782782

783-
# Run help command just so we have a command in history
784-
run_cmd(base_app, 'help')
785-
786-
run_cmd(base_app, 'edit')
787-
788-
# We have an editor, so should expect a system call
789-
m.assert_called_once()
790-
791-
def test_edit_empty_history(base_app, capsys):
792783
run_cmd(base_app, 'edit')
793-
out, err = capsys.readouterr()
794-
assert out == ''
795-
assert err == 'ERROR: edit must be called with argument if history is empty\n'
796-
797-
def test_edit_valid_positive_number(base_app, monkeypatch):
798-
# Set a fake editor just to make sure we have one. We aren't really going to call it due to the mock
799-
base_app.editor = 'fooedit'
800-
801-
# Mock out the os.system call so we don't actually open an editor
802-
m = mock.MagicMock(name='system')
803-
monkeypatch.setattr("os.system", m)
804-
805-
# Run help command just so we have a command in history
806-
run_cmd(base_app, 'help')
807-
808-
run_cmd(base_app, 'edit 1')
809784

810785
# We have an editor, so should expect a system call
811786
m.assert_called_once()
812787

813-
def test_edit_valid_negative_number(base_app, monkeypatch):
814-
# Set a fake editor just to make sure we have one. We aren't really going to call it due to the mock
815-
base_app.editor = 'fooedit'
816-
817-
# Mock out the os.system call so we don't actually open an editor
818-
m = mock.MagicMock(name='system')
819-
monkeypatch.setattr("os.system", m)
820-
821-
# Run help command just so we have a command in history
822-
run_cmd(base_app, 'help')
823-
824-
run_cmd(base_app, 'edit "-1"')
825-
826-
# We have an editor, so should expect a system call
827-
m.assert_called_once()
828-
829-
def test_edit_invalid_positive_number(base_app, monkeypatch):
830-
# Set a fake editor just to make sure we have one. We aren't really going to call it due to the mock
831-
base_app.editor = 'fooedit'
832-
833-
# Mock out the os.system call so we don't actually open an editor
834-
m = mock.MagicMock(name='system')
835-
monkeypatch.setattr("os.system", m)
836-
837-
# Run help command just so we have a command in history
838-
run_cmd(base_app, 'help')
839-
840-
run_cmd(base_app, 'edit 23')
841-
842-
# History index is invalid, so should expect a system call
843-
m.assert_not_called()
844-
845-
def test_edit_invalid_negative_number(base_app, monkeypatch):
846-
# Set a fake editor just to make sure we have one. We aren't really going to call it due to the mock
847-
base_app.editor = 'fooedit'
848-
849-
# Mock out the os.system call so we don't actually open an editor
850-
m = mock.MagicMock(name='system')
851-
monkeypatch.setattr("os.system", m)
852-
853-
# Run help command just so we have a command in history
854-
run_cmd(base_app, 'help')
855-
856-
run_cmd(base_app, 'edit "-23"')
857-
858-
# History index is invalid, so should expect a system call
859-
m.assert_not_called()
860-
861788

862789
def test_base_py_interactive(base_app):
863790
# Mock out the InteractiveConsole.interact() call so we don't actually wait for a user's response on stdin

tests/transcripts/regex_set.txt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55

66
(Cmd) set
77
abbrev: True
8-
autorun_on_edit: False
98
colors: /(True|False)/
109
continuation_prompt: >/ /
1110
debug: False

0 commit comments

Comments
 (0)