From 06cba6ccd165ca8b224797e37fccb9e63f026d77 Mon Sep 17 00:00:00 2001 From: Daniel Baumann Date: Sat, 21 Mar 2020 11:28:17 +0100 Subject: Adding upstream version 1.9.1. Signed-off-by: Daniel Baumann --- iredis/processors.py | 79 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 79 insertions(+) create mode 100644 iredis/processors.py (limited to 'iredis/processors.py') diff --git a/iredis/processors.py b/iredis/processors.py new file mode 100644 index 0000000..029f80d --- /dev/null +++ b/iredis/processors.py @@ -0,0 +1,79 @@ +import logging + +from prompt_toolkit.layout.processors import ( + Processor, + Transformation, + TransformationInput, +) + +from .exceptions import InvalidArguments, AmbiguousCommand +from .commands import split_command_args + +logger = logging.getLogger(__name__) + + +class UserInputCommand: + """ + User inputted command in real time. + + ``UpdateBottomProcessor`` update it, and ``BottomToolbar`` read it + """ + + def __init__(self): + # command will always be upper case + self.command = None + + +class UpdateBottomProcessor(Processor): + """ + Update Footer display text while user input. + """ + + def __init__(self, command_holder, session): + # processor will call for internal_refresh, when input_text didn't + # change, don't run + self.session = session + self.command_holder = command_holder + + def apply_transformation( + self, transformation_input: TransformationInput + ) -> Transformation: + input_text = transformation_input.document.text + try: + command, _ = split_command_args(input_text) + except (InvalidArguments, AmbiguousCommand): + self.command_holder.command = None + else: + self.command_holder.command = command.upper() + + return Transformation(transformation_input.fragments) + + +class PasswordProcessor(Processor): + """ + Processor that turns masks the input. (For passwords.) + + :param char: (string) Character to be used. "*" by default. + """ + + def __init__(self, char: str = "*") -> None: + self.char = char + + def apply_transformation(self, ti: TransformationInput) -> Transformation: + input_text = ti.document.text + default_transformation = Transformation(ti.fragments) + try: + command, _ = split_command_args(input_text) + except (InvalidArguments, AmbiguousCommand): + return default_transformation + + if command.upper() != "AUTH": + return default_transformation + + fragments = [] + for style, text, *handler in ti.fragments: + if style == "class:password": + fragments.append((style, self.char * len(text), *handler)) + else: + fragments.append((style, text, *handler)) + return Transformation(fragments) -- cgit v1.2.3