MCPcopy Index your code
hub / github.com/python-cmd2/cmd2 / _restore_output

Method _restore_output

cmd2/cmd2.py:3305–3333  ·  view source on GitHub ↗

Handle restoring state after output redirection. :param statement: Statement object which contains the parsed input from the user :param saved_redir_state: contains information needed to restore state data

(self, statement: Statement, saved_redir_state: utils.RedirectionSavedState)

Source from the content-addressed store, hash-verified

3303 return redir_saved_state
3304
3305 def _restore_output(self, statement: Statement, saved_redir_state: utils.RedirectionSavedState) -> None:
3306 """Handle restoring state after output redirection.
3307
3308 :param statement: Statement object which contains the parsed input from the user
3309 :param saved_redir_state: contains information needed to restore state data
3310 """
3311 if saved_redir_state.redirecting:
3312 # If we redirected output to the clipboard
3313 if (
3314 statement.redirector in (constants.REDIRECTION_OVERWRITE, constants.REDIRECTION_APPEND)
3315 and not statement.redirect_to
3316 ):
3317 self.stdout.seek(0)
3318 write_to_paste_buffer(self.stdout.read())
3319
3320 with contextlib.suppress(BrokenPipeError):
3321 # Close the file or pipe that stdout was redirected to
3322 self.stdout.close()
3323
3324 # Restore self.stdout
3325 self.stdout = cast(TextIO, saved_redir_state.saved_self_stdout)
3326
3327 # Check if we need to wait for the process being piped to
3328 if self._cur_pipe_proc_reader is not None:
3329 self._cur_pipe_proc_reader.wait()
3330
3331 # These are restored regardless of whether the command redirected
3332 self._cur_pipe_proc_reader = saved_redir_state.saved_pipe_proc_reader
3333 self._redirecting = saved_redir_state.saved_redirecting
3334
3335 def get_command_func(self, command: str) -> BoundCommandFunc | None:
3336 """Get the bound command function for a command.

Callers 1

onecmd_plus_hooksMethod · 0.95

Calls 3

write_to_paste_bufferFunction · 0.85
readMethod · 0.80
waitMethod · 0.80

Tested by

no test coverage detected