Skip to content

Commit d8ade12

Browse files
committed
Remove do_save() and do_run() for #252
1 parent 60890fa commit d8ade12

File tree

5 files changed

+20
-172
lines changed

5 files changed

+20
-172
lines changed

cmd2.py

Lines changed: 8 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -1716,7 +1716,7 @@ def do_ipy(self, arg):
17161716
def do_history(self, args):
17171717
# If an argument was supplied, then retrieve partial contents of the
17181718
# history
1719-
cowerdly_refuse_to_run = False
1719+
cowardly_refuse_to_run = False
17201720
if args.arg:
17211721
# If a character indicating a slice is present, retrieve
17221722
# a slice of the history
@@ -1732,15 +1732,18 @@ def do_history(self, args):
17321732
history = self.history.get(arg)
17331733
else:
17341734
# If no arg given, then retrieve the entire history
1735-
cowerdly_refuse_to_run = True
1735+
cowardly_refuse_to_run = True
17361736
history = self.history
17371737

17381738
if args.run:
1739-
if cowerdly_refuse_to_run:
1740-
self.perror("Cowerdly refusing to run all previously entered commands.", traceback_war=False)
1739+
if cowardly_refuse_to_run:
1740+
self.perror("Cowardly refusing to run all previously entered commands.", traceback_war=False)
17411741
self.perror("If this is what you want to do, specify '1:' as the range of history.", traceback_war=False)
17421742
else:
1743-
self.cmdqueue.extend(history)
1743+
for runme in history:
1744+
self.pfeedback(runme)
1745+
if runme:
1746+
return self.onecmd_plus_hooks(runme)
17441747
elif args.edit:
17451748
fd, fname = tempfile.mkstemp(suffix='.txt', text=True)
17461749
with os.fdopen(fd, 'w') as fobj:
@@ -1860,50 +1863,6 @@ def do_edit(self, arglist):
18601863
if delete_tempfile:
18611864
os.remove(filename)
18621865

1863-
saveparser = (pyparsing.Optional(pyparsing.Word(pyparsing.nums) ^ '*')("idx") +
1864-
pyparsing.Optional(pyparsing.Word(legalChars + '/\\'))("fname") +
1865-
pyparsing.stringEnd)
1866-
1867-
def do_save(self, arg):
1868-
"""Saves command(s) from history to file.
1869-
1870-
Usage: save [N] [file_path]
1871-
1872-
* N - Number of command (from history), or `*` for all commands in history (default: last command)
1873-
* file_path - location to save script of command(s) to (default: value stored in temporary file)"""
1874-
try:
1875-
args = self.saveparser.parseString(arg)
1876-
except pyparsing.ParseException:
1877-
self.perror('Could not understand save target %s' % arg, traceback_war=False)
1878-
raise SyntaxError(self.do_save.__doc__)
1879-
1880-
# If a filename was supplied then use that, otherwise use a temp file
1881-
if args.fname:
1882-
fname = args.fname
1883-
else:
1884-
fd, fname = tempfile.mkstemp(suffix='.txt', text=True)
1885-
os.close(fd)
1886-
1887-
if args.idx == '*':
1888-
saveme = '\n\n'.join(self.history[:])
1889-
elif args.idx:
1890-
saveme = self.history[int(args.idx) - 1]
1891-
else:
1892-
# Wrap in try to deal with case of empty history
1893-
try:
1894-
# Since this save command has already been added to history, need to go one more back for previous
1895-
saveme = self.history[-2]
1896-
except IndexError:
1897-
self.perror('History is empty, nothing to save.', traceback_war=False)
1898-
return
1899-
try:
1900-
f = open(os.path.expanduser(fname), 'w')
1901-
f.write(saveme)
1902-
f.close()
1903-
self.pfeedback('Saved to {}'.format(fname))
1904-
except Exception as e:
1905-
self.perror('Saving {!r} - {}'.format(fname, e), traceback_war=False)
1906-
19071866
@property
19081867
def _current_script_dir(self):
19091868
"""Accessor to get the current script directory from the _script_dir LIFO queue."""
@@ -1990,17 +1949,6 @@ def do_load(self, arglist):
19901949

19911950
self._script_dir.append(os.path.dirname(expanded_path))
19921951

1993-
def do_run(self, arg):
1994-
"""run [arg]: re-runs an earlier command
1995-
1996-
no arg -> run most recent command
1997-
arg is integer -> run one history item, by index
1998-
arg is string -> run most recent command by string search
1999-
arg is /enclosed in forward-slashes/ -> run most recent by regex"""
2000-
runme = self._last_matching(arg)
2001-
self.pfeedback(runme)
2002-
if runme:
2003-
return self.onecmd_plus_hooks(runme)
20041952

20051953
@staticmethod
20061954
def is_text_file(file_path):

tests/conftest.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
# Help text for base cmd2.Cmd application
1616
BASE_HELP = """Documented commands (type help <topic>):
1717
========================================
18-
edit help history load py pyscript quit run save set shell shortcuts
18+
edit help history load py pyscript quit set shell shortcuts
1919
"""
2020

2121
# Help text for the history command

tests/test_cmd2.py

Lines changed: 4 additions & 104 deletions
Original file line numberDiff line numberDiff line change
@@ -356,12 +356,9 @@ def test_history_run_all_commands(base_app):
356356
assert out == []
357357

358358
def test_history_run_one_command(base_app):
359-
run_cmd(base_app, 'help')
360-
# because of the way run_cmd works, it will not
361-
# process the cmdqueue. So we can check to see
362-
# if the cmdqueue has a waiting item
363-
run_cmd(base_app, 'history -r 1')
364-
assert len(base_app.cmdqueue) == 1
359+
expected = run_cmd(base_app, 'help')
360+
output = run_cmd(base_app, 'history -r 1')
361+
assert output == expected
365362

366363
def test_base_load(base_app, request):
367364
test_dir = os.path.dirname(request.module.__file__)
@@ -525,88 +522,6 @@ def test_relative_load_requires_an_argument(base_app, capsys):
525522
assert base_app.cmdqueue == []
526523

527524

528-
def test_base_save(base_app):
529-
# TODO: Use a temporary directory for the file
530-
filename = 'deleteme.txt'
531-
base_app.feedback_to_output = True
532-
run_cmd(base_app, 'help')
533-
run_cmd(base_app, 'help save')
534-
535-
# Test the * form of save which saves all commands from history
536-
out = run_cmd(base_app, 'save * {}'.format(filename))
537-
assert out == normalize('Saved to {}\n'.format(filename))
538-
expected = normalize("""
539-
help
540-
541-
help save
542-
543-
save * deleteme.txt
544-
""")
545-
with open(filename) as f:
546-
content = normalize(f.read())
547-
assert content == expected
548-
549-
# Test the N form of save which saves a numbered command from history
550-
out = run_cmd(base_app, 'save 1 {}'.format(filename))
551-
assert out == normalize('Saved to {}\n'.format(filename))
552-
expected = normalize('help')
553-
with open(filename) as f:
554-
content = normalize(f.read())
555-
assert content == expected
556-
557-
# Test the blank form of save which saves the most recent command from history
558-
out = run_cmd(base_app, 'save {}'.format(filename))
559-
assert out == normalize('Saved to {}\n'.format(filename))
560-
expected = normalize('save 1 {}'.format(filename))
561-
with open(filename) as f:
562-
content = normalize(f.read())
563-
assert content == expected
564-
565-
# Delete file that was created
566-
os.remove(filename)
567-
568-
def test_save_parse_error(base_app, capsys):
569-
invalid_file = '~!@'
570-
run_cmd(base_app, 'save {}'.format(invalid_file))
571-
out, err = capsys.readouterr()
572-
assert out == ''
573-
assert err.startswith('ERROR: Could not understand save target {}\n'.format(invalid_file))
574-
575-
def test_save_tempfile(base_app):
576-
# Just run help to make sure there is something in the history
577-
base_app.feedback_to_output = True
578-
run_cmd(base_app, 'help')
579-
out = run_cmd(base_app, 'save *')
580-
output = out[0]
581-
assert output.startswith('Saved to ')
582-
583-
# Delete the tempfile which was created
584-
temp_file = output.split('Saved to ')[1].strip()
585-
os.remove(temp_file)
586-
587-
def test_save_invalid_history_index(base_app, capsys):
588-
run_cmd(base_app, 'save 5')
589-
out, err = capsys.readouterr()
590-
assert out == ''
591-
assert err.startswith("EXCEPTION of type 'IndexError' occurred with message: 'list index out of range'\n")
592-
593-
def test_save_empty_history_and_index(base_app, capsys):
594-
run_cmd(base_app, 'save')
595-
out, err = capsys.readouterr()
596-
assert out == ''
597-
assert err.startswith("ERROR: History is empty, nothing to save.\n")
598-
599-
def test_save_invalid_path(base_app, capsys):
600-
# Just run help to make sure there is something in the history
601-
run_cmd(base_app, 'help')
602-
603-
invalid_path = '/no_such_path/foobar.txt'
604-
run_cmd(base_app, 'save {}'.format(invalid_path))
605-
out, err = capsys.readouterr()
606-
assert out == ''
607-
assert err.startswith("ERROR: Saving '{}' - ".format(invalid_path))
608-
609-
610525
def test_output_redirection(base_app):
611526
fd, filename = tempfile.mkstemp(prefix='cmd2_test', suffix='.txt')
612527
os.close(fd)
@@ -1144,8 +1059,7 @@ def test_custom_help_menu(help_app):
11441059
expected = normalize("""
11451060
Documented commands (type help <topic>):
11461061
========================================
1147-
edit history py quit save shell squat
1148-
help load pyscript run set shortcuts
1062+
edit help history load py pyscript quit set shell shortcuts squat
11491063
11501064
Undocumented commands:
11511065
======================
@@ -1406,20 +1320,6 @@ def test_clipboard_failure(capsys):
14061320
assert 'Cannot redirect to paste buffer; install ``xclip`` and re-run to enable' in err
14071321

14081322

1409-
def test_run_command_with_empty_arg(base_app):
1410-
command = 'help'
1411-
base_app.feedback_to_output = True
1412-
run_cmd(base_app, command)
1413-
out = run_cmd(base_app, 'run')
1414-
expected = normalize('{}\n\n'.format(command) + BASE_HELP)
1415-
assert out == expected
1416-
1417-
def test_run_command_with_empty_history(base_app):
1418-
base_app.feedback_to_output = True
1419-
out = run_cmd(base_app, 'run')
1420-
assert out == []
1421-
1422-
14231323
class CmdResultApp(cmd2.Cmd):
14241324
def __init__(self, *args, **kwargs):
14251325
# Need to use this older form of invoking super class constructor to support Python 2.x and Python 3.x

tests/test_transcript.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -130,8 +130,8 @@ def test_base_with_transcript(_cmdline_app):
130130
131131
Documented commands (type help <topic>):
132132
========================================
133-
edit history mumble py quit save set shortcuts
134-
help load orate pyscript run say shell speak
133+
edit history mumble py quit set shortcuts
134+
help load orate pyscript say shell speak
135135
136136
(Cmd) help say
137137
Repeats what you tell me to.
@@ -172,7 +172,7 @@ def test_base_with_transcript(_cmdline_app):
172172
set maxrepeats 5
173173
-------------------------[6]
174174
say -ps --repeat=5 goodnight, Gracie
175-
(Cmd) run 4
175+
(Cmd) history -r 4
176176
OODNIGHT, GRACIEGAY
177177
OODNIGHT, GRACIEGAY
178178
OODNIGHT, GRACIEGAY

tests/transcripts/from_cmdloop.txt

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@
55

66
Documented commands (type help <topic>):
77
========================================
8-
edit history mumble py quit save set shortcuts/ */
9-
help load orate pyscript run say shell speak/ */
8+
edit history mumble py quit set shortcuts/ */
9+
help load orate pyscript say shell speak/ */
1010

1111
(Cmd) help say
1212
Repeats what you tell me to.
@@ -34,7 +34,7 @@ OODNIGHT, GRACIEGAY
3434
OODNIGHT, GRACIEGAY
3535
OODNIGHT, GRACIEGAY
3636
OODNIGHT, GRACIEGAY
37-
(Cmd) hi
37+
(Cmd) history
3838
-------------------------[1]
3939
help
4040
-------------------------[2]
@@ -47,7 +47,7 @@ say -ps --repeat=5 goodnight, Gracie
4747
set maxrepeats 5
4848
-------------------------[6]
4949
say -ps --repeat=5 goodnight, Gracie
50-
(Cmd) run 4
50+
(Cmd) history -r 4
5151
say -ps --repeat=5 goodnight, Gracie
5252
OODNIGHT, GRACIEGAY
5353
OODNIGHT, GRACIEGAY

0 commit comments

Comments
 (0)