summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorKevin Van Brunt <kmvanbrunt@gmail.com>2020-04-20 21:21:14 -0400
committerKevin Van Brunt <kmvanbrunt@gmail.com>2020-04-20 21:21:14 -0400
commitf9a8727805ea786781635db5d6e3c2247080c441 (patch)
tree54c5dd9b107c72e07947a9391438688a113b3ed6
parent85bb29b7edfc6485556f398ddb192c16d1509856 (diff)
downloadcmd2-git-f9a8727805ea786781635db5d6e3c2247080c441.tar.gz
ipy now returns its PyBridge's stop value so the main loop can know to stop. This is consistent with the py command.
-rw-r--r--cmd2/cmd2.py39
1 files changed, 29 insertions, 10 deletions
diff --git a/cmd2/cmd2.py b/cmd2/cmd2.py
index e0c28941..fa61f961 100644
--- a/cmd2/cmd2.py
+++ b/cmd2/cmd2.py
@@ -531,6 +531,7 @@ class Cmd(cmd.Cmd):
def pfeedback(self, msg: Any, *, end: str = '\n') -> None:
"""For printing nonessential feedback. Can be silenced with `quiet`.
Inclusion in redirected output is controlled by `feedback_to_output`.
+
:param msg: message to print (anything convertible to a str with '{}'.format() is OK)
:param end: string appended after the end of the message, default a newline
"""
@@ -2098,9 +2099,10 @@ class Cmd(cmd.Cmd):
def read_input(self, prompt: str, *, allow_completion: bool = False) -> str:
"""
Read input from appropriate stdin value. Also allows you to disable tab completion while input is being read.
+
:param prompt: prompt to display to user
:param allow_completion: if True, then tab completion of commands is enabled. This generally should be
- set to False unless reading the command line. Defaults to False.
+ set to False unless reading the command line. Defaults to False.
:return: the line read from stdin with all trailing new lines removed
:raises any exceptions raised by input() and stdin.readline()
"""
@@ -2195,6 +2197,7 @@ class Cmd(cmd.Cmd):
def _set_up_cmd2_readline(self) -> _SavedReadlineSettings:
"""
Set up readline with cmd2-specific settings
+
:return: Class containing saved readline settings
"""
readline_settings = _SavedReadlineSettings()
@@ -2229,6 +2232,7 @@ class Cmd(cmd.Cmd):
def _restore_readline(self, readline_settings: _SavedReadlineSettings):
"""
Restore saved readline settings
+
:param readline_settings: the readline settings to restore
"""
if self._completion_supported():
@@ -3117,6 +3121,7 @@ class Cmd(cmd.Cmd):
def _restore_cmd2_env(self, cmd2_env: _SavedCmd2Env) -> None:
"""
Restore cmd2 environment after exiting an interactive Python shell
+
:param cmd2_env: the environment settings to restore
"""
sys.stdout = cmd2_env.sys_stdout
@@ -3174,6 +3179,7 @@ class Cmd(cmd.Cmd):
def do_py(self, args: argparse.Namespace) -> Optional[bool]:
"""
Enter an interactive Python shell
+
:return: True if running of commands should stop
"""
def py_quit():
@@ -3293,7 +3299,8 @@ class Cmd(cmd.Cmd):
def do_run_pyscript(self, args: argparse.Namespace) -> Optional[bool]:
"""
Run a Python script file inside the console
- :return: True if running of commands should stop
+
+ :return: True if running of commands should stop
"""
# Expand ~ before placing this path in sys.argv just as a shell would
args.script_path = os.path.expanduser(args.script_path)
@@ -3327,8 +3334,12 @@ class Cmd(cmd.Cmd):
ipython_parser = DEFAULT_ARGUMENT_PARSER(description="Enter an interactive IPython shell")
@with_argparser(ipython_parser)
- def do_ipy(self, _: argparse.Namespace) -> None:
- """Enter an interactive IPython shell"""
+ def do_ipy(self, _: argparse.Namespace) -> Optional[bool]:
+ """
+ Enter an interactive IPython shell
+
+ :return: True if running of commands should stop
+ """
from .py_bridge import PyBridge
banner = ('Entering an embedded IPython shell. Type quit or <Ctrl>-d to exit.\n'
'Run Python code from external files with: run filename.py\n')
@@ -3338,8 +3349,9 @@ class Cmd(cmd.Cmd):
def load_ipy(cmd2_app: Cmd, py_bridge: PyBridge):
"""
Embed an IPython shell in an environment that is restricted to only the variables in this function
+
:param cmd2_app: instance of the cmd2 app
- :param py_bridge: a PyscriptBridge
+ :param py_bridge: a PyBridge
"""
# Create a variable pointing to py_bridge and name it using the value of py_bridge_name
exec("{} = py_bridge".format(cmd2_app.py_bridge_name))
@@ -3354,7 +3366,9 @@ class Cmd(cmd.Cmd):
embed(banner1=banner, exit_msg=exit_msg)
- load_ipy(self, PyBridge(self))
+ new_py_bridge = PyBridge(self)
+ load_ipy(self, new_py_bridge)
+ return new_py_bridge.stop
history_description = "View, run, edit, save, or clear previously entered commands"
@@ -3396,6 +3410,7 @@ class Cmd(cmd.Cmd):
def do_history(self, args: argparse.Namespace) -> Optional[bool]:
"""
View, run, edit, save, or clear previously entered commands
+
:return: True if running of commands should stop
"""
@@ -3568,7 +3583,7 @@ class Cmd(cmd.Cmd):
atexit.register(self._persist_history)
def _persist_history(self):
- """write history out to the history file"""
+ """Write history out to the history file"""
if not self.persistent_history_file:
return
@@ -3581,9 +3596,7 @@ class Cmd(cmd.Cmd):
self.pexcept(msg.format(self.persistent_history_file, ex))
def _generate_transcript(self, history: List[Union[HistoryItem, str]], transcript_file: str) -> None:
- """
- Generate a transcript file from a given history of commands
- """
+ """Generate a transcript file from a given history of commands"""
# Validate the transcript file path to make sure directory exists and write access is available
transcript_path = os.path.abspath(os.path.expanduser(transcript_file))
transcript_dir = os.path.dirname(transcript_path)
@@ -3686,6 +3699,7 @@ class Cmd(cmd.Cmd):
def _run_editor(self, file_path: Optional[str]) -> None:
"""
Run a text editor and optionally open a file with it
+
:param file_path: optional path of the file to edit
:raises EnvironmentError if self.editor is not set
"""
@@ -3797,6 +3811,7 @@ class Cmd(cmd.Cmd):
def do__relative_run_script(self, args: argparse.Namespace) -> Optional[bool]:
"""
Run commands in script file that is encoded as either ASCII or UTF-8 text
+
:return: True if running of commands should stop
"""
file_path = args.file_path
@@ -3979,6 +3994,7 @@ class Cmd(cmd.Cmd):
def enable_command(self, command: str) -> None:
"""
Enable a command by restoring its functions
+
:param command: the command being enabled
"""
# If the commands is already enabled, then return
@@ -4010,6 +4026,7 @@ class Cmd(cmd.Cmd):
def enable_category(self, category: str) -> None:
"""
Enable an entire category of commands
+
:param category: the category to enable
"""
for cmd_name in list(self.disabled_commands):
@@ -4020,6 +4037,7 @@ class Cmd(cmd.Cmd):
def disable_command(self, command: str, message_to_print: str) -> None:
"""
Disable a command and overwrite its functions
+
:param command: the command being disabled
:param message_to_print: what to print when this command is run or help is called on it while disabled
@@ -4074,6 +4092,7 @@ class Cmd(cmd.Cmd):
def _report_disabled_command_usage(self, *_args, message_to_print: str, **_kwargs) -> None:
"""
Report when a disabled command has been run or had help called on it
+
:param args: not used
:param message_to_print: the message reporting that the command is disabled
:param kwargs: not used