def split_command_args(command, all_commands): """ Split Redis command text into command and args. :param command: redis command string, with args :param all_commands: full redis commands list """ command = command.lstrip() upper_raw_command = command.upper() for command_name in all_commands: total_equal = command_name == upper_raw_command ends_with_space = upper_raw_command.startswith(command_name + " ") if total_equal or ends_with_space: name_len = len(command_name) input_command = command[:name_len] input_args = command[name_len:] break else: logger.info("Utils split command] Not valid") raise InvalidArguments(f"`{command}` is not a valide Redis Command") args = list(_strip_quote_args(input_args)) logger.debug( f"[Utils split command] Command: {input_command} Args: {args}") return input_command, args
def send_command(self, command, completer): """ Send command to redis-server, return parsed response. :param command: text command, not parsed :param completer: RedisGrammarCompleter will update completer based on redis response. eg: update key completer after ``keys`` command """ # Parse command-name and args upper_raw_command = command.upper() for command_name in all_commands: if upper_raw_command.startswith(command_name): l = len(command_name) input_command = command[:l] input_args = command[l:] break else: raise InvalidArguments(r"`{command} is not a valide Redis Command") args = list(self._strip_quote_args(input_args)) logger.debug(f"[Parsed comamnd name] {input_command}") logger.debug(f"[Parsed comamnd args] {args}") redis_resp = self.execute_command(completer, input_command, *args) return redis_resp
def _strip_quote_args(self, s): """ Given string s, split it into args.(Like bash paring) Handle with all quote cases. Raise ``InvalidArguments`` if quotes not match :return: args list. """ sperator = re.compile(r"\s") word = [] in_quote = None pre_back_slash = False for char in s: if in_quote: # close quote if char == in_quote: if not pre_back_slash: yield from self._valide_token(word) word = [] in_quote = None else: # previous char is \ , merge with current " word[-1] = char else: word.append(char) # not in quote else: # sperator if sperator.match(char): if word: yield from self._valide_token(word) word = [] else: word.append(char) # open quotes elif char in ["'", '"']: in_quote = char else: word.append(char) if char == "\\" and not pre_back_slash: pre_back_slash = True else: pre_back_slash = False if word: yield from self._valide_token(word) # quote not close if in_quote: raise InvalidArguments()
def split_command_args(command, all_commands): """ Split Redis command text into command and args. :param command: redis command string, with args :param all_commands: full redis commands list """ command = command.strip() upper_command_list = command.upper().split() for command_name in all_commands: _command_name = command_name.split() _command_length = len(_command_name) if upper_command_list[:_command_length] == _command_name: input_command = " ".join(command.split()[:_command_length]) input_args = " ".join(command.split()[_command_length:]) break else: raise InvalidArguments(f"`{command}` is not a valide Redis Command") args = list(_strip_quote_args(input_args)) return input_command, args
def split_command_args(command, all_commands): """ Split Redis command text into command and args. :param command: redis command string, with args :param all_commands: full redis commands list """ command = command.lstrip() upper_raw_command = command.upper() for command_name in all_commands: if upper_raw_command.startswith(command_name): name_len = len(command_name) input_command = command[:name_len] input_args = command[name_len:] break else: raise InvalidArguments(f"`{command}` is not a valide Redis Command") args = list(_strip_quote_args(input_args)) logger.debug(f"[Parsed comamnd name] {input_command}") logger.debug(f"[Parsed comamnd args] {args}") return input_command, args