Beispiel #1
0
    def gen_cmd_and_param_completions(self, text):
        """ generates command and parameter completions """
        temp_command = str('')
        txtspt = text.split()
        for word in txtspt:
            if word.startswith("-"):
                self._is_command = False

            # building what the command is
            elif self._is_command:
                temp_command += ' ' + str(word) if temp_command else str(word)

            if self.branch.has_child(
                    word) and text[-1].isspace():  # moving down command tree
                self.branch = self.branch.get_child(word, self.branch.children)

        if len(text) > 0 and text[-1].isspace():
            if in_tree(self.command_tree, temp_command):
                self.curr_command = temp_command
            else:
                self._is_command = False
        else:
            self.curr_command = temp_command

        last_word = txtspt[-1]
        # this is for single char parameters
        if last_word.startswith("-") and not last_word.startswith("--"):
            self._is_command = False
            if self.has_parameters(self.curr_command):
                for param in self.command_parameters[self.curr_command]:
                    if self.validate_completion(param, last_word, text) and\
                            not param.startswith("--"):
                        yield Completion(
                            param,
                            -len(last_word),
                            display_meta=self.get_param_description(
                                self.curr_command + " " + str(param)).replace(
                                    '\n', ''))
        elif last_word.startswith("--"):  # for regular parameters
            self._is_command = False

            if self.has_parameters(
                    self.curr_command):  # Everything should, map to empty list
                for param in self.command_parameters[self.curr_command]:
                    if self.validate_completion(param, last_word, text):
                        yield Completion(
                            param,
                            -len(last_word),
                            display_meta=self.get_param_description(
                                self.curr_command + " " + str(param)).replace(
                                    '\n', ''))

        if self.branch.children is not None and self._is_command:  # all underneath commands
            for kid in self.branch.children:
                if self.validate_completion(kid.data, txtspt[-1], text, False):
                    yield Completion(str(kid.data), -len(txtspt[-1]))
    def gen_cmd_and_param_completions(self, text):
        """ generates command and parameter completions """
        temp_command = str('')
        txtspt = text.split()

        for word in txtspt:
            if word.startswith("-"):
                self._is_command = False

            # building what the command is
            elif self._is_command:
                temp_command += ' ' + str(word) if temp_command else str(word)

            mid_val = text.find(word) + len(word)
            # moving down command tree
            if self.branch.has_child(word) and len(text) > mid_val and text[mid_val].isspace():
                self.branch = self.branch.get_child(word, self.branch.children)

        if len(text) > 0 and text[-1].isspace():
            if in_tree(self.command_tree, temp_command):
                self.curr_command = temp_command
            else:
                self._is_command = False
        else:
            self.curr_command = temp_command

        last_word = txtspt[-1]
        # this is for single char parameters
        if last_word.startswith("-") and not last_word.startswith("--"):
            self._is_command = False
            if self.has_parameters(self.curr_command):
                for param in self.command_parameters[self.curr_command]:
                    if self.validate_completion(param, last_word, text) and\
                            not param.startswith("--"):
                        yield self.yield_param_completion(param, last_word)

        elif last_word.startswith("--"):  # for regular parameters
            self._is_command = False

            if self.has_parameters(self.curr_command):  # Everything should, map to empty list
                for param in self.command_parameters[self.curr_command]:
                    if self.validate_completion(param, last_word, text):
                        yield self.yield_param_completion(param, last_word)

        if self.branch.children and self._is_command:  # all underneath commands
            for kid in self.branch.children:
                if self.validate_completion(kid.data, txtspt[-1], text, False):
                    yield Completion(
                        str(kid.data), -len(txtspt[-1]))
        elif self._is_command and self.curr_command.strip() in self.command_parameters:
            for param in self.command_parameters[self.curr_command.strip()]:
                if param.startswith('--'):
                    yield self.yield_param_completion(param, '')
Beispiel #3
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
Beispiel #4
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
Beispiel #5
0
    def test_in_tree(self):
        # tests in tree
        tree4 = CommandBranch("CB1")
        tree3 = CommandBranch("Again")
        tree2 = CommandBranch("World")
        tree2.add_child(tree3)
        tree2.add_child(tree4)
        tree1 = CommandBranch("Hello")
        tree1.add_child(tree2)
        tree = CommandHead()
        tree.add_child(tree1)
        self.assertTrue(in_tree(tree3, 'Again'))
        self.assertTrue(in_tree(tree2, 'World Again'))
        self.assertTrue(in_tree(tree1, 'Hello World Again'))
        self.assertTrue(in_tree(tree1, 'Hello World CB1'))

        self.assertFalse(in_tree(tree1, 'World Hello CB1'))
        self.assertFalse(in_tree(tree, 'World Hello CB1'))
        self.assertFalse(in_tree(tree, 'Hello World Again CB1'))
Beispiel #6
0
    def test_in_tree(self):
        """ tests in tree """
        tree4 = CommandBranch("CB1")
        tree3 = CommandBranch("Again")
        tree2 = CommandBranch("World")
        tree2.add_child(tree3)
        tree2.add_child(tree4)
        tree1 = CommandBranch("Hello")
        tree1.add_child(tree2)
        tree = CommandHead()
        tree.add_child(tree1)
        self.assertTrue(in_tree(tree3, 'Again'))
        self.assertTrue(in_tree(tree2, 'World Again'))
        self.assertTrue(in_tree(tree1, 'Hello World Again'))
        self.assertTrue(in_tree(tree1, 'Hello World CB1'))

        self.assertFalse(in_tree(tree1, 'World Hello CB1'))
        self.assertFalse(in_tree(tree, 'World Hello CB1'))
        self.assertFalse(in_tree(tree, 'Hello World Again CB1'))