diff options
author | Todd Leonhardt <tleonhardt@gmail.com> | 2017-03-10 15:32:24 -0500 |
---|---|---|
committer | Todd Leonhardt <tleonhardt@gmail.com> | 2017-03-10 15:32:24 -0500 |
commit | 2cdadd69fc74fbc30b1beb83b7c3d2d1eadfae8a (patch) | |
tree | c4c375e9b66c4ccf5f26381ec6b513cb88d72c55 | |
parent | 27c182b14c6f2529fdb31b7b3dba1120ca23a3dc (diff) | |
download | cmd2-git-2cdadd69fc74fbc30b1beb83b7c3d2d1eadfae8a.tar.gz |
Improvements to py command.
The following improvements have been made:
1) Can now use "cmd" to run cmd2 do_* commands within a python script before ever entering an interactive Python console
2) Issuing a cmd('quit') within a python script will now really quit the application.
3) Recursively entering an interactive Python session from within an existing one is no longer allowed.
-rwxr-xr-x | cmd2.py | 25 |
1 files changed, 19 insertions, 6 deletions
@@ -595,6 +595,9 @@ class Cmd(cmd.Cmd): self._temp_filename = None self._transcript_files = transcript_files + self._should_quit = False + self._in_py = False + def poutput(self, msg): """Convenient shortcut for self.stdout.write(); adds newline if necessary.""" if msg: @@ -1001,6 +1004,7 @@ class Cmd(cmd.Cmd): def do_quit(self, arg): """Exits this application.""" + self._should_quit = True return self._STOP_AND_EXIT def select(self, options, prompt='Your choice? '): @@ -1131,13 +1135,18 @@ class Cmd(cmd.Cmd): self.stdout.write("{}\n".format(help_str)) def do_py(self, arg): - ''' + """ py <command>: Executes a Python command. py: Enters interactive Python mode. End with ``Ctrl-D`` (Unix) / ``Ctrl-Z`` (Windows), ``quit()``, '`exit()``. Non-python commands can be issued with ``cmd("your command")``. Run python code from external files with ``run("filename.py")`` - ''' + """ + if self._in_py: + self.perror("Recursively entering interactive Python consoles is not allowed.", traceback_war=False) + return + self._in_py = True + self.pystate['self'] = self arg = arg.parsed.raw[2:].strip() @@ -1148,7 +1157,12 @@ class Cmd(cmd.Cmd): interp.runcode(f.read()) except IOError as e: self.perror(e) + + def onecmd_plus_hooks(arg): + return self.onecmd_plus_hooks(arg + '\n') + self.pystate['run'] = run + self.pystate['cmd'] = onecmd_plus_hooks localvars = (self.locals_in_py and self.pystate) or {} interp = InteractiveConsole(locals=localvars) @@ -1160,12 +1174,9 @@ class Cmd(cmd.Cmd): def quit(): raise EmbeddedConsoleExit - def onecmd_plus_hooks(arg): - return self.onecmd_plus_hooks(arg + '\n') - self.pystate['quit'] = quit self.pystate['exit'] = quit - self.pystate['cmd'] = onecmd_plus_hooks + keepstate = None try: cprt = 'Type "help", "copyright", "credits" or "license" for more information.' @@ -1178,6 +1189,8 @@ class Cmd(cmd.Cmd): pass if keepstate is not None: keepstate.restore() + self._in_py = False + return self._should_quit # Only include the do_ipy() method if IPython is available on the system if ipython_available: |