| 1 |
import datetime |
| 2 |
|
| 3 |
from valkey.utils import str_if_bytes |
| 4 |
|
| 5 |
|
| 6 |
def timestamp_to_datetime(response): |
| 7 |
"Converts a unix timestamp to a Python datetime object" |
| 8 |
if not response: |
| 9 |
return None |
| 10 |
try: |
| 11 |
response = int(response) |
| 12 |
except ValueError: |
| 13 |
return None |
| 14 |
return datetime.datetime.fromtimestamp(response) |
| 15 |
|
| 16 |
|
| 17 |
def parse_debug_object(response): |
| 18 |
"Parse the results of Valkey's DEBUG OBJECT command into a Python dict" |
| 19 |
# The 'type' of the object is the first item in the response, but isn't |
| 20 |
# prefixed with a name |
| 21 |
response = str_if_bytes(response) |
| 22 |
response = "type:" + response |
| 23 |
response = dict(kv.split(":") for kv in response.split()) |
| 24 |
|
| 25 |
# parse some expected int values from the string response |
| 26 |
# note: this cmd isn't spec'd so these may not appear in all valkey versions |
| 27 |
int_fields = ("refcount", "serializedlength", "lru", "lru_seconds_idle") |
| 28 |
for field in int_fields: |
| 29 |
if field in response: |
| 30 |
response[field] = int(response[field]) |
| 31 |
|
| 32 |
return response |
| 33 |
|
| 34 |
|
| 35 |
def parse_info(response): |
| 36 |
"""Parse the result of Valkey's INFO command into a Python dict""" |
| 37 |
info = {} |
| 38 |
response = str_if_bytes(response) |
| 39 |
|
| 40 |
def get_value(value): |
| 41 |
if "," not in value and "=" not in value: |
| 42 |
try: |
| 43 |
if "." in value: |
| 44 |
return float(value) |
| 45 |
else: |
| 46 |
return int(value) |
| 47 |
except ValueError: |
| 48 |
return value |
| 49 |
elif "=" not in value: |
| 50 |
return [get_value(v) for v in value.split(",") if v] |
| 51 |
else: |
| 52 |
sub_dict = {} |
| 53 |
for item in value.split(","): |
| 54 |
if not item: |
| 55 |
continue |
| 56 |
if "=" in item: |
| 57 |
k, v = item.rsplit("=", 1) |
| 58 |
sub_dict[k] = get_value(v) |
| 59 |
else: |
| 60 |
sub_dict[item] = True |
| 61 |
return sub_dict |
| 62 |
|
| 63 |
for line in response.splitlines(): |
| 64 |
if line and not line.startswith("#"): |
| 65 |
if line.find(":") != -1: |
| 66 |
# Split, the info fields keys and values. |
| 67 |
# Note that the value may contain ':'. but the 'host:' |
| 68 |
# pseudo-command is the only case where the key contains ':' |
| 69 |
key, value = line.split(":", 1) |
| 70 |
if key == "cmdstat_host": |
| 71 |
key, value = line.rsplit(":", 1) |
| 72 |
|
| 73 |
if key == "module": |
| 74 |
# Hardcode a list for key 'modules' since there could be |
| 75 |
# multiple lines that started with 'module' |
| 76 |
info.setdefault("modules", []).append(get_value(value)) |
| 77 |
else: |
| 78 |
info[key] = get_value(value) |
| 79 |
else: |
| 80 |
# if the line isn't splittable, append it to the "__raw__" key |
| 81 |
info.setdefault("__raw__", []).append(line) |
| 82 |
|
| 83 |
return info |
| 84 |
|
| 85 |
|
| 86 |
def parse_memory_stats(response, **kwargs): |
| 87 |
"""Parse the results of MEMORY STATS""" |
| 88 |
stats = pairs_to_dict(response, decode_keys=True, decode_string_values=True) |
| 89 |
for key, value in stats.items(): |
| 90 |
if key.startswith("db.") and isinstance(value, list): |
| 91 |
stats[key] = pairs_to_dict( |
| 92 |
value, decode_keys=True, decode_string_values=True |
| 93 |
) |
| 94 |
return stats |
| 95 |
|
| 96 |
|
| 97 |
SENTINEL_STATE_TYPES = { |
| 98 |
"can-failover-its-master": int, |
| 99 |
"config-epoch": int, |
| 100 |
"down-after-milliseconds": int, |
| 101 |
"failover-timeout": int, |
| 102 |
"info-refresh": int, |
| 103 |
"last-hello-message": int, |
| 104 |
"last-ok-ping-reply": int, |
| 105 |
"last-ping-reply": int, |
| 106 |
"last-ping-sent": int, |
| 107 |
"master-link-down-time": int, |
| 108 |
"master-port": int, |
| 109 |
"num-other-sentinels": int, |
| 110 |
"num-slaves": int, |
| 111 |
"o-down-time": int, |
| 112 |
"pending-commands": int, |
| 113 |
"parallel-syncs": int, |
| 114 |
"port": int, |
| 115 |
"quorum": int, |
| 116 |
"role-reported-time": int, |
| 117 |
"s-down-time": int, |
| 118 |
"slave-priority": int, |
| 119 |
"slave-repl-offset": int, |
| 120 |
"voted-leader-epoch": int, |
| 121 |
} |
| 122 |
|
| 123 |
|
| 124 |
def parse_sentinel_state(item): |
| 125 |
result = pairs_to_dict_typed(item, SENTINEL_STATE_TYPES) |
| 126 |
flags = set(result["flags"].split(",")) |
| 127 |
for name, flag in ( |
| 128 |
("is_master", "master"), |
| 129 |
("is_slave", "slave"), |
| 130 |
("is_sdown", "s_down"), |
| 131 |
("is_odown", "o_down"), |
| 132 |
("is_sentinel", "sentinel"), |
| 133 |
("is_disconnected", "disconnected"), |
| 134 |
("is_master_down", "master_down"), |
| 135 |
): |
| 136 |
result[name] = flag in flags |
| 137 |
return result |
| 138 |
|
| 139 |
|
| 140 |
def parse_sentinel_master(response): |
| 141 |
return parse_sentinel_state(map(str_if_bytes, response)) |
| 142 |
|
| 143 |
|
| 144 |
def parse_sentinel_state_resp3(response): |
| 145 |
result = {} |
| 146 |
for key in response: |
| 147 |
try: |
| 148 |
value = SENTINEL_STATE_TYPES[key](str_if_bytes(response[key])) |
| 149 |
result[str_if_bytes(key)] = value |
| 150 |
except Exception: |
| 151 |
result[str_if_bytes(key)] = response[str_if_bytes(key)] |
| 152 |
flags = set(result["flags"].split(",")) |
| 153 |
result["flags"] = flags |
| 154 |
return result |
| 155 |
|
| 156 |
|
| 157 |
def parse_sentinel_masters(response): |
| 158 |
result = {} |
| 159 |
for item in response: |
| 160 |
state = parse_sentinel_state(map(str_if_bytes, item)) |
| 161 |
result[state["name"]] = state |
| 162 |
return result |
| 163 |
|
| 164 |
|
| 165 |
def parse_sentinel_masters_resp3(response): |
| 166 |
return [parse_sentinel_state(master) for master in response] |
| 167 |
|
| 168 |
|
| 169 |
def parse_sentinel_slaves_and_sentinels(response): |
| 170 |
return [parse_sentinel_state(map(str_if_bytes, item)) for item in response] |
| 171 |
|
| 172 |
|
| 173 |
def parse_sentinel_slaves_and_sentinels_resp3(response): |
| 174 |
return [parse_sentinel_state_resp3(item) for item in response] |
| 175 |
|
| 176 |
|
| 177 |
def parse_sentinel_get_master(response): |
| 178 |
return response and (response[0], int(response[1])) or None |
| 179 |
|
| 180 |
|
| 181 |
def pairs_to_dict(response, decode_keys=False, decode_string_values=False): |
| 182 |
"""Create a dict given a list of key/value pairs""" |
| 183 |
if response is None: |
| 184 |
return {} |
| 185 |
if decode_keys or decode_string_values: |
| 186 |
# the iter form is faster, but I don't know how to make that work |
| 187 |
# with a str_if_bytes() map |
| 188 |
keys = response[::2] |
| 189 |
if decode_keys: |
| 190 |
keys = map(str_if_bytes, keys) |
| 191 |
values = response[1::2] |
| 192 |
if decode_string_values: |
| 193 |
values = map(str_if_bytes, values) |
| 194 |
return dict(zip(keys, values)) |
| 195 |
else: |
| 196 |
it = iter(response) |
| 197 |
return dict(zip(it, it)) |
| 198 |
|
| 199 |
|
| 200 |
def pairs_to_dict_typed(response, type_info): |
| 201 |
it = iter(response) |
| 202 |
result = {} |
| 203 |
for key, value in zip(it, it): |
| 204 |
if key in type_info: |
| 205 |
try: |
| 206 |
value = type_info[key](value) |
| 207 |
except Exception: |
| 208 |
# if for some reason the value can't be coerced, just use |
| 209 |
# the string value |
| 210 |
pass |
| 211 |
result[key] = value |
| 212 |
return result |
| 213 |
|
| 214 |
|
| 215 |
def zset_score_pairs(response, **options): |
| 216 |
""" |
| 217 |
If ``withscores`` is specified in the options, return the response as |
| 218 |
a list of (value, score) pairs |
| 219 |
""" |
| 220 |
if not response or not options.get("withscores"): |
| 221 |
return response |
| 222 |
score_cast_func = options.get("score_cast_func", float) |
| 223 |
it = iter(response) |
| 224 |
return list(zip(it, map(score_cast_func, it))) |
| 225 |
|
| 226 |
|
| 227 |
def sort_return_tuples(response, **options): |
| 228 |
""" |
| 229 |
If ``groups`` is specified, return the response as a list of |
| 230 |
n-element tuples with n being the value found in options['groups'] |
| 231 |
""" |
| 232 |
if not response or not options.get("groups"): |
| 233 |
return response |
| 234 |
n = options["groups"] |
| 235 |
return list(zip(*[response[i::n] for i in range(n)])) |
| 236 |
|
| 237 |
|
| 238 |
def parse_stream_list(response): |
| 239 |
if response is None: |
| 240 |
return None |
| 241 |
data = [] |
| 242 |
for r in response: |
| 243 |
if r is not None: |
| 244 |
data.append((r[0], pairs_to_dict(r[1]))) |
| 245 |
else: |
| 246 |
data.append((None, None)) |
| 247 |
return data |
| 248 |
|
| 249 |
|
| 250 |
def pairs_to_dict_with_str_keys(response): |
| 251 |
return pairs_to_dict(response, decode_keys=True) |
| 252 |
|
| 253 |
|
| 254 |
def parse_list_of_dicts(response): |
| 255 |
return list(map(pairs_to_dict_with_str_keys, response)) |
| 256 |
|
| 257 |
|
| 258 |
def parse_xclaim(response, **options): |
| 259 |
if options.get("parse_justid", False): |
| 260 |
return response |
| 261 |
return parse_stream_list(response) |
| 262 |
|
| 263 |
|
| 264 |
def parse_xautoclaim(response, **options): |
| 265 |
if options.get("parse_justid", False): |
| 266 |
return response[1] |
| 267 |
response[1] = parse_stream_list(response[1]) |
| 268 |
return response |
| 269 |
|
| 270 |
|
| 271 |
def parse_xinfo_stream(response, **options): |
| 272 |
if isinstance(response, list): |
| 273 |
data = pairs_to_dict(response, decode_keys=True) |
| 274 |
else: |
| 275 |
data = {str_if_bytes(k): v for k, v in response.items()} |
| 276 |
if not options.get("full", False): |
| 277 |
first = data.get("first-entry") |
| 278 |
if first is not None and first[0] is not None: |
| 279 |
data["first-entry"] = (first[0], pairs_to_dict(first[1])) |
| 280 |
last = data["last-entry"] |
| 281 |
if last is not None and last[0] is not None: |
| 282 |
data["last-entry"] = (last[0], pairs_to_dict(last[1])) |
| 283 |
else: |
| 284 |
data["entries"] = {_id: pairs_to_dict(entry) for _id, entry in data["entries"]} |
| 285 |
if len(data["groups"]) > 0 and isinstance(data["groups"][0], list): |
| 286 |
data["groups"] = [ |
| 287 |
pairs_to_dict(group, decode_keys=True) for group in data["groups"] |
| 288 |
] |
| 289 |
for g in data["groups"]: |
| 290 |
if g["consumers"] and g["consumers"][0] is not None: |
| 291 |
g["consumers"] = [ |
| 292 |
pairs_to_dict(c, decode_keys=True) for c in g["consumers"] |
| 293 |
] |
| 294 |
else: |
| 295 |
data["groups"] = [ |
| 296 |
{str_if_bytes(k): v for k, v in group.items()} |
| 297 |
for group in data["groups"] |
| 298 |
] |
| 299 |
return data |
| 300 |
|
| 301 |
|
| 302 |
def parse_xread(response): |
| 303 |
if response is None: |
| 304 |
return [] |
| 305 |
return [[r[0], parse_stream_list(r[1])] for r in response] |
| 306 |
|
| 307 |
|
| 308 |
def parse_xread_resp3(response): |
| 309 |
if response is None: |
| 310 |
return {} |
| 311 |
return {key: [parse_stream_list(value)] for key, value in response.items()} |
| 312 |
|
| 313 |
|
| 314 |
def parse_xpending(response, **options): |
| 315 |
if options.get("parse_detail", False): |
| 316 |
return parse_xpending_range(response) |
| 317 |
consumers = [{"name": n, "pending": int(p)} for n, p in response[3] or []] |
| 318 |
return { |
| 319 |
"pending": response[0], |
| 320 |
"min": response[1], |
| 321 |
"max": response[2], |
| 322 |
"consumers": consumers, |
| 323 |
} |
| 324 |
|
| 325 |
|
| 326 |
def parse_xpending_range(response): |
| 327 |
k = ("message_id", "consumer", "time_since_delivered", "times_delivered") |
| 328 |
return [dict(zip(k, r)) for r in response] |
| 329 |
|
| 330 |
|
| 331 |
def float_or_none(response): |
| 332 |
if response is None: |
| 333 |
return None |
| 334 |
return float(response) |
| 335 |
|
| 336 |
|
| 337 |
def bool_ok(response, **options): |
| 338 |
return str_if_bytes(response) == "OK" |
| 339 |
|
| 340 |
|
| 341 |
def parse_zadd(response, **options): |
| 342 |
if response is None: |
| 343 |
return None |
| 344 |
if options.get("as_score"): |
| 345 |
return float(response) |
| 346 |
return int(response) |
| 347 |
|
| 348 |
|
| 349 |
def parse_client_list(response, **options): |
| 350 |
clients = [] |
| 351 |
for c in str_if_bytes(response).splitlines(): |
| 352 |
# Values might contain '=' |
| 353 |
clients.append(dict(pair.split("=", 1) for pair in c.split(" "))) |
| 354 |
return clients |
| 355 |
|
| 356 |
|
| 357 |
def parse_config_get(response, **options): |
| 358 |
response = [str_if_bytes(i) if i is not None else None for i in response] |
| 359 |
return response and pairs_to_dict(response) or {} |
| 360 |
|
| 361 |
|
| 362 |
def parse_scan(response, **options): |
| 363 |
cursor, r = response |
| 364 |
return int(cursor), r |
| 365 |
|
| 366 |
|
| 367 |
def parse_hscan(response, **options): |
| 368 |
cursor, r = response |
| 369 |
no_values = options.get("no_values", False) |
| 370 |
if no_values: |
| 371 |
payload = r or [] |
| 372 |
else: |
| 373 |
payload = r and pairs_to_dict(r) or {} |
| 374 |
return int(cursor), payload |
| 375 |
|
| 376 |
|
| 377 |
def parse_zscan(response, **options): |
| 378 |
score_cast_func = options.get("score_cast_func", float) |
| 379 |
cursor, r = response |
| 380 |
it = iter(r) |
| 381 |
return int(cursor), list(zip(it, map(score_cast_func, it))) |
| 382 |
|
| 383 |
|
| 384 |
def parse_zmscore(response, **options): |
| 385 |
# zmscore: list of scores (double precision floating point number) or nil |
| 386 |
return [float(score) if score is not None else None for score in response] |
| 387 |
|
| 388 |
|
| 389 |
def parse_slowlog_get(response, **options): |
| 390 |
space = " " if options.get("decode_responses", False) else b" " |
| 391 |
|
| 392 |
def parse_item(item): |
| 393 |
result = {"id": item[0], "start_time": int(item[1]), "duration": int(item[2])} |
| 394 |
# Valkey Enterprise injects another entry at index [3], which has |
| 395 |
# the complexity info (i.e. the value N in case the command has |
| 396 |
# an O(N) complexity) instead of the command. |
| 397 |
if isinstance(item[3], list): |
| 398 |
result["command"] = space.join(item[3]) |
| 399 |
result["client_address"] = item[4] |
| 400 |
result["client_name"] = item[5] |
| 401 |
else: |
| 402 |
result["complexity"] = item[3] |
| 403 |
result["command"] = space.join(item[4]) |
| 404 |
result["client_address"] = item[5] |
| 405 |
result["client_name"] = item[6] |
| 406 |
return result |
| 407 |
|
| 408 |
return [parse_item(item) for item in response] |
| 409 |
|
| 410 |
|
| 411 |
def parse_stralgo(response, **options): |
| 412 |
""" |
| 413 |
Parse the response from `STRALGO` command. |
| 414 |
Without modifiers the returned value is string. |
| 415 |
When LEN is given the command returns the length of the result |
| 416 |
(i.e integer). |
| 417 |
When IDX is given the command returns a dictionary with the LCS |
| 418 |
length and all the ranges in both the strings, start and end |
| 419 |
offset for each string, where there are matches. |
| 420 |
When WITHMATCHLEN is given, each array representing a match will |
| 421 |
also have the length of the match at the beginning of the array. |
| 422 |
""" |
| 423 |
if options.get("len", False): |
| 424 |
return int(response) |
| 425 |
if options.get("idx", False): |
| 426 |
if options.get("withmatchlen", False): |
| 427 |
matches = [ |
| 428 |
[int(match[-1])] + list(map(tuple, match[:-1])) for match in response[1] |
| 429 |
] |
| 430 |
else: |
| 431 |
matches = [list(map(tuple, match)) for match in response[1]] |
| 432 |
return { |
| 433 |
str_if_bytes(response[0]): matches, |
| 434 |
str_if_bytes(response[2]): int(response[3]), |
| 435 |
} |
| 436 |
return str_if_bytes(response) |
| 437 |
|
| 438 |
|
| 439 |
def parse_cluster_info(response, **options): |
| 440 |
response = str_if_bytes(response) |
| 441 |
return dict(line.split(":") for line in response.splitlines() if line) |
| 442 |
|
| 443 |
|
| 444 |
def _parse_node_line(line): |
| 445 |
line_items = line.split(" ") |
| 446 |
node_id, addr, flags, master_id, ping, pong, epoch, connected = line.split(" ")[:8] |
| 447 |
addr = addr.split("@")[0] |
| 448 |
node_dict = { |
| 449 |
"node_id": node_id, |
| 450 |
"flags": flags, |
| 451 |
"master_id": master_id, |
| 452 |
"last_ping_sent": ping, |
| 453 |
"last_pong_rcvd": pong, |
| 454 |
"epoch": epoch, |
| 455 |
"slots": [], |
| 456 |
"migrations": [], |
| 457 |
"connected": True if connected == "connected" else False, |
| 458 |
} |
| 459 |
if len(line_items) >= 9: |
| 460 |
slots, migrations = _parse_slots(line_items[8:]) |
| 461 |
node_dict["slots"], node_dict["migrations"] = slots, migrations |
| 462 |
return addr, node_dict |
| 463 |
|
| 464 |
|
| 465 |
def _parse_slots(slot_ranges): |
| 466 |
slots, migrations = [], [] |
| 467 |
for s_range in slot_ranges: |
| 468 |
if "->-" in s_range: |
| 469 |
slot_id, dst_node_id = s_range[1:-1].split("->-", 1) |
| 470 |
migrations.append( |
| 471 |
{"slot": slot_id, "node_id": dst_node_id, "state": "migrating"} |
| 472 |
) |
| 473 |
elif "-<-" in s_range: |
| 474 |
slot_id, src_node_id = s_range[1:-1].split("-<-", 1) |
| 475 |
migrations.append( |
| 476 |
{"slot": slot_id, "node_id": src_node_id, "state": "importing"} |
| 477 |
) |
| 478 |
else: |
| 479 |
s_range = [sl for sl in s_range.split("-")] |
| 480 |
slots.append(s_range) |
| 481 |
|
| 482 |
return slots, migrations |
| 483 |
|
| 484 |
|
| 485 |
def parse_cluster_nodes(response, **options): |
| 486 |
""" |
| 487 |
@see: https://valkey.io/commands/cluster-nodes # string / bytes |
| 488 |
@see: https://valkey.io/commands/cluster-replicas # list of string / bytes |
| 489 |
""" |
| 490 |
if isinstance(response, (str, bytes)): |
| 491 |
response = response.splitlines() |
| 492 |
return dict(_parse_node_line(str_if_bytes(node)) for node in response) |
| 493 |
|
| 494 |
|
| 495 |
def parse_geosearch_generic(response, **options): |
| 496 |
""" |
| 497 |
Parse the response of 'GEOSEARCH', GEORADIUS' and 'GEORADIUSBYMEMBER' |
| 498 |
commands according to 'withdist', 'withhash' and 'withcoord' labels. |
| 499 |
""" |
| 500 |
try: |
| 501 |
if options["store"] or options["store_dist"]: |
| 502 |
# `store` and `store_dist` cant be combined |
| 503 |
# with other command arguments. |
| 504 |
# relevant to 'GEORADIUS' and 'GEORADIUSBYMEMBER' |
| 505 |
return response |
| 506 |
except KeyError: # it means the command was sent via execute_command |
| 507 |
return response |
| 508 |
|
| 509 |
if not isinstance(response, list): |
| 510 |
response_list = [response] |
| 511 |
else: |
| 512 |
response_list = response |
| 513 |
|
| 514 |
if not options["withdist"] and not options["withcoord"] and not options["withhash"]: |
| 515 |
# just a bunch of places |
| 516 |
return response_list |
| 517 |
|
| 518 |
cast = { |
| 519 |
"withdist": float, |
| 520 |
"withcoord": lambda ll: (float(ll[0]), float(ll[1])), |
| 521 |
"withhash": int, |
| 522 |
} |
| 523 |
|
| 524 |
# zip all output results with each casting function to get |
| 525 |
# the properly native Python value. |
| 526 |
f = [lambda x: x] |
| 527 |
f += [cast[o] for o in ["withdist", "withhash", "withcoord"] if options[o]] |
| 528 |
return [list(map(lambda fv: fv[0](fv[1]), zip(f, r))) for r in response_list] |
| 529 |
|
| 530 |
|
| 531 |
def parse_command(response, **options): |
| 532 |
commands = {} |
| 533 |
for command in response: |
| 534 |
cmd_dict = {} |
| 535 |
cmd_name = str_if_bytes(command[0]) |
| 536 |
cmd_dict["name"] = cmd_name |
| 537 |
cmd_dict["arity"] = int(command[1]) |
| 538 |
cmd_dict["flags"] = [str_if_bytes(flag) for flag in command[2]] |
| 539 |
cmd_dict["first_key_pos"] = command[3] |
| 540 |
cmd_dict["last_key_pos"] = command[4] |
| 541 |
cmd_dict["step_count"] = command[5] |
| 542 |
if len(command) > 7: |
| 543 |
cmd_dict["tips"] = command[7] |
| 544 |
cmd_dict["key_specifications"] = command[8] |
| 545 |
cmd_dict["subcommands"] = command[9] |
| 546 |
commands[cmd_name] = cmd_dict |
| 547 |
return commands |
| 548 |
|
| 549 |
|
| 550 |
def parse_command_resp3(response, **options): |
| 551 |
commands = {} |
| 552 |
for command in response: |
| 553 |
cmd_dict = {} |
| 554 |
cmd_name = str_if_bytes(command[0]) |
| 555 |
cmd_dict["name"] = cmd_name |
| 556 |
cmd_dict["arity"] = command[1] |
| 557 |
cmd_dict["flags"] = {str_if_bytes(flag) for flag in command[2]} |
| 558 |
cmd_dict["first_key_pos"] = command[3] |
| 559 |
cmd_dict["last_key_pos"] = command[4] |
| 560 |
cmd_dict["step_count"] = command[5] |
| 561 |
cmd_dict["acl_categories"] = command[6] |
| 562 |
if len(command) > 7: |
| 563 |
cmd_dict["tips"] = command[7] |
| 564 |
cmd_dict["key_specifications"] = command[8] |
| 565 |
cmd_dict["subcommands"] = command[9] |
| 566 |
|
| 567 |
commands[cmd_name] = cmd_dict |
| 568 |
return commands |
| 569 |
|
| 570 |
|
| 571 |
def parse_pubsub_numsub(response, **options): |
| 572 |
return list(zip(response[0::2], response[1::2])) |
| 573 |
|
| 574 |
|
| 575 |
def parse_client_kill(response, **options): |
| 576 |
if isinstance(response, int): |
| 577 |
return response |
| 578 |
return str_if_bytes(response) == "OK" |
| 579 |
|
| 580 |
|
| 581 |
def parse_acl_getuser(response, **options): |
| 582 |
if response is None: |
| 583 |
return None |
| 584 |
if isinstance(response, list): |
| 585 |
data = pairs_to_dict(response, decode_keys=True) |
| 586 |
else: |
| 587 |
data = {str_if_bytes(key): value for key, value in response.items()} |
| 588 |
|
| 589 |
# convert everything but user-defined data in 'keys' to native strings |
| 590 |
data["flags"] = list(map(str_if_bytes, data["flags"])) |
| 591 |
data["passwords"] = list(map(str_if_bytes, data["passwords"])) |
| 592 |
data["commands"] = str_if_bytes(data["commands"]) |
| 593 |
if isinstance(data["keys"], str) or isinstance(data["keys"], bytes): |
| 594 |
data["keys"] = list(str_if_bytes(data["keys"]).split(" ")) |
| 595 |
if data["keys"] == [""]: |
| 596 |
data["keys"] = [] |
| 597 |
if "channels" in data: |
| 598 |
if isinstance(data["channels"], str) or isinstance(data["channels"], bytes): |
| 599 |
data["channels"] = list(str_if_bytes(data["channels"]).split(" ")) |
| 600 |
if data["channels"] == [""]: |
| 601 |
data["channels"] = [] |
| 602 |
if "selectors" in data: |
| 603 |
if data["selectors"] != [] and isinstance(data["selectors"][0], list): |
| 604 |
data["selectors"] = [ |
| 605 |
list(map(str_if_bytes, selector)) for selector in data["selectors"] |
| 606 |
] |
| 607 |
elif data["selectors"] != []: |
| 608 |
data["selectors"] = [ |
| 609 |
{str_if_bytes(k): str_if_bytes(v) for k, v in selector.items()} |
| 610 |
for selector in data["selectors"] |
| 611 |
] |
| 612 |
|
| 613 |
# split 'commands' into separate 'categories' and 'commands' lists |
| 614 |
commands, categories = [], [] |
| 615 |
for command in data["commands"].split(" "): |
| 616 |
categories.append(command) if "@" in command else commands.append(command) |
| 617 |
|
| 618 |
data["commands"] = commands |
| 619 |
data["categories"] = categories |
| 620 |
data["enabled"] = "on" in data["flags"] |
| 621 |
return data |
| 622 |
|
| 623 |
|
| 624 |
def parse_acl_log(response, **options): |
| 625 |
if response is None: |
| 626 |
return None |
| 627 |
if isinstance(response, list): |
| 628 |
data = [] |
| 629 |
for log in response: |
| 630 |
log_data = pairs_to_dict(log, True, True) |
| 631 |
client_info = log_data.get("client-info", "") |
| 632 |
log_data["client-info"] = parse_client_info(client_info) |
| 633 |
|
| 634 |
# float() is lossy comparing to the "double" in C |
| 635 |
log_data["age-seconds"] = float(log_data["age-seconds"]) |
| 636 |
data.append(log_data) |
| 637 |
else: |
| 638 |
data = bool_ok(response) |
| 639 |
return data |
| 640 |
|
| 641 |
|
| 642 |
def parse_client_info(value): |
| 643 |
""" |
| 644 |
Parsing client-info in ACL Log in following format. |
| 645 |
"key1=value1 key2=value2 key3=value3" |
| 646 |
""" |
| 647 |
client_info = {} |
| 648 |
for info in str_if_bytes(value).strip().split(): |
| 649 |
key, value = info.split("=") |
| 650 |
client_info[key] = value |
| 651 |
|
| 652 |
# Those fields are defined as int in networking.c |
| 653 |
for int_key in { |
| 654 |
"id", |
| 655 |
"age", |
| 656 |
"idle", |
| 657 |
"db", |
| 658 |
"sub", |
| 659 |
"psub", |
| 660 |
"multi", |
| 661 |
"qbuf", |
| 662 |
"qbuf-free", |
| 663 |
"obl", |
| 664 |
"argv-mem", |
| 665 |
"oll", |
| 666 |
"omem", |
| 667 |
"tot-mem", |
| 668 |
}: |
| 669 |
client_info[int_key] = int(client_info[int_key]) |
| 670 |
return client_info |
| 671 |
|
| 672 |
|
| 673 |
def parse_set_result(response, **options): |
| 674 |
""" |
| 675 |
Parsing SET result into: |
| 676 |
- BOOL |
| 677 |
- String when GET argument is used |
| 678 |
""" |
| 679 |
if options.get("get"): |
| 680 |
# Valkey will return a getCommand result. |
| 681 |
# See `setGenericCommand` in t_string.c |
| 682 |
return response |
| 683 |
return response and str_if_bytes(response) == "OK" |
| 684 |
|
| 685 |
|
| 686 |
def string_keys_to_dict(key_string, callback): |
| 687 |
return dict.fromkeys(key_string.split(), callback) |
| 688 |
|
| 689 |
|
| 690 |
_ValkeyCallbacks = { |
| 691 |
**string_keys_to_dict( |
| 692 |
"AUTH COPY EXPIRE EXPIREAT HEXISTS HMSET MOVE MSETNX PERSIST PSETEX " |
| 693 |
"PEXPIRE PEXPIREAT RENAMENX SETEX SETNX SMOVE", |
| 694 |
bool, |
| 695 |
), |
| 696 |
**string_keys_to_dict("HINCRBYFLOAT INCRBYFLOAT", float), |
| 697 |
**string_keys_to_dict( |
| 698 |
"ASKING FLUSHALL FLUSHDB LSET LTRIM MSET PFMERGE READONLY READWRITE " |
| 699 |
"RENAME SAVE SELECT SHUTDOWN SLAVEOF SWAPDB WATCH UNWATCH", |
| 700 |
bool_ok, |
| 701 |
), |
| 702 |
**string_keys_to_dict("XREAD XREADGROUP", parse_xread), |
| 703 |
**string_keys_to_dict( |
| 704 |
"GEORADIUS GEORADIUSBYMEMBER GEOSEARCH", |
| 705 |
parse_geosearch_generic, |
| 706 |
), |
| 707 |
**string_keys_to_dict("XRANGE XREVRANGE", parse_stream_list), |
| 708 |
"ACL GETUSER": parse_acl_getuser, |
| 709 |
"ACL LOAD": bool_ok, |
| 710 |
"ACL LOG": parse_acl_log, |
| 711 |
"ACL SETUSER": bool_ok, |
| 712 |
"ACL SAVE": bool_ok, |
| 713 |
"CLIENT INFO": parse_client_info, |
| 714 |
"CLIENT KILL": parse_client_kill, |
| 715 |
"CLIENT LIST": parse_client_list, |
| 716 |
"CLIENT PAUSE": bool_ok, |
| 717 |
"CLIENT SETINFO": bool_ok, |
| 718 |
"CLIENT SETNAME": bool_ok, |
| 719 |
"CLIENT UNBLOCK": bool, |
| 720 |
"CLUSTER ADDSLOTS": bool_ok, |
| 721 |
"CLUSTER ADDSLOTSRANGE": bool_ok, |
| 722 |
"CLUSTER DELSLOTS": bool_ok, |
| 723 |
"CLUSTER DELSLOTSRANGE": bool_ok, |
| 724 |
"CLUSTER FAILOVER": bool_ok, |
| 725 |
"CLUSTER FORGET": bool_ok, |
| 726 |
"CLUSTER INFO": parse_cluster_info, |
| 727 |
"CLUSTER MEET": bool_ok, |
| 728 |
"CLUSTER NODES": parse_cluster_nodes, |
| 729 |
"CLUSTER REPLICAS": parse_cluster_nodes, |
| 730 |
"CLUSTER REPLICATE": bool_ok, |
| 731 |
"CLUSTER RESET": bool_ok, |
| 732 |
"CLUSTER SAVECONFIG": bool_ok, |
| 733 |
"CLUSTER SET-CONFIG-EPOCH": bool_ok, |
| 734 |
"CLUSTER SETSLOT": bool_ok, |
| 735 |
"CLUSTER SLAVES": parse_cluster_nodes, |
| 736 |
"COMMAND": parse_command, |
| 737 |
"CONFIG RESETSTAT": bool_ok, |
| 738 |
"CONFIG SET": bool_ok, |
| 739 |
"FUNCTION DELETE": bool_ok, |
| 740 |
"FUNCTION FLUSH": bool_ok, |
| 741 |
"FUNCTION RESTORE": bool_ok, |
| 742 |
"GEODIST": float_or_none, |
| 743 |
"HSCAN": parse_hscan, |
| 744 |
"INFO": parse_info, |
| 745 |
"LASTSAVE": timestamp_to_datetime, |
| 746 |
"MEMORY PURGE": bool_ok, |
| 747 |
"MODULE LOAD": bool, |
| 748 |
"MODULE UNLOAD": bool, |
| 749 |
"PING": lambda r: str_if_bytes(r) == "PONG", |
| 750 |
"PUBSUB NUMSUB": parse_pubsub_numsub, |
| 751 |
"PUBSUB SHARDNUMSUB": parse_pubsub_numsub, |
| 752 |
"QUIT": bool_ok, |
| 753 |
"SET": parse_set_result, |
| 754 |
"SCAN": parse_scan, |
| 755 |
"SCRIPT EXISTS": lambda r: list(map(bool, r)), |
| 756 |
"SCRIPT FLUSH": bool_ok, |
| 757 |
"SCRIPT KILL": bool_ok, |
| 758 |
"SCRIPT LOAD": str_if_bytes, |
| 759 |
"SENTINEL CKQUORUM": bool_ok, |
| 760 |
"SENTINEL FAILOVER": bool_ok, |
| 761 |
"SENTINEL FLUSHCONFIG": bool_ok, |
| 762 |
"SENTINEL GET-MASTER-ADDR-BY-NAME": parse_sentinel_get_master, |
| 763 |
"SENTINEL MONITOR": bool_ok, |
| 764 |
"SENTINEL RESET": bool_ok, |
| 765 |
"SENTINEL REMOVE": bool_ok, |
| 766 |
"SENTINEL SET": bool_ok, |
| 767 |
"SLOWLOG GET": parse_slowlog_get, |
| 768 |
"SLOWLOG RESET": bool_ok, |
| 769 |
"SORT": sort_return_tuples, |
| 770 |
"SSCAN": parse_scan, |
| 771 |
"TIME": lambda x: (int(x[0]), int(x[1])), |
| 772 |
"XAUTOCLAIM": parse_xautoclaim, |
| 773 |
"XCLAIM": parse_xclaim, |
| 774 |
"XGROUP CREATE": bool_ok, |
| 775 |
"XGROUP DESTROY": bool, |
| 776 |
"XGROUP SETID": bool_ok, |
| 777 |
"XINFO STREAM": parse_xinfo_stream, |
| 778 |
"XPENDING": parse_xpending, |
| 779 |
"ZSCAN": parse_zscan, |
| 780 |
} |
| 781 |
|
| 782 |
|
| 783 |
_ValkeyCallbacksRESP2 = { |
| 784 |
**string_keys_to_dict( |
| 785 |
"ZDIFF ZINTER ZPOPMAX ZPOPMIN ZRANGE ZRANGEBYSCORE ZRANK ZREVRANGE " |
| 786 |
"ZREVRANGEBYSCORE ZREVRANK ZUNION", |
| 787 |
zset_score_pairs, |
| 788 |
), |
| 789 |
**string_keys_to_dict("ZINCRBY ZSCORE", float_or_none), |
| 790 |
**string_keys_to_dict("BGREWRITEAOF BGSAVE", lambda r: True), |
| 791 |
**string_keys_to_dict("BLPOP BRPOP", lambda r: r and tuple(r) or None), |
| 792 |
**string_keys_to_dict( |
| 793 |
"BZPOPMAX BZPOPMIN", lambda r: r and (r[0], r[1], float(r[2])) or None |
| 794 |
), |
| 795 |
"ACL CAT": lambda r: list(map(str_if_bytes, r)), |
| 796 |
"ACL GENPASS": str_if_bytes, |
| 797 |
"ACL HELP": lambda r: list(map(str_if_bytes, r)), |
| 798 |
"ACL LIST": lambda r: list(map(str_if_bytes, r)), |
| 799 |
"ACL USERS": lambda r: list(map(str_if_bytes, r)), |
| 800 |
"ACL WHOAMI": str_if_bytes, |
| 801 |
"CLIENT GETNAME": str_if_bytes, |
| 802 |
"CLIENT TRACKINGINFO": lambda r: list(map(str_if_bytes, r)), |
| 803 |
"CLUSTER GETKEYSINSLOT": lambda r: list(map(str_if_bytes, r)), |
| 804 |
"COMMAND GETKEYS": lambda r: list(map(str_if_bytes, r)), |
| 805 |
"CONFIG GET": parse_config_get, |
| 806 |
"DEBUG OBJECT": parse_debug_object, |
| 807 |
"GEOHASH": lambda r: list(map(str_if_bytes, r)), |
| 808 |
"GEOPOS": lambda r: list( |
| 809 |
map(lambda ll: (float(ll[0]), float(ll[1])) if ll is not None else None, r) |
| 810 |
), |
| 811 |
"HGETALL": lambda r: r and pairs_to_dict(r) or {}, |
| 812 |
"MEMORY STATS": parse_memory_stats, |
| 813 |
"MODULE LIST": lambda r: [pairs_to_dict(m) for m in r], |
| 814 |
"RESET": str_if_bytes, |
| 815 |
"SENTINEL MASTER": parse_sentinel_master, |
| 816 |
"SENTINEL MASTERS": parse_sentinel_masters, |
| 817 |
"SENTINEL SENTINELS": parse_sentinel_slaves_and_sentinels, |
| 818 |
"SENTINEL SLAVES": parse_sentinel_slaves_and_sentinels, |
| 819 |
"STRALGO": parse_stralgo, |
| 820 |
"XINFO CONSUMERS": parse_list_of_dicts, |
| 821 |
"XINFO GROUPS": parse_list_of_dicts, |
| 822 |
"ZADD": parse_zadd, |
| 823 |
"ZMSCORE": parse_zmscore, |
| 824 |
} |
| 825 |
|
| 826 |
|
| 827 |
_ValkeyCallbacksRESP3 = { |
| 828 |
**string_keys_to_dict( |
| 829 |
"ZRANGE ZINTER ZPOPMAX ZPOPMIN ZRANGEBYSCORE ZREVRANGE ZREVRANGEBYSCORE " |
| 830 |
"ZUNION HGETALL XREADGROUP", |
| 831 |
lambda r, **kwargs: r, |
| 832 |
), |
| 833 |
**string_keys_to_dict("XREAD XREADGROUP", parse_xread_resp3), |
| 834 |
"ACL LOG": lambda r: ( |
| 835 |
[ |
| 836 |
{str_if_bytes(key): str_if_bytes(value) for key, value in x.items()} |
| 837 |
for x in r |
| 838 |
] |
| 839 |
if isinstance(r, list) |
| 840 |
else bool_ok(r) |
| 841 |
), |
| 842 |
"COMMAND": parse_command_resp3, |
| 843 |
"CONFIG GET": lambda r: { |
| 844 |
str_if_bytes(key) if key is not None else None: ( |
| 845 |
str_if_bytes(value) if value is not None else None |
| 846 |
) |
| 847 |
for key, value in r.items() |
| 848 |
}, |
| 849 |
"MEMORY STATS": lambda r: {str_if_bytes(key): value for key, value in r.items()}, |
| 850 |
"SENTINEL MASTER": parse_sentinel_state_resp3, |
| 851 |
"SENTINEL MASTERS": parse_sentinel_masters_resp3, |
| 852 |
"SENTINEL SENTINELS": parse_sentinel_slaves_and_sentinels_resp3, |
| 853 |
"SENTINEL SLAVES": parse_sentinel_slaves_and_sentinels_resp3, |
| 854 |
"STRALGO": lambda r, **options: ( |
| 855 |
{str_if_bytes(key): str_if_bytes(value) for key, value in r.items()} |
| 856 |
if isinstance(r, dict) |
| 857 |
else str_if_bytes(r) |
| 858 |
), |
| 859 |
"XINFO CONSUMERS": lambda r: [ |
| 860 |
{str_if_bytes(key): value for key, value in x.items()} for x in r |
| 861 |
], |
| 862 |
"XINFO GROUPS": lambda r: [ |
| 863 |
{str_if_bytes(key): value for key, value in d.items()} for d in r |
| 864 |
], |
| 865 |
} |