Skip to content

Commit 7a24546

Browse files
committed
Started addressing my PR comments
1 parent c17858d commit 7a24546

File tree

2 files changed

+39
-37
lines changed

2 files changed

+39
-37
lines changed

cmd2/cmd2.py

Lines changed: 7 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -303,21 +303,6 @@ class EmptyStatement(Exception):
303303
DisabledCommand = namedtuple('DisabledCommand', ['command_function', 'help_function'])
304304

305305

306-
class RedirectionSavedState(object):
307-
# Created by each command to store information about their redirection
308-
def __init__(self):
309-
# Tells if the command is redirecting
310-
self.redirecting = False
311-
312-
# If the command created a process to pipe to, then then is its reader
313-
self.pipe_proc_reader = None
314-
315-
# Used to restore values after the command ends
316-
self.saved_self_stdout = None
317-
self.saved_sys_stdout = None
318-
self.saved_pipe_proc_reader = None
319-
320-
321306
class Cmd(cmd.Cmd):
322307
"""An easy but powerful framework for writing line-oriented command interpreters.
323308
@@ -433,12 +418,11 @@ def __init__(self, completekey: str = 'tab', stdin=None, stdout=None, persistent
433418
# Used load command to store the current script dir as a LIFO queue to support _relative_load command
434419
self._script_dir = []
435420

436-
# A flag used to protect critical sections in the main thread from stopping due to a KeyboardInterrupt
421+
# Context manager used to protect critical sections in the main thread from stopping due to a KeyboardInterrupt
437422
self.sigint_protection = utils.ContextFlag()
438423

439424
# If the current command created a process to pipe to, then this will be a ProcReader object.
440-
# Otherwise the value will be None. This member is used to know when a pipe process can be killed
441-
# and also waited upon.
425+
# Otherwise it will be None. Its used to know when a pipe process can be killed and/or waited upon.
442426
self.cur_pipe_proc_reader = None
443427

444428
# Used by complete() for readline tab completion
@@ -1724,7 +1708,7 @@ def onecmd_plus_hooks(self, line: str) -> bool:
17241708
# Keep track of whether or not we were already redirecting before this command
17251709
already_redirecting = self.redirecting
17261710

1727-
# This will be a RedirectionSavedState object for the command
1711+
# This will be a utils.RedirectionSavedState object for the command
17281712
saved_state = None
17291713

17301714
try:
@@ -1900,22 +1884,19 @@ def _complete_statement(self, line: str) -> Statement:
19001884
raise EmptyStatement()
19011885
return statement
19021886

1903-
def _redirect_output(self, statement: Statement) -> Tuple[bool, RedirectionSavedState]:
1887+
def _redirect_output(self, statement: Statement) -> Tuple[bool, utils.RedirectionSavedState]:
19041888
"""Handles output redirection for >, >>, and |.
19051889
19061890
:param statement: a parsed statement from the user
1907-
:return: A bool telling if an error occurred and a RedirectionSavedState object
1891+
:return: A bool telling if an error occurred and a utils.RedirectionSavedState object
19081892
"""
19091893
import io
19101894
import subprocess
19111895

19121896
redir_error = False
19131897

19141898
# Initialize the saved state
1915-
saved_state = RedirectionSavedState()
1916-
saved_state.saved_self_stdout = self.stdout
1917-
saved_state.saved_sys_stdout = sys.stdout
1918-
saved_state.saved_pipe_proc_reader = self.cur_pipe_proc_reader
1899+
saved_state = utils.RedirectionSavedState(self.stdout, sys.stdout, self.cur_pipe_proc_reader)
19191900

19201901
if not self.allow_redirection:
19211902
return redir_error, saved_state
@@ -1990,7 +1971,7 @@ def _redirect_output(self, statement: Statement) -> Tuple[bool, RedirectionSaved
19901971

19911972
return redir_error, saved_state
19921973

1993-
def _restore_output(self, statement: Statement, saved_state: RedirectionSavedState) -> None:
1974+
def _restore_output(self, statement: Statement, saved_state: utils.RedirectionSavedState) -> None:
19941975
"""Handles restoring state after output redirection as well as
19951976
the actual pipe operation if present.
19961977

cmd2/utils.py

Lines changed: 32 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -342,12 +342,14 @@ def readbytes(self) -> bytes:
342342

343343
def clear(self) -> None:
344344
"""Clear the internal contents"""
345-
self.buffer.byte_buf = b''
345+
self.buffer.byte_buf = bytearray()
346346

347-
@staticmethod
348-
def isatty() -> bool:
349-
"""StdSim will never be considered an interactive stream"""
350-
return False
347+
def isatty(self) -> bool:
348+
"""StdSim only be considered an interactive stream if `echo` is True and `inner_stream` is a tty."""
349+
if self.echo:
350+
return self.inner_stream.isatty()
351+
else:
352+
return False
351353

352354
def __getattr__(self, item: str):
353355
if item in self.__dict__:
@@ -469,24 +471,43 @@ def _write_bytes(stream: Union[StdSim, BinaryIO, TextIO], to_write: bytes) -> No
469471

470472

471473
class ContextFlag(object):
472-
"""
473-
A flag value that is used in a with statement.
474+
"""A context manager which is also used as a boolean flag value within the default sigint handler.
475+
474476
Its main use is as a flag to prevent the SIGINT handler in cmd2 from raising a KeyboardInterrupt
475477
while a critical code section has set the flag to True. Because signal handling is always done on the
476478
main thread, this class is not thread-safe since there is no need.
477479
"""
478-
def __init__(self):
480+
def __init__(self) -> None:
479481
# When this flag has a positive value, it is considered set.
480482
# When it is 0, it is not set. It should never go below 0.
481483
self.__count = 0
482484

483-
def __bool__(self):
485+
def __bool__(self) -> bool:
484486
return self.__count > 0
485487

486-
def __enter__(self):
488+
def __enter__(self) -> None:
487489
self.__count += 1
488490

489-
def __exit__(self, *args):
491+
def __exit__(self, *args) -> None:
490492
self.__count -= 1
491493
if self.__count < 0:
492494
raise ValueError("count has gone below 0")
495+
496+
497+
class RedirectionSavedState(object):
498+
"""Created by each command to store information about their redirection."""
499+
500+
def __init__(self, self_stdout: Union[StdSim, BinaryIO, TextIO], sys_stdout: Union[StdSim, BinaryIO, TextIO],
501+
pipe_proc_reader: Optional[ProcReader]) -> None:
502+
# Used to restore values after the command ends
503+
self.saved_self_stdout = self_stdout
504+
self.saved_sys_stdout = sys_stdout
505+
self.saved_pipe_proc_reader = pipe_proc_reader
506+
507+
# Tells if the command is redirecting
508+
self.redirecting = False
509+
510+
# If the command created a process to pipe to, then then is its reader
511+
self.pipe_proc_reader = None
512+
513+

0 commit comments

Comments
 (0)