Ejemplo n.º 1
0
 def postparse_hook_stop(
         self,
         data: cmd2.plugin.PostparsingData) -> cmd2.plugin.PostparsingData:
     """A postparsing hook with requests application exit"""
     self.called_postparsing += 1
     data.stop = True
     return data
Ejemplo n.º 2
0
 def downcase_hook(self, data: cmd2.plugin.PostparsingData) -> cmd2.plugin.PostparsingData:
     """A hook to make uppercase commands lowercase."""
     command = data.statement.command.lower()
     data.statement = self.statement_parser.parse("{} {}".format(
         command,
         '' if data.statement.args is None else data.statement.args
     ))
     return data
Ejemplo n.º 3
0
    def pre_command_hook(self, params: cmd2.plugin.PostparsingData) -> cmd2.plugin.PostparsingData:
        # The statement object created from the user input is available as params.statement

        # Add the last argument connection_mode only for python methods. In this case it is always shell_mode
        # This argument will be another in bluetooth module
        if params.statement.command in self.shell_methods_name:
            new_raw_statement = params.statement.raw + ' ' + SHELL_MODE_CONNECTION
            params.statement = self.statement_parser.parse_command_only(new_raw_statement)

        # Modify params detecting if is python command or shell command
        # It needs to create a new statement, not modify the statement in params
        if params.statement.command not in self.keywords:
            # Modify raw argument adding shell command
            new_raw_statement = 'shell ' + params.statement.raw
            params.statement = self.statement_parser.parse_command_only(new_raw_statement)

        return params
Ejemplo n.º 4
0
 def abbrev_hook(self, data: cmd2.plugin.PostparsingData) -> cmd2.plugin.PostparsingData:
     """Accept unique abbreviated commands"""
     func = self.cmd_func(data.statement.command)
     if func is None:
         # check if the entered command might be an abbreviation
         possible_cmds = [cmd for cmd in self.get_all_commands() if cmd.startswith(data.statement.command)]
         if len(possible_cmds) == 1:
             raw = data.statement.raw.replace(data.statement.command, possible_cmds[0], 1)
             data.statement = self.statement_parser.parse(raw)
     return data
Ejemplo n.º 5
0
Archivo: hooks.py Proyecto: rowhit/cmd2
 def abbrev_hook(
         self,
         data: cmd2.plugin.PostparsingData) -> cmd2.plugin.PostparsingData:
     """Accept unique abbreviated commands"""
     target = 'do_' + data.statement.command
     if target not in dir(self):
         # check if the entered command might be an abbreviation
         funcs = [
             func for func in self.keywords
             if func.startswith(data.statement.command)
         ]
         if len(funcs) == 1:
             raw = data.statement.raw.replace(data.statement.command,
                                              funcs[0], 1)
             data.statement = self.statement_parser.parse(raw)
     return data
Ejemplo n.º 6
0
 def cmd2_abbrev_hook(
         self,
         data: cmd2.plugin.PostparsingData) -> cmd2.plugin.PostparsingData:
     """Postparsing hook which interprets abbreviated command names."""
     if self.abbrev:
         target = 'do_' + data.statement.command
         if target not in dir(self):
             # check if the entered command might be an abbreviation
             cmds = self.get_all_commands()
             funcs = [
                 func for func in cmds
                 if func.startswith(data.statement.command)
             ]
             if len(funcs) == 1:
                 raw = data.statement.raw.replace(data.statement.command,
                                                  funcs[0], 1)
                 data.statement = self.statement_parser.parse(raw)
     return data
Ejemplo n.º 7
0
    def add_whitespace_hook(self, data: cmd2.plugin.PostparsingData) -> cmd2.plugin.PostparsingData:
        """A hook to split alphabetic command names immediately followed by a number.

        l24 -> l 24
        list24 -> list 24
        list 24 -> list 24

        """
        command = data.statement.command
        # regular expression with looks for:
        #  ^ - the beginning of the string
        # ([^\s\d]+) - one or more non-whitespace non-digit characters, set as capture group 1
        # (\d+) - one or more digit characters, set as capture group 2
        command_pattern = re.compile(r'^([^\s\d]+)(\d+)')
        match = command_pattern.search(command)
        if match:
            data.statement = self.statement_parser.parse("{} {} {}".format(
                match.group(1),
                match.group(2),
                '' if data.statement.args is None else data.statement.args
            ))
        return data