Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def test_render_members(completer):
completer.completers["members"].words = []
config.raw = False
config.withscores = True
rendered = renders.render_members([b"duck", b"667", b"camel", b"708"], completer)
assert rendered == FormattedText(
[
("", "1)"),
("", " "),
("class:integer", "667 "),
("class:member", '"duck"'),
("", "\n"),
("", "2)"),
("", " "),
("class:integer", "708 "),
("class:member", '"camel"'),
]
)
assert completer.completers["member"].words == ["camel", "duck"]
redis_resp = self.execute_command_and_read_response(
completer, command_name, *args
)
self.after_hook(raw_command, command_name, args, completer)
if command_name.upper() == "MONITOR":
logger.info("monitor")
print_formatted_text(redis_resp, style=STYLE)
try:
self.monitor()
except KeyboardInterrupt:
return
except Exception as e:
logger.exception(e)
return render_error(str(e))
finally:
config.withscores = False
return redis_resp
def render_members(items, completer):
if config.withscores:
if config.raw:
return _update_completer_then_render(
items, completer, "member", "class:member", completer_iter_step=2
)
return _update_completer_then_render_withscores(items, completer)
return _update_completer_then_render(
items, completer, "member", "class:member", completer_iter_step=1
)
def pre_hook(self, command, command_name, args, completer):
"""
Before execute command, patch completers first.
Eg: When user run `GET foo`, key completer need to
touch foo.
Only works when compile-grammar thread is done.
"""
# TRANSATION state chage
if command_name.upper() in ["EXEC", "DISCARD"]:
logger.debug(f"[After hook] Command is {command_name}, unset transaction.")
config.transaction = False
# score display for sorted set
if command_name.upper() in ["ZSCAN", "ZPOPMAX", "ZPOPMIN"]:
config.withscores = True
# patch completers
if not completer:
logger.warning("[Pre patch completer] Complter not ready, not patched...")
return
redis_grammar = completer.compiled_grammar
m = redis_grammar.match(command)
if not m:
# invalide command!
return
variables = m.variables()
# zset withscores
withscores = variables.get("withscores")
logger.debug(f"[PRE HOOK] withscores: {withscores}")
if withscores:
config.withscores = True