Skip to content

Commit 181cecb

Browse files
committed
Restored a few attributes to be public
1 parent f2166d8 commit 181cecb

File tree

6 files changed

+109
-109
lines changed

6 files changed

+109
-109
lines changed

cmd2/cmd2.py

Lines changed: 51 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -427,14 +427,14 @@ def __init__(self, completekey: str = 'tab', stdin=None, stdout=None, *,
427427
self._script_dir = []
428428

429429
# Context manager used to protect critical sections in the main thread from stopping due to a KeyboardInterrupt
430-
self._sigint_protection = utils.ContextFlag()
430+
self.sigint_protection = utils.ContextFlag()
431431

432432
# If the current command created a process to pipe to, then this will be a ProcReader object.
433433
# Otherwise it will be None. Its used to know when a pipe process can be killed and/or waited upon.
434434
self._cur_pipe_proc_reader = None
435435

436436
# Used by complete() for readline tab completion
437-
self._completion_matches = []
437+
self.completion_matches = []
438438

439439
# Used to keep track of whether we are redirecting or piping output
440440
self._redirecting = False
@@ -537,7 +537,7 @@ def __init__(self, completekey: str = 'tab', stdin=None, stdout=None, *,
537537
# This lock should be acquired before doing any asynchronous changes to the terminal to
538538
# ensure the updates to the terminal don't interfere with the input being typed or output
539539
# being printed by a command.
540-
self._terminal_lock = threading.RLock()
540+
self.terminal_lock = threading.RLock()
541541

542542
# Commands that have been disabled from use. This is to support commands that are only available
543543
# during specific states of the application. This dictionary's keys are the command names and its
@@ -688,7 +688,7 @@ def ppaged(self, msg: str, end: str = '\n', chop: bool = False) -> None:
688688

689689
# Prevent KeyboardInterrupts while in the pager. The pager application will
690690
# still receive the SIGINT since it is in the same process group as us.
691-
with self._sigint_protection:
691+
with self.sigint_protection:
692692
pipe_proc = subprocess.Popen(pager, shell=True, stdin=subprocess.PIPE)
693693
pipe_proc.communicate(msg_str.encode('utf-8', 'replace'))
694694
else:
@@ -1398,7 +1398,7 @@ def _complete_worker(self, text: str, state: int) -> Optional[str]:
13981398
# Check if we either had a parsing error or are trying to complete the command token
13991399
# The latter can happen if " or ' was entered as the command
14001400
if len(tokens) <= 1:
1401-
self._completion_matches = []
1401+
self.completion_matches = []
14021402
return None
14031403

14041404
# Text we need to remove from completions later
@@ -1456,28 +1456,28 @@ def _complete_worker(self, text: str, state: int) -> Optional[str]:
14561456

14571457
# Attempt tab completion for redirection first, and if that isn't occurring,
14581458
# call the completer function for the current command
1459-
self._completion_matches = self._redirect_complete(text, line, begidx, endidx, compfunc)
1459+
self.completion_matches = self._redirect_complete(text, line, begidx, endidx, compfunc)
14601460

1461-
if self._completion_matches:
1461+
if self.completion_matches:
14621462

14631463
# Eliminate duplicates
1464-
self._completion_matches = utils.remove_duplicates(self._completion_matches)
1464+
self.completion_matches = utils.remove_duplicates(self.completion_matches)
14651465
self.display_matches = utils.remove_duplicates(self.display_matches)
14661466

14671467
if not self.display_matches:
1468-
# Since self.display_matches is empty, set it to self._completion_matches
1468+
# Since self.display_matches is empty, set it to self.completion_matches
14691469
# before we alter them. That way the suggestions will reflect how we parsed
14701470
# the token being completed and not how readline did.
14711471
import copy
1472-
self.display_matches = copy.copy(self._completion_matches)
1472+
self.display_matches = copy.copy(self.completion_matches)
14731473

14741474
# Check if we need to add an opening quote
14751475
if not unclosed_quote:
14761476

14771477
add_quote = False
14781478

14791479
# This is the tab completion text that will appear on the command line.
1480-
common_prefix = os.path.commonprefix(self._completion_matches)
1480+
common_prefix = os.path.commonprefix(self.completion_matches)
14811481

14821482
if self.matches_delimited:
14831483
# Check if any portion of the display matches appears in the tab completion
@@ -1490,36 +1490,36 @@ def _complete_worker(self, text: str, state: int) -> Optional[str]:
14901490
add_quote = True
14911491

14921492
# If there is a tab completion and any match has a space, then add an opening quote
1493-
elif common_prefix and any(' ' in match for match in self._completion_matches):
1493+
elif common_prefix and any(' ' in match for match in self.completion_matches):
14941494
add_quote = True
14951495

14961496
if add_quote:
14971497
# Figure out what kind of quote to add and save it as the unclosed_quote
1498-
if any('"' in match for match in self._completion_matches):
1498+
if any('"' in match for match in self.completion_matches):
14991499
unclosed_quote = "'"
15001500
else:
15011501
unclosed_quote = '"'
15021502

1503-
self._completion_matches = [unclosed_quote + match for match in self._completion_matches]
1503+
self.completion_matches = [unclosed_quote + match for match in self.completion_matches]
15041504

15051505
# Check if we need to remove text from the beginning of tab completions
15061506
elif text_to_remove:
1507-
self._completion_matches = \
1508-
[match.replace(text_to_remove, '', 1) for match in self._completion_matches]
1507+
self.completion_matches = \
1508+
[match.replace(text_to_remove, '', 1) for match in self.completion_matches]
15091509

15101510
# Check if we need to restore a shortcut in the tab completions
15111511
# so it doesn't get erased from the command line
15121512
if shortcut_to_restore:
1513-
self._completion_matches = \
1514-
[shortcut_to_restore + match for match in self._completion_matches]
1513+
self.completion_matches = \
1514+
[shortcut_to_restore + match for match in self.completion_matches]
15151515

15161516
else:
15171517
# Complete token against anything a user can run
1518-
self._completion_matches = self.basic_complete(text, line, begidx, endidx,
1519-
self._get_commands_aliases_and_macros_for_completion())
1518+
self.completion_matches = self.basic_complete(text, line, begidx, endidx,
1519+
self._get_commands_aliases_and_macros_for_completion())
15201520

15211521
# Handle single result
1522-
if len(self._completion_matches) == 1:
1522+
if len(self.completion_matches) == 1:
15231523
str_to_append = ''
15241524

15251525
# Add a closing quote if needed and allowed
@@ -1530,16 +1530,16 @@ def _complete_worker(self, text: str, state: int) -> Optional[str]:
15301530
if self.allow_appended_space and endidx == len(line):
15311531
str_to_append += ' '
15321532

1533-
self._completion_matches[0] += str_to_append
1533+
self.completion_matches[0] += str_to_append
15341534

15351535
# Sort matches if they haven't already been sorted
15361536
if not self.matches_sorted:
1537-
self._completion_matches.sort(key=self.matches_sort_key)
1537+
self.completion_matches.sort(key=self.matches_sort_key)
15381538
self.display_matches.sort(key=self.matches_sort_key)
15391539
self.matches_sorted = True
15401540

15411541
try:
1542-
return self._completion_matches[state]
1542+
return self.completion_matches[state]
15431543
except IndexError:
15441544
return None
15451545

@@ -1631,7 +1631,7 @@ def sigint_handler(self, signum: int, frame) -> None:
16311631
self._cur_pipe_proc_reader.send_sigint()
16321632

16331633
# Check if we are allowed to re-raise the KeyboardInterrupt
1634-
if not self._sigint_protection:
1634+
if not self.sigint_protection:
16351635
raise KeyboardInterrupt("Got a keyboard interrupt")
16361636

16371637
def precmd(self, statement: Statement) -> Statement:
@@ -1700,7 +1700,7 @@ def onecmd_plus_hooks(self, line: str, pyscript_bridge_call: bool = False) -> bo
17001700

17011701
try:
17021702
# Get sigint protection while we set up redirection
1703-
with self._sigint_protection:
1703+
with self.sigint_protection:
17041704
if pyscript_bridge_call:
17051705
# Start saving command's stdout at this point
17061706
self.stdout.pause_storage = False
@@ -1743,7 +1743,7 @@ def onecmd_plus_hooks(self, line: str, pyscript_bridge_call: bool = False) -> bo
17431743
self.pfeedback('Elapsed: {}'.format(datetime.datetime.now() - timestart))
17441744
finally:
17451745
# Get sigint protection while we restore stuff
1746-
with self._sigint_protection:
1746+
with self.sigint_protection:
17471747
if saved_state is not None:
17481748
self._restore_output(statement, saved_state)
17491749

@@ -1765,7 +1765,7 @@ def onecmd_plus_hooks(self, line: str, pyscript_bridge_call: bool = False) -> bo
17651765
def _run_cmdfinalization_hooks(self, stop: bool, statement: Optional[Statement]) -> bool:
17661766
"""Run the command finalization hooks"""
17671767

1768-
with self._sigint_protection:
1768+
with self.sigint_protection:
17691769
if not sys.platform.startswith('win') and self.stdout.isatty():
17701770
# Before the next command runs, fix any terminal problems like those
17711771
# caused by certain binary characters having been printed to it.
@@ -2147,10 +2147,10 @@ def _pseudo_raw_input(self, prompt: str) -> str:
21472147
if self.use_rawinput:
21482148
try:
21492149
if sys.stdin.isatty():
2150-
# Wrap in try since _terminal_lock may not be locked when this function is called from unit tests
2150+
# Wrap in try since terminal_lock may not be locked when this function is called from unit tests
21512151
try:
21522152
# A prompt is about to be drawn. Allow asynchronous changes to the terminal.
2153-
self._terminal_lock.release()
2153+
self.terminal_lock.release()
21542154
except RuntimeError:
21552155
pass
21562156

@@ -2166,7 +2166,7 @@ def _pseudo_raw_input(self, prompt: str) -> str:
21662166
finally:
21672167
if sys.stdin.isatty():
21682168
# The prompt is gone. Do not allow asynchronous changes to the terminal.
2169-
self._terminal_lock.acquire()
2169+
self.terminal_lock.acquire()
21702170
else:
21712171
if self.stdin.isatty():
21722172
# on a tty, print the prompt first, then read the line
@@ -2970,7 +2970,7 @@ def do_shell(self, args: argparse.Namespace) -> None:
29702970

29712971
# Prevent KeyboardInterrupts while in the shell process. The shell process will
29722972
# still receive the SIGINT since it is in the same process group as us.
2973-
with self._sigint_protection:
2973+
with self.sigint_protection:
29742974
# For any stream that is a StdSim, we will use a pipe so we can capture its output
29752975
proc = subprocess.Popen(expanded_command,
29762976
stdout=subprocess.PIPE if isinstance(self.stdout, utils.StdSim) else self.stdout,
@@ -3490,7 +3490,7 @@ def _generate_transcript(self, history: List[Union[HistoryItem, str]], transcrip
34903490

34913491
commands_run = 0
34923492
try:
3493-
with self._sigint_protection:
3493+
with self.sigint_protection:
34943494
# Disable echo while we manually redirect stdout to a StringIO buffer
34953495
saved_echo = self.echo
34963496
saved_stdout = self.stdout
@@ -3535,7 +3535,7 @@ def _generate_transcript(self, history: List[Union[HistoryItem, str]], transcrip
35353535
if stop:
35363536
break
35373537
finally:
3538-
with self._sigint_protection:
3538+
with self.sigint_protection:
35393539
# Restore altered attributes to their original state
35403540
self.echo = saved_echo
35413541
self.stdout = saved_stdout
@@ -3655,7 +3655,7 @@ def do_run_script(self, args: argparse.Namespace) -> Optional[bool]:
36553655
return self.runcmds_plus_hooks(script_commands)
36563656

36573657
finally:
3658-
with self._sigint_protection:
3658+
with self.sigint_protection:
36593659
# Check if a script dir was added before an exception occurred
36603660
if orig_script_dir_count != len(self._script_dir):
36613661
self._script_dir.pop()
@@ -3765,14 +3765,14 @@ def async_alert(self, alert_msg: str, new_prompt: Optional[str] = None) -> None:
37653765
To the user it appears as if an alert message is printed above the prompt and their current input
37663766
text and cursor location is left alone.
37673767
3768-
IMPORTANT: This function will not print an alert unless it can acquire self._terminal_lock to ensure
3768+
IMPORTANT: This function will not print an alert unless it can acquire self.terminal_lock to ensure
37693769
a prompt is onscreen. Therefore it is best to acquire the lock before calling this function
37703770
to guarantee the alert prints.
37713771
37723772
:param alert_msg: the message to display to the user
37733773
:param new_prompt: if you also want to change the prompt that is displayed, then include it here
37743774
see async_update_prompt() docstring for guidance on updating a prompt
3775-
:raises RuntimeError if called while another thread holds _terminal_lock
3775+
:raises RuntimeError if called while another thread holds terminal_lock
37763776
"""
37773777
if not (vt100_support and self.use_rawinput):
37783778
return
@@ -3781,8 +3781,8 @@ def async_alert(self, alert_msg: str, new_prompt: Optional[str] = None) -> None:
37813781
import colorama.ansi as ansi
37823782
from colorama import Cursor
37833783

3784-
# Sanity check that can't fail if self._terminal_lock was acquired before calling this function
3785-
if self._terminal_lock.acquire(blocking=False):
3784+
# Sanity check that can't fail if self.terminal_lock was acquired before calling this function
3785+
if self.terminal_lock.acquire(blocking=False):
37863786

37873787
# Figure out what prompt is displaying
37883788
current_prompt = self.continuation_prompt if self._at_continuation_prompt else self.prompt
@@ -3857,10 +3857,10 @@ def async_alert(self, alert_msg: str, new_prompt: Optional[str] = None) -> None:
38573857
# Redraw the prompt and input lines
38583858
rl_force_redisplay()
38593859

3860-
self._terminal_lock.release()
3860+
self.terminal_lock.release()
38613861

38623862
else:
3863-
raise RuntimeError("another thread holds _terminal_lock")
3863+
raise RuntimeError("another thread holds terminal_lock")
38643864

38653865
def async_update_prompt(self, new_prompt: str) -> None: # pragma: no cover
38663866
"""
@@ -3870,7 +3870,7 @@ def async_update_prompt(self, new_prompt: str) -> None: # pragma: no cover
38703870
it is best to keep the prompt the same width as what's on screen. Otherwise the user's input text will
38713871
be shifted and the update will not be seamless.
38723872
3873-
IMPORTANT: This function will not update the prompt unless it can acquire self._terminal_lock to ensure
3873+
IMPORTANT: This function will not update the prompt unless it can acquire self.terminal_lock to ensure
38743874
a prompt is onscreen. Therefore it is best to acquire the lock before calling this function
38753875
to guarantee the prompt changes.
38763876
@@ -3879,37 +3879,37 @@ def async_update_prompt(self, new_prompt: str) -> None: # pragma: no cover
38793879
and display immediately after the multiline line command completes.
38803880
38813881
:param new_prompt: what to change the prompt to
3882-
:raises RuntimeError if called while another thread holds _terminal_lock
3882+
:raises RuntimeError if called while another thread holds terminal_lock
38833883
"""
38843884
self.async_alert('', new_prompt)
38853885

38863886
def set_window_title(self, title: str) -> None: # pragma: no cover
38873887
"""
38883888
Set the terminal window title
38893889
3890-
IMPORTANT: This function will not set the title unless it can acquire self._terminal_lock to avoid
3890+
IMPORTANT: This function will not set the title unless it can acquire self.terminal_lock to avoid
38913891
writing to stderr while a command is running. Therefore it is best to acquire the lock
38923892
before calling this function to guarantee the title changes.
38933893
38943894
:param title: the new window title
3895-
:raises RuntimeError if called while another thread holds _terminal_lock
3895+
:raises RuntimeError if called while another thread holds terminal_lock
38963896
"""
38973897
if not vt100_support:
38983898
return
38993899

3900-
# Sanity check that can't fail if self._terminal_lock was acquired before calling this function
3901-
if self._terminal_lock.acquire(blocking=False):
3900+
# Sanity check that can't fail if self.terminal_lock was acquired before calling this function
3901+
if self.terminal_lock.acquire(blocking=False):
39023902
try:
39033903
import colorama.ansi as ansi
39043904
sys.stderr.write(ansi.set_title(title))
39053905
except AttributeError:
39063906
# Debugging in Pycharm has issues with setting terminal title
39073907
pass
39083908
finally:
3909-
self._terminal_lock.release()
3909+
self.terminal_lock.release()
39103910

39113911
else:
3912-
raise RuntimeError("another thread holds _terminal_lock")
3912+
raise RuntimeError("another thread holds terminal_lock")
39133913

39143914
def enable_command(self, command: str) -> None:
39153915
"""
@@ -4027,7 +4027,7 @@ def cmdloop(self, intro: Optional[str] = None) -> int:
40274027
signal.signal(signal.SIGINT, self.sigint_handler)
40284028

40294029
# Grab terminal lock before the prompt has been drawn by readline
4030-
self._terminal_lock.acquire()
4030+
self.terminal_lock.acquire()
40314031

40324032
# Always run the preloop first
40334033
for func in self._preloop_hooks:
@@ -4056,7 +4056,7 @@ def cmdloop(self, intro: Optional[str] = None) -> int:
40564056

40574057
# Release terminal lock now that postloop code should have stopped any terminal updater threads
40584058
# This will also zero the lock count in case cmdloop() is called again
4059-
self._terminal_lock.release()
4059+
self.terminal_lock.release()
40604060

40614061
# Restore the original signal handler
40624062
signal.signal(signal.SIGINT, original_sigint_handler)

cmd2/pyscript_bridge.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ def __call__(self, command: str, echo: Optional[bool] = None) -> CommandResult:
9797
with redirect_stderr(copy_stderr):
9898
stop = self._cmd2_app.onecmd_plus_hooks(command, pyscript_bridge_call=True)
9999
finally:
100-
with self._cmd2_app._sigint_protection:
100+
with self._cmd2_app.sigint_protection:
101101
self._cmd2_app.stdout = copy_cmd_stdout.inner_stream
102102
self.stop = stop or self.stop
103103

0 commit comments

Comments
 (0)