diff options
| -rwxr-xr-x | cmd2.py | 29 | ||||
| -rw-r--r-- | examples/transcript_regex.txt | 3 | ||||
| -rw-r--r-- | tests/conftest.py | 31 | ||||
| -rw-r--r-- | tests/test_cmd2.py | 10 | ||||
| -rw-r--r-- | tests/test_completion.py | 2 | ||||
| -rw-r--r-- | tests/transcript_regex.txt | 3 |
6 files changed, 42 insertions, 36 deletions
@@ -597,12 +597,11 @@ class Cmd(cmd.Cmd): # Attributes which ARE dynamically settable at runtime abbrev = True # Abbreviated commands recognized - autorun_on_edit = True # Should files automatically run after editing (doesn't apply to commands) + autorun_on_edit = False # Should files automatically run after editing (doesn't apply to commands) case_insensitive = True # Commands recognized regardless of case colors = (platform.system() != 'Windows') continuation_prompt = '> ' debug = False - default_file_name = 'command.txt' # For ``edit`` when called with a history item number and ``save`` echo = False editor = os.environ.get('EDITOR') if not editor: @@ -626,7 +625,6 @@ class Cmd(cmd.Cmd): colors Colorized output (*nix only) continuation_prompt On 2nd+ line of input debug Show full error stack on error - default_file_name For ``edit`` and ``save`` echo Echo command issued into output editor Program used by ``edit`` feedback_to_output Include nonessentials in `|`, `>` results @@ -929,7 +927,8 @@ class Cmd(cmd.Cmd): # TODO: Once support for Python 3.x prior to 3.5 is no longer necessary, replace with a real subprocess pipe # Redirect stdout to a temporary file - _, self._temp_filename = tempfile.mkstemp() + fd, self._temp_filename = tempfile.mkstemp() + os.close(fd) self.stdout = open(self._temp_filename, 'w') elif statement.parsed.output: if (not statement.parsed.outputTo) and (not can_clip): @@ -1706,7 +1705,7 @@ Edited commands are always run after the editor is closed. Edited files are run on close if the ``autorun_on_edit`` settable parameter is True.""" if not self.editor: raise EnvironmentError("Please use 'set editor' to specify your text editing program of choice.") - filename = self.default_file_name + filename = None if arg: try: buffer = self._last_matching(int(arg)) @@ -1720,7 +1719,13 @@ Edited files are run on close if the ``autorun_on_edit`` settable parameter is T self.perror('edit must be called with argument if history is empty', traceback_war=False) return + delete_tempfile = False if buffer: + if filename is None: + fd, filename = tempfile.mkstemp(suffix='.txt', text=True) + os.close(fd) + delete_tempfile = True + f = open(os.path.expanduser(filename), 'w') f.write(buffer or '') f.close() @@ -1730,6 +1735,9 @@ Edited files are run on close if the ``autorun_on_edit`` settable parameter is T if self.autorun_on_edit or buffer: self.do_load(filename) + if delete_tempfile: + os.remove(filename) + saveparser = (pyparsing.Optional(pyparsing.Word(pyparsing.nums) ^ '*')("idx") + pyparsing.Optional(pyparsing.Word(legalChars + '/\\'))("fname") + pyparsing.stringEnd) @@ -1740,13 +1748,20 @@ Edited files are run on close if the ``autorun_on_edit`` settable parameter is T Usage: save [N] [file_path] * N - Number of command (from history), or `*` for all commands in history (default: last command) - * file_path - location to save script of command(s) to (default: value stored in `default_file_name` param)""" + * file_path - location to save script of command(s) to (default: value stored in temporary file)""" try: args = self.saveparser.parseString(arg) except pyparsing.ParseException: self.perror('Could not understand save target %s' % arg) raise SyntaxError(self.do_save.__doc__) - fname = args.fname or self.default_file_name + + # If a filename was supplied then use that, otherwise use a temp file + if args.fname: + fname = args.fname + else: + fd, fname = tempfile.mkstemp(suffix='.txt', text=True) + os.close(fd) + if args.idx == '*': saveme = '\n\n'.join(self.history[:]) elif args.idx: diff --git a/examples/transcript_regex.txt b/examples/transcript_regex.txt index b8e0e654..61bd8838 100644 --- a/examples/transcript_regex.txt +++ b/examples/transcript_regex.txt @@ -2,12 +2,11 @@ # The regex for editor matches any word until first space. The one for colors is because no color on Windows. (Cmd) set abbrev: True -autorun_on_edit: True +autorun_on_edit: False case_insensitive: True colors: /(True|False)/ continuation_prompt: > debug: False -default_file_name: command.txt echo: False editor: /([^\s]+)/ feedback_to_output: False diff --git a/tests/conftest.py b/tests/conftest.py index 77f525f7..6941d9fc 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -48,12 +48,11 @@ if sys.platform.startswith('win'): expect_colors = False # Output from the show command with default settings SHOW_TXT = """abbrev: True -autorun_on_edit: True +autorun_on_edit: False case_insensitive: True colors: {} continuation_prompt: > debug: False -default_file_name: command.txt echo: False editor: vim feedback_to_output: True @@ -67,20 +66,20 @@ if expect_colors: color_str = 'True ' else: color_str = 'False' -SHOW_LONG = """abbrev: True # Accept abbreviated commands -autorun_on_edit: True # Automatically run files after editing -case_insensitive: True # upper- and lower-case both OK -colors: {} # Colorized output (*nix only) -continuation_prompt: > # On 2nd+ line of input -debug: False # Show full error stack on error -default_file_name: command.txt # for ``save``, ``load``, etc. -echo: False # Echo command issued into output -editor: vim # Program used by ``edit`` -feedback_to_output: True # include nonessentials in `|`, `>` results -locals_in_py: True # Allow access to your application in py via self -prompt: (Cmd) # The prompt issued to solicit input -quiet: False # Don't print nonessential feedback -timing: False # Report execution times +SHOW_LONG = """ +abbrev: True # Accept abbreviated commands +autorun_on_edit: False # Automatically run files after editing +case_insensitive: True # Upper- and lower-case both OK +colors: {} # Colorized output (*nix only) +continuation_prompt: > # On 2nd+ line of input +debug: False # Show full error stack on error +echo: False # Echo command issued into output +editor: vim # Program used by ``edit`` +feedback_to_output: True # Include nonessentials in `|`, `>` results +locals_in_py: True # Allow access to your application in py via self +prompt: (Cmd) # The prompt issued to solicit input +quiet: False # Don't print nonessential feedback +timing: False # Report execution times """.format(color_str) diff --git a/tests/test_cmd2.py b/tests/test_cmd2.py index 92fdaa14..707699f3 100644 --- a/tests/test_cmd2.py +++ b/tests/test_cmd2.py @@ -545,10 +545,7 @@ def test_edit_number(base_app): run_cmd(base_app, 'edit 1') # We have an editor, so should expect a system call - m.assert_called_once_with('{} {}'.format(base_app.editor, base_app.default_file_name)) - - # Editing history item causes a file of default name to get created, remove it so we have a clean slate - os.remove(base_app.default_file_name) + m.assert_called_once() def test_edit_blank(base_app): @@ -565,10 +562,7 @@ def test_edit_blank(base_app): run_cmd(base_app, 'edit') # We have an editor, so should expect a system call - m.assert_called_once_with('{} {}'.format(base_app.editor, base_app.default_file_name)) - - # Editing history item causes a file of default name to get created, remove it so we have a clean slate - os.remove(base_app.default_file_name) + m.assert_called_once() def test_base_py_interactive(base_app): diff --git a/tests/test_completion.py b/tests/test_completion.py index 74cc3d57..220d6e26 100644 --- a/tests/test_completion.py +++ b/tests/test_completion.py @@ -150,7 +150,7 @@ def test_path_completion_multiple(cmd2_app, request): endidx = len(line) begidx = endidx - len(text) - assert cmd2_app.path_complete(text, line, begidx, endidx) == ['script.py', 'script.txt'] + assert cmd2_app.path_complete(text, line, begidx, endidx) == ['script.py', 'script.txt', 'scripts/'] def test_path_completion_nomatch(cmd2_app, request): test_dir = os.path.dirname(request.module.__file__) diff --git a/tests/transcript_regex.txt b/tests/transcript_regex.txt index a44870e9..4cddad2c 100644 --- a/tests/transcript_regex.txt +++ b/tests/transcript_regex.txt @@ -2,12 +2,11 @@ # The regex for editor matches any word until first space. The one for colors is because no color on Windows. (Cmd) set abbrev: True -autorun_on_edit: True +autorun_on_edit: False case_insensitive: True colors: /(True|False)/ continuation_prompt: > debug: False -default_file_name: command.txt echo: False editor: /([^\s]+)/ feedback_to_output: True |
