Ejemplo n.º 1
0
def pan_down(event):
    """ Pans the example pan down"""
    global _SECTION
    shell_telemetry.track_key('ControlN')

    if _SECTION < 10:
        _SECTION += 1
def pan_up(event):
    """ Pans the example pan up"""
    global _SECTION
    shell_telemetry.track_key('ControlY')

    if _SECTION > 1:
        _SECTION -= 1
Ejemplo n.º 3
0
def pan_up(event):
    """ Pans the example pan up"""
    global _SECTION
    shell_telemetry.track_key('ControlY')

    if _SECTION > 1:
        _SECTION -= 1
def pan_down(event):
    """ Pans the example pan down"""
    global _SECTION
    shell_telemetry.track_key('ControlN')

    if _SECTION < 10:
        _SECTION += 1
Ejemplo n.º 5
0
    def _special_cases(self, cmd, outside):
        break_flag = False
        continue_flag = False
        args = parse_quotes(cmd)
        cmd_stripped = cmd.strip()
        if cmd_stripped and cmd.split(' ', 1)[0].lower() == 'az':
            telemetry.track_ssg('az', cmd)
            cmd = ' '.join(cmd.split()[1:])
        if self.default_command:
            cmd = self.default_command + " " + cmd

        if cmd_stripped == "quit" or cmd_stripped == "exit":
            break_flag = True
        elif cmd_stripped == "clear-history":
            # clears the history, but only when you restart
            outside = True
            cmd = 'echo -n "" >' +\
                os.path.join(
                    SHELL_CONFIG_DIR(),
                    SHELL_CONFIGURATION.get_history())
        elif cmd_stripped == CLEAR_WORD:
            outside = True
            cmd = CLEAR_WORD
        if cmd_stripped:
            if cmd_stripped[0] == SELECT_SYMBOL['outside']:
                cmd = cmd_stripped[1:]
                outside = True
                if cmd.strip() and cmd.split()[0] == 'cd':
                    self.handle_cd(parse_quotes(cmd))
                    continue_flag = True
                telemetry.track_ssg('outside', '')

            elif cmd_stripped[0] == SELECT_SYMBOL['exit_code']:
                meaning = "Success" if self.last_exit == 0 else "Failure"

                print(meaning + ": " + str(self.last_exit), file=self.output)
                continue_flag = True
                telemetry.track_ssg('exit code', '')
            elif SELECT_SYMBOL['query'] in cmd_stripped and self.last and self.last.result:
                continue_flag = self.handle_jmespath_query(args)
                telemetry.track_ssg('query', '')

            elif args[0] == '--version' or args[0] == '-v':
                try:
                    continue_flag = True
                    show_version_info_exit(self.output)
                except SystemExit:
                    pass
            elif "|" in cmd or ">" in cmd:
                # anything I don't parse, send off
                outside = True
                cmd = "az " + cmd

            elif SELECT_SYMBOL['example'] in cmd:
                cmd, continue_flag = self.handle_example(cmd, continue_flag)
                telemetry.track_ssg('tutorial', cmd)
            elif len(cmd_stripped) > 2 and SELECT_SYMBOL['scope'] == cmd_stripped[0:2]:
                continue_flag, cmd = self.handle_scoping_input(continue_flag, cmd, cmd_stripped)

        return break_flag, continue_flag, outside, cmd
Ejemplo n.º 6
0
    def run(self):
        """ starts the REPL """
        telemetry.start()
        from azure.cli.core.application import APPLICATION
        APPLICATION.get_progress_controller = self.progress_patch

        # refresh the cache and completer
        self.command_table_thread = LoadCommandTableThread(
            restart_completer, self)
        self.command_table_thread.start()

        from azclishell.configuration import SHELL_HELP
        self.cli.buffers['symbols'].reset(
            initial_document=Document(u'{}'.format(SHELL_HELP)))
        while True:
            try:
                try:
                    document = self.cli.run(reset_current_buffer=True)
                    text = document.text
                    if not text:
                        # not input
                        self.set_prompt()
                        continue
                    cmd = text
                    outside = False

                except AttributeError:
                    # when the user pressed Control D
                    break
                else:
                    b_flag, c_flag, outside, cmd = self._special_cases(
                        cmd, outside)
                    if not self.default_command:
                        self.history.append(text)
                    if b_flag:
                        break
                    if c_flag:
                        self.set_prompt()
                        continue

                    self.set_prompt()

                    if outside:
                        subprocess.Popen(cmd, shell=True).communicate()
                    else:
                        self.cli_execute(cmd)
                        # because I catch the sys exit, I have to push out
                        cli_telemetry.conclude()

            except (KeyboardInterrupt, ValueError):
                # CTRL C
                self.set_prompt()
                continue

        print('Have a lovely day!!', file=self.output)
        telemetry.conclude()
Ejemplo n.º 7
0
    def run(self):
        """ starts the REPL """
        telemetry.start()
        from azure.cli.core.application import APPLICATION
        APPLICATION.get_progress_controller = self.progress_patch

        # refresh the cache and completer
        self.command_table_thread = LoadCommandTableThread(restart_completer, self)
        self.command_table_thread.start()

        from azclishell.configuration import SHELL_HELP
        self.cli.buffers['symbols'].reset(
            initial_document=Document(u'{}'.format(SHELL_HELP)))
        while True:
            try:
                try:
                    document = self.cli.run(reset_current_buffer=True)
                    text = document.text
                    if not text:
                        # not input
                        self.set_prompt()
                        continue
                    cmd = text
                    outside = False

                except AttributeError:
                    # when the user pressed Control D
                    break
                else:
                    b_flag, c_flag, outside, cmd = self._special_cases(cmd, outside)
                    if not self.default_command:
                        self.history.append(text)
                    if b_flag:
                        break
                    if c_flag:
                        self.set_prompt()
                        continue

                    self.set_prompt()

                    if outside:
                        subprocess.Popen(cmd, shell=True).communicate()
                    else:
                        self.cli_execute(cmd)
                        # because I catch the sys exit, I have to push out
                        cli_telemetry.conclude()

            except (KeyboardInterrupt, ValueError):
                # CTRL C
                self.set_prompt()
                continue

        print('Have a lovely day!!', file=self.output)
        telemetry.conclude()
Ejemplo n.º 8
0
    def handle_scoping_input(self, continue_flag, cmd, text):
        default_split = text.partition(SELECT_SYMBOL['scope'])[2].split()
        cmd = cmd.replace(SELECT_SYMBOL['scope'], '')

        if text and SELECT_SYMBOL['scope'] == text[0:2]:
            continue_flag = True

            if not default_split:
                self.default_command = ""
                set_scope("", add=False)
                print('unscoping all')

                return continue_flag, cmd

            while default_split:
                if not text:
                    value = ''
                else:
                    value = default_split[0]

                if self.default_command:
                    tree_val = self.default_command + " " + value
                else:
                    tree_val = value

                if in_tree(self.completer.command_tree, tree_val.strip()):
                    self.set_scope(value)
                    print("defaulting: " + value)
                    cmd = cmd.replace(SELECT_SYMBOL['scope'], '')
                    telemetry.track_ssg('scope command', value)

                elif SELECT_SYMBOL['unscope'] == default_split[0] and \
                        len(self.default_command.split()) > 0:

                    value = self.default_command.split()[-1]
                    self.default_command = ' ' + ' '.join(
                        self.default_command.split()[:-1])

                    if not self.default_command.strip():
                        self.default_command = self.default_command.strip()
                    set_scope(self.default_command, add=False)
                    print('unscoping: ' + value)

                elif SELECT_SYMBOL['unscope'] not in text:
                    print("Scope must be a valid command")

                default_split = default_split[1:]
        else:
            return continue_flag, cmd
        return continue_flag, cmd
Ejemplo n.º 9
0
    def _special_cases(self, text, cmd, outside):
        break_flag = False
        continue_flag = False

        if text and len(text.split()) > 0 and text.split()[0].lower() == 'az':
            telemetry.track_ssg('az', text)
            cmd = ' '.join(text.split()[1:])
        if self.default_command:
            cmd = self.default_command + " " + cmd

        if text.strip() == "quit" or text.strip() == "exit":
            break_flag = True
        elif text.strip(
        ) == "clear-history":  # clears the history, but only when you restart
            outside = True
            cmd = 'echo -n "" >' +\
                os.path.join(
                    SHELL_CONFIG_DIR(),
                    SHELL_CONFIGURATION.get_history())
        elif text.strip() == CLEAR_WORD:
            outside = True
            cmd = CLEAR_WORD
        if text:
            if text[0] == SELECT_SYMBOL['outside']:
                cmd = text[1:]
                outside = True
                if cmd.strip() and cmd.split()[0] == 'cd':
                    handle_cd(parse_quotes(cmd))
                    continue_flag = True
                telemetry.track_ssg('outside', '')

            elif text[0] == SELECT_SYMBOL['exit_code']:
                meaning = "Success" if self.last_exit == 0 else "Failure"

                print(meaning + ": " + str(self.last_exit))
                continue_flag = True
                telemetry.track_ssg('exit code', '')

            elif text[0] == SELECT_SYMBOL['query']:  # query previous output
                continue_flag = self.handle_jmespath_query(text, continue_flag)
            elif text[0] == '--version' or text[0] == '-v':
                try:
                    continue_flag = True
                    show_version_info_exit(sys.stdout)
                except SystemExit:
                    pass
            elif "|" in text or ">" in text:  # anything I don't parse, send off
                outside = True
                cmd = "az " + cmd

            elif SELECT_SYMBOL['example'] in text:
                cmd, continue_flag = self.handle_example(cmd, continue_flag)
                telemetry.track_ssg('tutorial', text)

        continue_flag, cmd = self.handle_scoping_input(continue_flag, cmd,
                                                       text)

        return break_flag, continue_flag, outside, cmd
Ejemplo n.º 10
0
    def handle_scoping_input(self, continue_flag, cmd, text):
        """ handles what to do with a scoping gesture """
        default_split = text.partition(SELECT_SYMBOL['scope'])[2].split()
        cmd = cmd.replace(SELECT_SYMBOL['scope'], '')

        continue_flag = True

        if not default_split:
            self.default_command = ""
            set_scope("", add=False)
            print('unscoping all', file=self.output)

            return continue_flag, cmd

        while default_split:
            if not text:
                value = ''
            else:
                value = default_split[0]

            if self.default_command:
                tree_val = self.default_command + " " + value
            else:
                tree_val = value

            if in_tree(self.completer.command_tree, tree_val.strip()):
                self.set_scope(value)
                print("defaulting: " + value, file=self.output)
                cmd = cmd.replace(SELECT_SYMBOL['scope'], '')
                telemetry.track_ssg('scope command', value)

            elif SELECT_SYMBOL['unscope'] == default_split[0] and \
                    len(self.default_command.split()) > 0:

                value = self.default_command.split()[-1]
                self.default_command = ' ' + ' '.join(self.default_command.split()[:-1])

                if not self.default_command.strip():
                    self.default_command = self.default_command.strip()
                set_scope(self.default_command, add=False)
                print('unscoping: ' + value, file=self.output)

            elif SELECT_SYMBOL['unscope'] not in text:
                print("Scope must be a valid command", file=self.output)

            default_split = default_split[1:]
        return continue_flag, cmd
Ejemplo n.º 11
0
 def handle_jmespath_query(self, text, continue_flag):
     if self.last and self.last.result:
         if hasattr(self.last.result, '__dict__'):
             input_dict = dict(self.last.result)
         else:
             input_dict = self.last.result
         try:
             query_text = text.partition(SELECT_SYMBOL['query'])[2]
             result = ""
             if query_text:
                 result = jmespath.search(query_text, input_dict)
             if isinstance(result, str):
                 print(result)
             else:
                 print(json.dumps(result, sort_keys=True, indent=2))
         except jmespath.exceptions.ParseError:
             print("Invalid Query")
     continue_flag = True
     telemetry.track_ssg('query', text)
     return continue_flag
Ejemplo n.º 12
0
def config_settings(event):
    """ opens the configuration """
    global PROMPTING
    shell_telemetry.track_key('F1')

    PROMPTING = True
    config = azclishell.configuration.CONFIGURATION
    answer = ""
    questions = {
        "Do you want command descriptions": "command_description",
        "Do you want parameter descriptions": "param_description",
        "Do you want examples": "examples"
    }
    for question in questions:
        while answer.lower() != 'y' and answer.lower() != 'n':
            answer = prompt(u'\n%s (y/n): ' % question)
        config.set_val('Layout', questions[question], format_response(answer))
        answer = ""

    PROMPTING = False
    print("\nPlease restart the interactive mode for changes to take effect.\n\n")
    event.cli.set_return_value(event.cli.current_buffer)
def config_settings(event):
    """ opens the configuration """
    global PROMPTING
    shell_telemetry.track_key('F1')

    PROMPTING = True
    config = azclishell.configuration.CONFIGURATION
    answer = ""
    questions = {
        "Do you want command descriptions": "command_description",
        "Do you want parameter descriptions": "param_description",
        "Do you want examples": "examples"
    }
    for question in questions:
        while answer.lower() != 'y' and answer.lower() != 'n':
            answer = prompt(u'\n%s (y/n): ' % question)
        config.set_val('Layout', questions[question], format_response(answer))
        answer = ""

    PROMPTING = False
    print(
        "\nPlease restart the interactive mode for changes to take effect.\n\n"
    )
    event.cli.set_return_value(event.cli.current_buffer)
Ejemplo n.º 14
0
    def _special_cases(self, text, cmd, outside):
        break_flag = False
        continue_flag = False
        args = parse_quotes(text)
        args_no_quotes = []
        text_stripped = text.strip()
        for arg in args:
            args_no_quotes.append(arg.strip("/'").strip('/"'))

        if text and len(text.split()) > 0 and text.split()[0].lower() == 'az':
            telemetry.track_ssg('az', text)
            cmd = ' '.join(text.split()[1:])
        if self.default_command:
            cmd = self.default_command + " " + cmd

        if text_stripped == "quit" or text_stripped == "exit":
            break_flag = True
        elif text_stripped == "clear-history":  # clears the history, but only when you restart
            outside = True
            cmd = 'echo -n "" >' +\
                os.path.join(
                    SHELL_CONFIG_DIR(),
                    SHELL_CONFIGURATION.get_history())
        elif text_stripped == CLEAR_WORD:
            outside = True
            cmd = CLEAR_WORD
        if text_stripped:
            if text_stripped[0] == SELECT_SYMBOL['outside']:
                cmd = text_stripped[1:]
                outside = True
                if cmd.strip() and cmd.split()[0] == 'cd':
                    self.handle_cd(parse_quotes(cmd))
                    continue_flag = True
                telemetry.track_ssg('outside', '')

            elif text_stripped[0] == SELECT_SYMBOL['exit_code']:
                meaning = "Success" if self.last_exit == 0 else "Failure"

                print(meaning + ": " + str(self.last_exit), file=self.output)
                continue_flag = True
                telemetry.track_ssg('exit code', '')
            elif validate_contains_query(
                    args_no_quotes,
                    SELECT_SYMBOL['query']) and self.last and self.last.result:
                continue_flag = self.handle_jmespath_query(
                    args_no_quotes, continue_flag)
                telemetry.track_ssg('query', '')

            elif args[0] == '--version' or args[0] == '-v':
                try:
                    continue_flag = True
                    show_version_info_exit(self.output)
                except SystemExit:
                    pass
            elif "|" in text or ">" in text:  # anything I don't parse, send off
                outside = True
                cmd = "az " + cmd

            elif SELECT_SYMBOL['example'] in text:
                cmd, continue_flag = self.handle_example(cmd, continue_flag)
                telemetry.track_ssg('tutorial', text)
            elif len(text_stripped
                     ) > 2 and SELECT_SYMBOL['scope'] == text_stripped[0:2]:
                continue_flag, cmd = self.handle_scoping_input(
                    continue_flag, cmd, text_stripped)

        return break_flag, continue_flag, outside, cmd
Ejemplo n.º 15
0
def toggle_default(event):
    """ shows the defaults"""
    global SHOW_DEFAULT
    shell_telemetry.track_key('F2')

    SHOW_DEFAULT = not SHOW_DEFAULT
Ejemplo n.º 16
0
def toggle_symbols(event):
    """ shows the symbol bindings"""
    global SYMBOLS
    shell_telemetry.track_key('F3')

    SYMBOLS = not SYMBOLS
def exit_(event):
    """ exits the program when Control D is pressed """
    shell_telemetry.track_key('ControlD')
    event.cli.set_return_value(None)
def toggle_symbols(event):
    """ shows the symbol bindings"""
    global SYMBOLS
    shell_telemetry.track_key('F3')

    SYMBOLS = not SYMBOLS
def toggle_default(event):
    """ shows the defaults"""
    global SHOW_DEFAULT
    shell_telemetry.track_key('F2')

    SHOW_DEFAULT = not SHOW_DEFAULT
Ejemplo n.º 20
0
def exit_(event):
    """ exits the program when Control D is pressed """
    shell_telemetry.track_key('ControlD')
    event.cli.set_return_value(None)