Coverage for /pythoncovmergedfiles/medio/medio/usr/local/lib/python3.8/site-packages/redis/commands/parser.py: 10%
90 statements
« prev ^ index » next coverage.py v7.2.7, created at 2023-06-07 07:16 +0000
« prev ^ index » next coverage.py v7.2.7, created at 2023-06-07 07:16 +0000
1from redis.exceptions import RedisError, ResponseError
2from redis.utils import str_if_bytes
5class CommandsParser:
6 """
7 Parses Redis commands to get command keys.
8 COMMAND output is used to determine key locations.
9 Commands that do not have a predefined key location are flagged with
10 'movablekeys', and these commands' keys are determined by the command
11 'COMMAND GETKEYS'.
12 """
14 def __init__(self, redis_connection):
15 self.commands = {}
16 self.initialize(redis_connection)
18 def initialize(self, r):
19 commands = r.execute_command("COMMAND")
20 uppercase_commands = []
21 for cmd in commands:
22 if any(x.isupper() for x in cmd):
23 uppercase_commands.append(cmd)
24 for cmd in uppercase_commands:
25 commands[cmd.lower()] = commands.pop(cmd)
26 self.commands = commands
28 def parse_subcommand(self, command, **options):
29 cmd_dict = {}
30 cmd_name = str_if_bytes(command[0])
31 cmd_dict["name"] = cmd_name
32 cmd_dict["arity"] = int(command[1])
33 cmd_dict["flags"] = [str_if_bytes(flag) for flag in command[2]]
34 cmd_dict["first_key_pos"] = command[3]
35 cmd_dict["last_key_pos"] = command[4]
36 cmd_dict["step_count"] = command[5]
37 if len(command) > 7:
38 cmd_dict["tips"] = command[7]
39 cmd_dict["key_specifications"] = command[8]
40 cmd_dict["subcommands"] = command[9]
41 return cmd_dict
43 # As soon as this PR is merged into Redis, we should reimplement
44 # our logic to use COMMAND INFO changes to determine the key positions
45 # https://github.com/redis/redis/pull/8324
46 def get_keys(self, redis_conn, *args):
47 """
48 Get the keys from the passed command.
50 NOTE: Due to a bug in redis<7.0, this function does not work properly
51 for EVAL or EVALSHA when the `numkeys` arg is 0.
52 - issue: https://github.com/redis/redis/issues/9493
53 - fix: https://github.com/redis/redis/pull/9733
55 So, don't use this function with EVAL or EVALSHA.
56 """
57 if len(args) < 2:
58 # The command has no keys in it
59 return None
61 cmd_name = args[0].lower()
62 if cmd_name not in self.commands:
63 # try to split the command name and to take only the main command,
64 # e.g. 'memory' for 'memory usage'
65 cmd_name_split = cmd_name.split()
66 cmd_name = cmd_name_split[0]
67 if cmd_name in self.commands:
68 # save the splitted command to args
69 args = cmd_name_split + list(args[1:])
70 else:
71 # We'll try to reinitialize the commands cache, if the engine
72 # version has changed, the commands may not be current
73 self.initialize(redis_conn)
74 if cmd_name not in self.commands:
75 raise RedisError(
76 f"{cmd_name.upper()} command doesn't exist in Redis commands"
77 )
79 command = self.commands.get(cmd_name)
80 if "movablekeys" in command["flags"]:
81 keys = self._get_moveable_keys(redis_conn, *args)
82 elif "pubsub" in command["flags"] or command["name"] == "pubsub":
83 keys = self._get_pubsub_keys(*args)
84 else:
85 if (
86 command["step_count"] == 0
87 and command["first_key_pos"] == 0
88 and command["last_key_pos"] == 0
89 ):
90 is_subcmd = False
91 if "subcommands" in command:
92 subcmd_name = f"{cmd_name}|{args[1].lower()}"
93 for subcmd in command["subcommands"]:
94 if str_if_bytes(subcmd[0]) == subcmd_name:
95 command = self.parse_subcommand(subcmd)
96 is_subcmd = True
98 # The command doesn't have keys in it
99 if not is_subcmd:
100 return None
101 last_key_pos = command["last_key_pos"]
102 if last_key_pos < 0:
103 last_key_pos = len(args) - abs(last_key_pos)
104 keys_pos = list(
105 range(command["first_key_pos"], last_key_pos + 1, command["step_count"])
106 )
107 keys = [args[pos] for pos in keys_pos]
109 return keys
111 def _get_moveable_keys(self, redis_conn, *args):
112 """
113 NOTE: Due to a bug in redis<7.0, this function does not work properly
114 for EVAL or EVALSHA when the `numkeys` arg is 0.
115 - issue: https://github.com/redis/redis/issues/9493
116 - fix: https://github.com/redis/redis/pull/9733
118 So, don't use this function with EVAL or EVALSHA.
119 """
120 pieces = []
121 cmd_name = args[0]
122 # The command name should be splitted into separate arguments,
123 # e.g. 'MEMORY USAGE' will be splitted into ['MEMORY', 'USAGE']
124 pieces = pieces + cmd_name.split()
125 pieces = pieces + list(args[1:])
126 try:
127 keys = redis_conn.execute_command("COMMAND GETKEYS", *pieces)
128 except ResponseError as e:
129 message = e.__str__()
130 if (
131 "Invalid arguments" in message
132 or "The command has no key arguments" in message
133 ):
134 return None
135 else:
136 raise e
137 return keys
139 def _get_pubsub_keys(self, *args):
140 """
141 Get the keys from pubsub command.
142 Although PubSub commands have predetermined key locations, they are not
143 supported in the 'COMMAND's output, so the key positions are hardcoded
144 in this method
145 """
146 if len(args) < 2:
147 # The command has no keys in it
148 return None
149 args = [str_if_bytes(arg) for arg in args]
150 command = args[0].upper()
151 keys = None
152 if command == "PUBSUB":
153 # the second argument is a part of the command name, e.g.
154 # ['PUBSUB', 'NUMSUB', 'foo'].
155 pubsub_type = args[1].upper()
156 if pubsub_type in ["CHANNELS", "NUMSUB"]:
157 keys = args[2:]
158 elif command in ["SUBSCRIBE", "PSUBSCRIBE", "UNSUBSCRIBE", "PUNSUBSCRIBE"]:
159 # format example:
160 # SUBSCRIBE channel [channel ...]
161 keys = list(args[1:])
162 elif command == "PUBLISH":
163 # format example:
164 # PUBLISH channel message
165 keys = [args[1]]
166 return keys