Skip to content

Commit f93d51b

Browse files
committed
Removed Statekeeper class and renamed some variables
1 parent 1241734 commit f93d51b

File tree

1 file changed

+18
-39
lines changed

1 file changed

+18
-39
lines changed

cmd2/cmd2.py

Lines changed: 18 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -289,31 +289,6 @@ def cmd_wrapper(cmd2_instance, statement: Union[Statement, str]):
289289
return arg_decorator
290290

291291

292-
class Statekeeper(object):
293-
"""Class used to save and restore state during load and py commands as well as when redirecting output or pipes."""
294-
def __init__(self, obj: Any, attribs: Iterable) -> None:
295-
"""Use the instance attributes as a generic key-value store to copy instance attributes from outer object.
296-
297-
:param obj: instance of cmd2.Cmd derived class (your application instance)
298-
:param attribs: tuple of strings listing attributes of obj to save a copy of
299-
"""
300-
self.obj = obj
301-
self.attribs = attribs
302-
if self.obj:
303-
self._save()
304-
305-
def _save(self) -> None:
306-
"""Create copies of attributes from self.obj inside this Statekeeper instance."""
307-
for attrib in self.attribs:
308-
setattr(self, attrib, getattr(self.obj, attrib))
309-
310-
def restore(self) -> None:
311-
"""Overwrite attributes in self.obj with the saved values stored in this Statekeeper instance."""
312-
if self.obj:
313-
for attrib in self.attribs:
314-
setattr(self.obj, attrib, getattr(self, attrib))
315-
316-
317292
class EmbeddedConsoleExit(SystemExit):
318293
"""Custom exception class for use with the py command."""
319294
pass
@@ -2186,10 +2161,10 @@ def _cmdloop(self) -> bool:
21862161
# Set GNU readline's rl_basic_quote_characters to NULL so it won't automatically add a closing quote
21872162
# We don't need to worry about setting rl_completion_suppress_quote since we never declared
21882163
# rl_completer_quote_characters.
2189-
old_basic_quotes = ctypes.cast(rl_basic_quote_characters, ctypes.c_void_p).value
2164+
saved_basic_quotes = ctypes.cast(rl_basic_quote_characters, ctypes.c_void_p).value
21902165
rl_basic_quote_characters.value = None
21912166

2192-
old_completer = readline.get_completer()
2167+
saved_completer = readline.get_completer()
21932168
readline.set_completer(self.complete)
21942169

21952170
# Break words on whitespace and quotes when tab completing
@@ -2199,7 +2174,7 @@ def _cmdloop(self) -> bool:
21992174
# If redirection is allowed, then break words on those characters too
22002175
completer_delims += ''.join(constants.REDIRECTION_CHARS)
22012176

2202-
old_delims = readline.get_completer_delims()
2177+
saved_delims = readline.get_completer_delims()
22032178
readline.set_completer_delims(completer_delims)
22042179

22052180
# Enable tab completion
@@ -2231,12 +2206,12 @@ def _cmdloop(self) -> bool:
22312206
if self.use_rawinput and self.completekey and rl_type != RlType.NONE:
22322207

22332208
# Restore what we changed in readline
2234-
readline.set_completer(old_completer)
2235-
readline.set_completer_delims(old_delims)
2209+
readline.set_completer(saved_completer)
2210+
readline.set_completer_delims(saved_delims)
22362211

22372212
if rl_type == RlType.GNU:
22382213
readline.set_completion_display_matches_hook(None)
2239-
rl_basic_quote_characters.value = old_basic_quotes
2214+
rl_basic_quote_characters.value = saved_basic_quotes
22402215
elif rl_type == RlType.PYREADLINE:
22412216
# noinspection PyUnresolvedReferences
22422217
readline.rl.mode._display_completions = orig_pyreadline_display
@@ -3097,7 +3072,7 @@ def py_quit():
30973072
# Set up tab completion for the Python console
30983073
# rlcompleter relies on the default settings of the Python readline module
30993074
if rl_type == RlType.GNU:
3100-
old_basic_quotes = ctypes.cast(rl_basic_quote_characters, ctypes.c_void_p).value
3075+
saved_basic_quotes = ctypes.cast(rl_basic_quote_characters, ctypes.c_void_p).value
31013076
rl_basic_quote_characters.value = orig_rl_basic_quotes
31023077

31033078
if 'gnureadline' in sys.modules:
@@ -3109,7 +3084,7 @@ def py_quit():
31093084

31103085
sys.modules['readline'] = sys.modules['gnureadline']
31113086

3112-
old_delims = readline.get_completer_delims()
3087+
saved_delims = readline.get_completer_delims()
31133088
readline.set_completer_delims(orig_rl_delims)
31143089

31153090
# rlcompleter will not need cmd2's custom display function
@@ -3122,15 +3097,18 @@ def py_quit():
31223097

31233098
# Save off the current completer and set a new one in the Python console
31243099
# Make sure it tab completes from its locals() dictionary
3125-
old_completer = readline.get_completer()
3100+
saved_completer = readline.get_completer()
31263101
interp.runcode("from rlcompleter import Completer")
31273102
interp.runcode("import readline")
31283103
interp.runcode("readline.set_completer(Completer(locals()).complete)")
31293104

31303105
# Set up sys module for the Python console
31313106
self._reset_py_display()
3132-
keepstate = Statekeeper(sys, ('stdin', 'stdout'))
3107+
3108+
saved_sys_stdout = sys.stdout
31333109
sys.stdout = self.stdout
3110+
3111+
saved_sys_stdin = sys.stdin
31343112
sys.stdin = self.stdin
31353113

31363114
cprt = 'Type "help", "copyright", "credits" or "license" for more information.'
@@ -3148,7 +3126,8 @@ def py_quit():
31483126
pass
31493127

31503128
finally:
3151-
keepstate.restore()
3129+
sys.stdout = saved_sys_stdout
3130+
sys.stdin = saved_sys_stdin
31523131

31533132
# Set up readline for cmd2
31543133
if rl_type != RlType.NONE:
@@ -3166,11 +3145,11 @@ def py_quit():
31663145

31673146
if self.use_rawinput and self.completekey:
31683147
# Restore cmd2's tab completion settings
3169-
readline.set_completer(old_completer)
3170-
readline.set_completer_delims(old_delims)
3148+
readline.set_completer(saved_completer)
3149+
readline.set_completer_delims(saved_delims)
31713150

31723151
if rl_type == RlType.GNU:
3173-
rl_basic_quote_characters.value = old_basic_quotes
3152+
rl_basic_quote_characters.value = saved_basic_quotes
31743153

31753154
if 'gnureadline' in sys.modules:
31763155
# Restore what the readline module pointed to

0 commit comments

Comments
 (0)