Beispiel #1
0
    def cmdloop(self, intro=None):  # pylint: disable=too-many-branches
        self.preloop()
        if self.completekey and readline:
            self.old_completer = readline.get_completer()
            self.old_delims = readline.get_completer_delims()
            readline.set_completer(self.complete)
            readline.set_completer_delims(' \n\t')
            to_parse = self.completekey + ': complete'
            if 'libedit' in readline.__doc__:
                # Special case for mac OSX
                to_parse = 'bind ^I rl_complete'
            readline.parse_and_bind(to_parse)
        try:
            if intro is not None:
                self.intro = intro
            if self.intro:
                click.echo(self.intro, file=self.stdout)
            stop = None
            while not stop:
                if self.cmdqueue:
                    line = self.cmdqueue.pop(0)
                else:
                    try:
                        line = get_input(self.get_prompt())
                    except EOFError:
                        # We just want to quit here instead of changing the arg to EOF
                        click.echo(file=self.stdout)
                        break
                    except KeyboardInterrupt:
                        # We don't want to exit the shell on a keyboard interrupt
                        click.echo(file=self.stdout)
                        click.echo('KeyboardInterrupt', file=self.stdout)
                        continue
                line = self.precmd(line)
                stop = self.onecmd(line)
                stop = self.postcmd(stop, line)

        finally:
            self.postloop()
            if self.completekey and readline:
                readline.set_completer(self.old_completer)
                readline.set_completer_delims(self.old_delims)
            # Finisher callback on the context
            if self.on_finished:
                self.on_finished(self.ctx)
Beispiel #2
0
    def cmdloop(self, intro=None):
        self.preloop()
        if self.completekey and readline:
            self.old_completer = readline.get_completer()
            self.old_delims = readline.get_completer_delims()
            readline.set_completer(self.complete)
            readline.set_completer_delims(' \n\t')
            to_parse = self.completekey + ': complete'
            readline.parse_and_bind(to_parse)
        try:
            if intro is not None:
                self.intro = intro
            if self.intro:
                click.echo(self.intro, file=self.stdout)
            stop = None
            while not stop:
                if self.cmdqueue:
                    line = self.cmdqueue.pop(0)
                else:
                    try:
                        line = get_input(self.get_prompt())
                    except EOFError:
                        # We just want to quit here instead of changing the arg to EOF
                        click.echo(file=self.stdout)
                        break
                    except KeyboardInterrupt:
                        # We don't want to exit the shell on a keyboard interrupt
                        click.echo(file=self.stdout)
                        click.echo('KeyboardInterrupt', file=self.stdout)
                        continue
                line = self.precmd(line)
                stop = self.onecmd(line)
                stop = self.postcmd(stop, line)

        finally:
            self.postloop()
            if self.completekey and readline:
                readline.set_completer(self.old_completer)
                readline.set_completer_delims(self.old_delims)
Beispiel #3
0
    def cmdloop(self, intro=None):  # pylint: disable=too-many-branches
        self.preloop()
        if self.completekey and readline:
            self.old_completer = readline.get_completer()
            readline.set_completer(self.complete)
            to_parse = self.completekey + ': complete'
            if 'libedit' in readline.__doc__:
                # Special case for mac OSX
                to_parse = 'bind ^I rl_complete'
            readline.parse_and_bind(to_parse)
        try:
            if intro is not None:
                self.intro = intro
            if self.intro:
                click.echo(self.intro, file=self.stdout)
            stop = None
            while not stop:
                if self.cmdqueue:
                    line = self.cmdqueue.pop(0)
                else:
                    try:
                        line = get_input(self.get_prompt())
                    except EOFError:
                        # We just want to quit here instead of changing the arg to EOF
                        click.echo(file=self.stdout)
                        break
                    except KeyboardInterrupt:
                        # We don't want to exit the shell on a keyboard interrupt
                        click.echo(file=self.stdout)
                        click.echo('KeyboardInterrupt', file=self.stdout)
                        continue
                line = self.precmd(line)
                stop = self.onecmd(line)
                stop = self.postcmd(stop, line)

        finally:
            self.postloop()
            if self.completekey and readline:
                readline.set_completer(self.old_completer)
Beispiel #4
0
    def cmdloop(self, intro=None):
        self.preloop()

        # Readline Handling
        if self.readline:
            if self.completekey and readline:
                self.old_completer = readline.get_completer()
                self.old_delims = readline.get_completer_delims()
                readline.set_completer(self.complete)
                readline.set_completer_delims(' \n\t')
                to_parse = self.completekey + ': complete'
                if readline.__doc__ and 'libedit' in readline.__doc__:
                    # Mac OSX
                    to_parse = 'bind ^I rl_complete'
                readline.parse_and_bind(to_parse)

        try:
            # Call an optional callback function before writing the intro and initializing/starting the shell
            if self.before_start:
                if callable(self.before_start): self.before_start()

            # Write an intro for the shell application
            if intro is not None:
                self.intro = intro
            if self.intro:
                click.echo(self.intro, file=self._stdout)
            stop = None

            if not self.readline:
                # Initialize Completion Tree for Master Shell
                if globs.__MASTER_SHELL__ == self.ctx.command.name:
                    BuildCompletionTree(self.ctx)

                # Initialize Prompter
                try:
                    from ._lexer import ShellLexer
                except:
                    pass
                from prompt_toolkit.output.color_depth import ColorDepth

                message = [
                    ('class:name', self.get_prompt()),
                    ('class:prompt', PROMPT_SYMBOL),
                ]

                self.prompter = PromptSession(
                    message,
                    style=colors.prompt_style,
                    color_depth=ColorDepth.TRUE_COLOR,
                    history=self.history,
                    enable_history_search=not self.complete_while_typing,
                    mouse_support=self.mouse_support,
                    completer=get_completer(self.fuzzy_completion),
                    complete_in_thread=self.complete_while_typing,
                    complete_while_typing=self.complete_while_typing,
                    lexer=PygmentsLexer(ShellLexer) if self.lexer else None)

                self.piped_prompter = PromptSession(
                    message,
                    style=colors.prompt_style,
                    color_depth=ColorDepth.TRUE_COLOR,
                    input=self._pipe_input,
                    key_bindings=None,
                    is_password=True,
                    lexer=PygmentsLexer(ShellLexer) if self.lexer else None)

            # Start Shell Application Loop
            while not stop:
                if self.cmdqueue:
                    line = self.cmdqueue.pop(0)
                else:
                    try:
                        if self.readline:
                            line = get_input(self.get_prompt() + PROMPT_SYMBOL)
                        else:
                            if not globs.__IS_REPEAT_EOF__:
                                line = self.prompter.prompt()
                            elif self._pipe_input:
                                if not globs.__IS_EXITING__:
                                    line = self.piped_prompter.prompt()
                                else:
                                    return

                    except EOFError:
                        if not globs.__IS_REPEAT_EOF__:
                            # Exits the Shell Application when stdin stream ends
                            click.echo(file=self._stdout)
                            break
                        else:
                            # Swap STDIN from Programmatic Input back to User Input
                            globs.__IS_REPEAT_EOF__ = False
                            if self.readline: sys.stdin = globs.__PREV_STDIN__
                            # Prevent empty lines from being created from null input
                            print('\n' + IGNORE_LINE)
                            continue

                    except KeyboardInterrupt:
                        # Do not exit the shell on a keyboard interrupt
                        try:
                            if line != '':
                                click.echo(file=self._stdout)
                                continue
                        except:
                            pass

                        # Prevent empty lines from being created from null input
                        if not self.readline: click.echo(IGNORE_LINE)
                        else: print('\n' + IGNORE_LINE)

                        continue

                # Safely handle calling command and pretty-displaying output / errors
                if line.strip():
                    if globs.__IS_REPEAT__:
                        if not globs.__IS_EXITING__:
                            # If stream source is from the 'repeat' command, display the "visible" repeated command
                            click.echo(globs.__LAST_COMMAND_VISIBLE__)

                    def fixTupleSpacing(val):
                        if '[' in val or ']' in val:
                            val = re.sub(r"(\[[\s])", '[', val)
                            val = re.sub(r"([\s]\])", ']', val)
                            val = re.sub(r"(\[,)", ']', val)
                            val = re.sub(r"(,\])", ']', val)
                        if ',' in val:
                            val = re.sub(r"(\",\")", "\", \"", val)
                            val = re.sub(r"([,]{1,999}.(?<=,))", ',', val)
                        return val

                    line = fixTupleSpacing(line)
                    globs.__CURRENT_LINE__ = line

                    try:
                        line = self.precmd(line)
                        stop = self.onecmd(line)
                        stop = self.postcmd(stop, line)
                        if not stop and not globs.__IS_EXITING__:
                            line = ''
                            click.echo(file=self._stdout)
                    except KeyboardInterrupt:
                        click.echo(file=self._stdout)
                        continue
                    finally:
                        # Will tell the next loop to switch stdin back to user input after completing a "repeated" command
                        if line[0:6] != 'repeat' and globs.__IS_REPEAT__:
                            globs.__IS_REPEAT__ = False
                            globs.__IS_REPEAT_EOF__ = True
                            if (not self.readline) and self._pipe_input:
                                if globs.__LAST_COMMAND__ and not globs.__IS_EXITING__:
                                    self._pipe_input.send_text(
                                        globs.__LAST_COMMAND__ + '\r')

                        elif self._pipe_input and globs.__IS_REPEAT_EOF__:
                            globs.__IS_REPEAT_EOF__ = False
                else:
                    # Prevent empty lines from being created from null input
                    if not self.readline: click.echo(IGNORE_LINE)
                    else: print(IGNORE_LINE)
                    continue

        finally:
            self.postloop()
            if self.completekey:
                try:
                    if self.readline:
                        readline.set_completer(self.old_completer)
                        readline.set_completer_delims(self.old_delims)
                except IOError:
                    pass