diff options
author | Todd Leonhardt <todd.leonhardt@gmail.com> | 2018-04-27 14:21:40 -0400 |
---|---|---|
committer | GitHub <noreply@github.com> | 2018-04-27 14:21:40 -0400 |
commit | 1306eebade58d7ffe5d0ab4008006b7fb3501b54 (patch) | |
tree | 368134105548c83ee224af234081b981be157725 /cmd2/cmd2.py | |
parent | a93437308bc597ff2fa759db144fd97d4db31c98 (diff) | |
parent | bed0b6be151b2eafd03111cd3782e2672d193443 (diff) | |
download | cmd2-git-1306eebade58d7ffe5d0ab4008006b7fb3501b54.tar.gz |
Merge pull request #374 from python-cmd2/constants
Move constants and utility functions into separate files.
Diffstat (limited to 'cmd2/cmd2.py')
-rwxr-xr-x | cmd2/cmd2.py | 50 |
1 files changed, 18 insertions, 32 deletions
diff --git a/cmd2/cmd2.py b/cmd2/cmd2.py index 8d8a5b07..1f48ad5a 100755 --- a/cmd2/cmd2.py +++ b/cmd2/cmd2.py @@ -49,6 +49,9 @@ from code import InteractiveConsole import pyparsing import pyperclip +from . import constants +from . import utils + # Set up readline from .rl_utils import rl_force_redisplay, readline, rl_type, RlType from .argparse_completer import AutoCompleter, ACArgumentParser @@ -134,10 +137,6 @@ POSIX_SHLEX = False # Strip outer quotes for convenience if POSIX_SHLEX = False STRIP_QUOTES_FOR_NON_POSIX = True -# Used for tab completion and word breaks. Do not change. -QUOTES = ['"', "'"] -REDIRECTION_CHARS = ['|', '<', '>'] - # optional attribute, when tagged on a function, allows cmd2 to categorize commands HELP_CATEGORY = 'help_category' HELP_SUMMARY = 'help_summary' @@ -398,19 +397,6 @@ class EmptyStatement(Exception): pass -# Regular expression to match ANSI escape codes -ANSI_ESCAPE_RE = re.compile(r'\x1b[^m]*m') - - -def strip_ansi(text: str) -> str: - """Strip ANSI escape codes from a string. - - :param text: string which may contain ANSI escape codes - :return: the same string with any ANSI escape codes removed - """ - return ANSI_ESCAPE_RE.sub('', text) - - def _pop_readline_history(clear_history: bool=True) -> List[str]: """Returns a copy of readline's history and optionally clears it (default)""" # noinspection PyArgumentList @@ -855,7 +841,7 @@ class Cmd(cmd.Cmd): :return: str - prompt stripped of any ANSI escape codes """ - return strip_ansi(self.prompt) + return utils.strip_ansi(self.prompt) def _finalize_app_parameters(self): self.commentGrammars.ignore(pyparsing.quotedString).setParseAction(lambda x: '') @@ -1021,7 +1007,7 @@ class Cmd(cmd.Cmd): Both items are None """ unclosed_quote = '' - quotes_to_try = copy.copy(QUOTES) + quotes_to_try = copy.copy(constants.QUOTES) tmp_line = line[:endidx] tmp_endidx = endidx @@ -1064,7 +1050,7 @@ class Cmd(cmd.Cmd): for cur_initial_token in initial_tokens: # Save tokens up to 1 character in length or quoted tokens. No need to parse these. - if len(cur_initial_token) <= 1 or cur_initial_token[0] in QUOTES: + if len(cur_initial_token) <= 1 or cur_initial_token[0] in constants.QUOTES: raw_tokens.append(cur_initial_token) continue @@ -1076,10 +1062,10 @@ class Cmd(cmd.Cmd): cur_raw_token = '' while True: - if cur_char not in REDIRECTION_CHARS: + if cur_char not in constants.REDIRECTION_CHARS: # Keep appending to cur_raw_token until we hit a redirect char - while cur_char not in REDIRECTION_CHARS: + while cur_char not in constants.REDIRECTION_CHARS: cur_raw_token += cur_char cur_index += 1 if cur_index < len(cur_initial_token): @@ -1485,7 +1471,7 @@ class Cmd(cmd.Cmd): if len(raw_tokens) > 1: # Build a list of all redirection tokens - all_redirects = REDIRECTION_CHARS + ['>>'] + all_redirects = constants.REDIRECTION_CHARS + ['>>'] # Check if there are redirection strings prior to the token being completed seen_pipe = False @@ -1695,7 +1681,7 @@ class Cmd(cmd.Cmd): raw_completion_token = raw_tokens[-1] # Check if the token being completed has an opening quote - if raw_completion_token and raw_completion_token[0] in QUOTES: + if raw_completion_token and raw_completion_token[0] in constants.QUOTES: # Since the token is still being completed, we know the opening quote is unclosed unclosed_quote = raw_completion_token[0] @@ -2391,11 +2377,11 @@ class Cmd(cmd.Cmd): readline.set_completer(self.complete) # Break words on whitespace and quotes when tab completing - completer_delims = " \t\n" + ''.join(QUOTES) + completer_delims = " \t\n" + ''.join(constants.QUOTES) if self.allow_redirection: # If redirection is allowed, then break words on those characters too - completer_delims += ''.join(REDIRECTION_CHARS) + completer_delims += ''.join(constants.REDIRECTION_CHARS) readline.set_completer_delims(completer_delims) @@ -2840,13 +2826,13 @@ Usage: Usage: unalias [-a] name [name ...] # Check if the token is quoted. Since shlex.split() passed, there isn't # an unclosed quote, so we only need to check the first character. first_char = tokens[index][0] - if first_char in QUOTES: + if first_char in constants.QUOTES: tokens[index] = strip_quotes(tokens[index]) tokens[index] = os.path.expanduser(tokens[index]) # Restore the quotes - if first_char in QUOTES: + if first_char in constants.QUOTES: tokens[index] = first_char + tokens[index] + first_char expanded_command = ' '.join(tokens) @@ -3713,13 +3699,13 @@ class Cmd2TestCase(unittest.TestCase): def _test_transcript(self, fname, transcript): line_num = 0 finished = False - line = strip_ansi(next(transcript)) + line = utils.strip_ansi(next(transcript)) line_num += 1 while not finished: # Scroll forward to where actual commands begin while not line.startswith(self.cmdapp.visible_prompt): try: - line = strip_ansi(next(transcript)) + line = utils.strip_ansi(next(transcript)) except StopIteration: finished = True break @@ -3743,13 +3729,13 @@ class Cmd2TestCase(unittest.TestCase): self.cmdapp.onecmd_plus_hooks(command) result = self.cmdapp.stdout.read() # Read the expected result from transcript - if strip_ansi(line).startswith(self.cmdapp.visible_prompt): + if utils.strip_ansi(line).startswith(self.cmdapp.visible_prompt): message = '\nFile {}, line {}\nCommand was:\n{}\nExpected: (nothing)\nGot:\n{}\n'.format( fname, line_num, command, result) self.assert_(not (result.strip()), message) continue expected = [] - while not strip_ansi(line).startswith(self.cmdapp.visible_prompt): + while not utils.strip_ansi(line).startswith(self.cmdapp.visible_prompt): expected.append(line) try: line = next(transcript) |