def test_completer(self): class completer1(Completer): def get_completions(self, document, complete_event): yield Completion('before-%s-after' % document.text, -len(document.text)) yield Completion('before-%s-after-B' % document.text, -len(document.text)) class completer2(Completer): def get_completions(self, document, complete_event): yield Completion('before2-%s-after2' % document.text, -len(document.text)) yield Completion('before2-%s-after2-B' % document.text, -len(document.text)) # Create grammar. "var1" + "whitespace" + "var2" g = compile(r'(?P<var1>[a-z]*) \s+ (?P<var2>[a-z]*)') # Test 'get_completions()' completer = GrammarCompleter(g, { 'var1': completer1(), 'var2': completer2() }) completions = list( completer.get_completions(Document('abc def', len('abc def')), CompleteEvent())) self.assertEqual(len(completions), 2) self.assertEqual(completions[0].text, 'before2-def-after2') self.assertEqual(completions[0].start_position, -3) self.assertEqual(completions[1].text, 'before2-def-after2-B') self.assertEqual(completions[1].start_position, -3)
def test_newbie_mode_complete_without_meta_dict(): fake_document = MagicMock() fake_document.text_before_cursor = "GEOR" completer = GrammarCompleter(command_grammar, get_completer_mapping()) completions = list(completer.get_completions(fake_document, None)) assert [word.text for word in completions] == ["GEORADIUS", "GEORADIUSBYMEMBER"]
def test_completer(self): class completer1(Completer): def get_completions(self, document, complete_event): yield Completion('before-%s-after' % document.text, -len(document.text)) yield Completion('before-%s-after-B' % document.text, -len(document.text)) class completer2(Completer): def get_completions(self, document, complete_event): yield Completion('before2-%s-after2' % document.text, -len(document.text)) yield Completion('before2-%s-after2-B' % document.text, -len(document.text)) # Create grammar. "var1" + "whitespace" + "var2" g = compile(r'(?P<var1>[a-z]*) \s+ (?P<var2>[a-z]*)') # Test 'get_completions()' completer = GrammarCompleter(g, {'var1': completer1(), 'var2': completer2()}) completions = list(completer.get_completions( Document('abc def', len('abc def')), CompleteEvent())) self.assertEqual(len(completions), 2) self.assertEqual(completions[0].text, 'before2-def-after2') self.assertEqual(completions[0].start_position, -3) self.assertEqual(completions[1].text, 'before2-def-after2-B') self.assertEqual(completions[1].start_position, -3)
def test_completer(): class completer1(Completer): def get_completions(self, document, complete_event): yield Completion("before-%s-after" % document.text, -len(document.text)) yield Completion("before-%s-after-B" % document.text, -len(document.text)) class completer2(Completer): def get_completions(self, document, complete_event): yield Completion("before2-%s-after2" % document.text, -len(document.text)) yield Completion("before2-%s-after2-B" % document.text, -len(document.text)) # Create grammar. "var1" + "whitespace" + "var2" g = compile(r"(?P<var1>[a-z]*) \s+ (?P<var2>[a-z]*)") # Test 'get_completions()' completer = GrammarCompleter(g, {"var1": completer1(), "var2": completer2()}) completions = list( completer.get_completions(Document("abc def", len("abc def")), CompleteEvent()) ) assert len(completions) == 2 assert completions[0].text == "before2-def-after2" assert completions[0].start_position == -3 assert completions[1].text == "before2-def-after2-B" assert completions[1].start_position == -3
def test_patch_completer(): client = Client("127.0.0.1", "6379", None) grammar = get_command_grammar("MGET") completer = GrammarCompleter(grammar, completer_mapping) client.pre_hook( "MGET foo bar hello world", "MGET", "foo bar hello world", completer ) assert completer.completers["key"].words == ["world", "hello", "bar", "foo"] assert completer.completers["keys"].words == ["world", "hello", "bar", "foo"] grammar = get_command_grammar("GET") completer = GrammarCompleter(grammar, completer_mapping) client.pre_hook("GET bar", "GET", "bar", completer) assert completer.completers["keys"].words == ["bar", "world", "hello", "foo"]
def create_prompt(): lexer = { name: token for token, level in ((Token.Command, COMMANDS), (Token.Operator, SUBCOMMANDS), (Token.Other, ARGUMENTS)) for names in level.values() for name in names } # print('LEXER:', lexer) lexer = GrammarLexer(GRAMMAR, lexer) completer = { k: WordCompleter(v) for item in LEVELS.values() for k, v in item.items() } # print('COMPLETER:', completer) completer = GrammarCompleter(GRAMMAR, completer) return functools.partial( get_input, message=DEFAULT_PROMPT, # text at the beginning lexer=lexer, completer=completer, # cf above style=ExampleStyle, # pygmentation patch_stdout=True, # printings occurs above the prompt line )
def create_completer(get_globals, get_locals, magics_manager, alias_manager): g = create_ipython_grammar() return GrammarCompleter( g, { 'python': PythonCompleter(get_globals, get_locals), 'magic': MagicsCompleter(magics_manager), 'alias_name': AliasCompleter(alias_manager), 'pdb_arg': WordCompleter(['on', 'off'], ignore_case=True), 'autocall_arg': WordCompleter(['0', '1', '2'], ignore_case=True), 'py_filename': PathCompleter(only_directories=False, file_filter=lambda name: name.endswith('.py')), 'filename': PathCompleter(only_directories=False), 'directory': PathCompleter(only_directories=True), 'system': SystemCompleter(), })
def test_newbie_mode_complete_with_meta_dict(): fake_document = MagicMock() fake_document.text_before_cursor = "GEOR" completer = GrammarCompleter(command_grammar, get_completer_mapping()) completions = list(completer.get_completions(fake_document, None)) assert sorted([completion.display_meta for completion in completions]) == [ FormattedText([( "", "Query a sorted set representing a geospatial index to fetch members matching a given maximum distance from a member", # noqa )]), FormattedText([( "", "Query a sorted set representing a geospatial index to fetch members matching a given maximum distance from a point", # noqa )]), ]
def create_completer( get_globals, get_locals, magics_manager, alias_manager, get_enable_dictionary_completion, ): g = create_ipython_grammar() return GrammarCompleter( g, { "python": PythonCompleter(get_globals, get_locals, get_enable_dictionary_completion), "magic": MagicsCompleter(magics_manager), "alias_name": AliasCompleter(alias_manager), "pdb_arg": WordCompleter(["on", "off"], ignore_case=True), "autocall_arg": WordCompleter(["0", "1", "2"], ignore_case=True), "py_filename": PathCompleter(only_directories=False, file_filter=lambda name: name.endswith(".py")), "filename": PathCompleter(only_directories=False), "directory": PathCompleter(only_directories=True), "system": SystemCompleter(), }, )
def _path_completer(self): if self._path_completer_cache is None: self._path_completer_cache = GrammarCompleter( self._path_completer_grammar, {'var1': PathCompleter(expanduser=True), 'var2': PathCompleter(expanduser=True),}) return self._path_completer_cache
def __init__(self, config, invoker): super().__init__(invoker) self.config = config self.grammar = commands_grammar(config) completer = GrammarCompleter( self.grammar, {'subcmd': WordCompleter(tuple(config.all_fields))}) self._get_input = partial(prompt, PROMPT_WELCOME, completer=completer)
def _get_input(self): """ Read PDB input. Return input text. """ # Reset multiline/paste mode every time. self.python_input.paste_mode = False self.python_input.currently_multiline = False # Set source code document. self._show_source_code(self.curframe.f_code.co_filename) self.cli.buffers[DEFAULT_BUFFER].document = Document('') # Select the current frame of the stack. for i, (frame, lineno) in enumerate(self.stack): if frame is self.curframe: self.callstack_selected_frame = i break # Set up a new completer and validator for the new grammar. g = self._create_grammar() self.completer = GrammarCompleter( g, completers={ 'enabled_breakpoint': BreakPointListCompleter(only_enabled=True), 'disabled_breakpoint': BreakPointListCompleter(only_disabled=True), 'alias_name': AliasCompleter(self), 'python_code': PythonCompleter(lambda: self.curframe.f_globals, lambda: self.curframe.f_locals), 'breakpoint': BreakPointListCompleter(), 'pdb_command': PdbCommandsCompleter(self), 'python_file': PythonFileCompleter(), 'python_function': PythonFunctionCompleter(self), }) self.validator = GrammarValidator(g, {'python_code': PythonValidator()}) # Make sure not to start in Vi navigation mode. self.python_input.key_bindings_manager.reset(self.cli) self.cli.buffers[DEFAULT_BUFFER].reset() def pre_run(): self._source_code_window.vertical_scroll = 100000 # source_code_doc.line_count try: return self.cli.run(reset_current_buffer=False, pre_run=pre_run).text except EOFError: # Turn Control-D key press into a 'quit' command. return 'quit'
def test_running_with_pipeline(clean_redis, iredis_client, capfd): grammar = get_command_grammar("get") completer = GrammarCompleter(grammar, get_completer_mapping()) clean_redis.set("foo", "hello \n world") with pytest.raises(StopIteration): next(iredis_client.send_command("get foo | grep w", completer)) out, err = capfd.readouterr() assert out == " world\n"
def _path_completer(self) -> GrammarCompleter: if self._path_completer_cache is None: self._path_completer_cache = GrammarCompleter( self._path_completer_grammar, { "var1": PathCompleter(expanduser=True), "var2": PathCompleter(expanduser=True), }, ) return self._path_completer_cache
def create_command_completer(editor): commands = [c + ' ' for c in get_commands()] return GrammarCompleter(COMMAND_GRAMMAR, { 'command': WordCompleter(commands), 'location': PathCompleter(expanduser=True), 'set_option': WordCompleter(sorted(SET_COMMANDS)), 'buffer_name': BufferNameCompleter(editor), 'colorscheme': ColorSchemeCompleter(editor), 'shell_command': SystemCompleter(), })
def get_completer(self, input_text): try: command, _ = split_command_args(input_text, all_commands) # here will compile grammar for this command grammar = get_command_grammar(command) completer = GrammarCompleter(compiled_grammar=grammar, completers=self.completer_mapping) except InvalidArguments: completer = self.root_completer return completer
def start(): g = create_grammar() lexer = GrammarLexer(g, lexers={ 'op_mac': SimpleLexer(Token.Operator), 'op_main': SimpleLexer(Token.Operator), 'op_instance': SimpleLexer(Token.Operator), 'op_configuration': SimpleLexer(Token.Operator), 'op_infrastructure': SimpleLexer(Token.Operator), 'op_parameter': SimpleLexer(Token.Text), }) completer = GrammarCompleter(g, { 'op_main': WordCompleter(op_main), 'op_instance': WordCompleter(op_instance), 'op_configuration': WordCompleter(op_configuration), 'op_infrastructure': WordCompleter(op_infrastructure), }) history = InMemoryHistory() parser = maccli.mac_cli.initialize_parser() show("Start typing 'mac', CTRL+C to exit") user_aborted = False program_running = True while program_running: try: text = prompt('> ', lexer=lexer, completer=completer, style=MacStyle, history=history, auto_suggest=AutoSuggestFromHistory()) argv_raw = shlex.split(text) argv = maccli.mac_cli.patch_help_option(argv_raw) args = parser.parse_args(argv) maccli.mac_cli.dispatch_cmds(args) user_aborted = False except InternalError as e: maccli.logger.debug("Code raised Internal Error", e) pass except EOFError as e: maccli.logger.debug("Code raised EOFError", e) pass except KeyboardInterrupt as e: maccli.logger.debug("Code raised KeyboardInterrupt", e) if user_aborted: program_running = False else: user_aborted = True show("Press CTRL+C again to exit") pass
def _create_path_completer(self): def unwrapper(text): return re.sub(r'\\(.)', r'\1', text) def single_quoted_wrapper(text): return text.replace('\\', '\\\\').replace("'", "\\'") def double_quoted_wrapper(text): return text.replace('\\', '\\\\').replace('"', '\\"') grammar = r""" # Text before the current string. ( [^'"#] | # Not quoted characters. ''' ([^']|'[^']|''[^']|\\.)* ''' | # Inside single quoted triple strings "" " ([^"]|"[^"]|""[^"]|\\.)* "" " | # Inside double quoted triple strings \#[^\n]*(\n|$) | # Comment. "(?!"") ([^"\\]|\\.)*" | # Inside double quoted strings. '(?!'') ([^'\\]|\\.)*' # Inside single quoted strings. # Warning: The negative lookahead in the above two # statements is important. If we drop that, # then the regex will try to interpret every # triple quoted string also as a single quoted # string, making this exponentially expensive to # execute! )* # The current string that we're completing. ( ' (?P<var1>([^\n'\\]|\\.)*) | # Inside a single quoted string. " (?P<var2>([^\n"\\]|\\.)*) # Inside a double quoted string. ) """ g = compile_grammar(grammar, escape_funcs={ 'var1': single_quoted_wrapper, 'var2': double_quoted_wrapper, }, unescape_funcs={ 'var1': unwrapper, 'var2': unwrapper, }) return g, GrammarCompleter( g, { 'var1': PathCompleter(expanduser=True), 'var2': PathCompleter(expanduser=True), })
def apply_transformation( self, transformation_input: TransformationInput) -> Transformation: input_text = transformation_input.document.text try: command, _ = split_command_args(input_text, all_commands) except InvalidArguments: self.command_holder.command = None self.session.completer = default_completer self.session.lexer = default_lexer else: self.command_holder.command = command.upper() # compile grammar for this command grammar = get_command_grammar(command) lexer = GrammarLexer(grammar, lexers=lexers_mapping) completer = GrammarCompleter(grammar, completer_mapping) self.session.completer = completer self.session.lexer = lexer return Transformation(transformation_input.fragments)
def _create_path_completer(self): def unwrapper(text): return re.sub(r'\\(.)', r'\1', text) def single_quoted_wrapper(text): return text.replace('\\', '\\\\').replace("'", "\\'") def double_quoted_wrapper(text): return text.replace('\\', '\\\\').replace('"', '\\"') grammar = r""" # Text before the current string. ( [^'"#] | # Not quoted characters. '''.*''' | # Inside single quoted triple strings "" ".*"" " | # Inside double quoted triple strings \#[^\n]* | # Comment. "([^"\\]|\\.)*" | # Inside double quoted strings. '([^'\\]|\\.)*' # Inside single quoted strings. )* # The current string that we're completing. ( ' (?P<var1>([^\n'\\]|\\.)*) | # Inside a single quoted string. " (?P<var2>([^\n"\\]|\\.)*) # Inside a double quoted string. ) """ g = compile_grammar(grammar, escape_funcs={ 'var1': single_quoted_wrapper, 'var2': double_quoted_wrapper, }, unescape_funcs={ 'var1': unwrapper, 'var2': unwrapper, }) return g, GrammarCompleter(g, { 'var1': PathCompleter(), 'var2': PathCompleter(), })
def __init__(self, hint=False, completion_casing="upper"): super().__init__() self.completer_mapping = get_completer_mapping(hint, completion_casing) self.current_completer = self.root_completer = GrammarCompleter( command_grammar, self.completer_mapping)
example_style = Style.from_dict({ 'filter': '#33aa33 bold', 'trailing-input': 'bg:#662222 #ffffff', }) if __name__ == '__main__': g = create_grammar() lexer = GrammarLexer(g, lexers={ 'operator': SimpleLexer('class:operator'), 'var': SimpleLexer('class:number'), }) completer = GrammarCompleter(g, { 'operator': WordCompleter(operators), }) try: # REPL loop. while True: # Read input and parse the result. text = prompt('Calculate: ', lexer=lexer, completer=completer, style=example_style) m = g.match(text) if m: v = m.variables() else: print('Invalid command\n')
if __name__ == '__main__': g = create_grammar() lexer = GrammarLexer(g, tokens={ 'operator1': Token.Operator, 'operator2': Token.Operator, 'var1': Token.Number, 'var2': Token.Number }) completer = GrammarCompleter( g, { 'operator1': WordCompleter(operators1), 'operator2': WordCompleter(operators2), }) try: # REPL loop. while True: # Read input and parse the result. text = get_input('Calculate: ', lexer=lexer, completer=completer, style=ExampleStyle) m = g.match(text) if m: vars = m.variables() else:
def __init__(self): super().__init__() self.completer_mapping = get_completer_mapping() self.current_completer = self.root_completer = GrammarCompleter( command_grammar, self.completer_mapping)
member_completer = LatestUsedFirstWordCompleter(config.completer_max, []) field_completer = LatestUsedFirstWordCompleter(config.completer_max, []) completer_mapping.update( { # all key related completers share the same completer "keys": key_completer, "key": key_completer, "destination": key_completer, "newkey": key_completer, # member "member": member_completer, "members": member_completer, # hash fields "field": field_completer, "fields": field_completer, } ) # patch command completer with hint command_hint = {key: info["summary"] for key, info in commands_summary.items()} hint = {command: command_hint.get(command.upper()) for command in all_commands} completer_mapping["command_pending"] = WordCompleter( all_commands[::-1], ignore_case=True, sentence=True, meta_dict=hint ) return completer_mapping completer_mapping = get_completer_mapping() default_completer = GrammarCompleter(command_grammar, completer_mapping)
g = create_grammar() lexer = GrammarLexer( g, lexers={ "operator1": SimpleLexer("class:operator"), "operator2": SimpleLexer("class:operator"), "var1": SimpleLexer("class:number"), "var2": SimpleLexer("class:number"), }, ) completer = GrammarCompleter( g, { "operator1": WordCompleter(operators1), "operator2": WordCompleter(operators2), }, ) try: # REPL loop. while True: # Read input and parse the result. text = prompt(">", lexer=lexer, completer=completer, style=example_style) m = g.match(text) if m:
class UHURepl: """The main class for UpdateHub REPL.""" completer = GrammarCompleter( GRAMMAR, { 'command': WordCompleter(COMMANDS), 'group': WordCompleter(GROUPS), 'hardware': WordCompleter(GROUPS['hardware']), 'product': WordCompleter(GROUPS['product']), 'package': WordCompleter(GROUPS['package']) }) def __init__(self, package_fn=None): """Creates a new interactive prompt. The interactive prompt will work using an exisiting configuration file (if it exists). It is also able to load an existing configuration file placed in a non standard place if REPL is created using the `package_fn`. Finally, if there is no configuration file present in the working directory, neighter a package file is explicty passed, `UHURepl` will create a new one. :param package_fn: An UHU package filename. """ self.local_config = get_local_config_file() if package_fn is not None: self.package = self.load_package(package_fn) elif os.path.exists(self.local_config): self.package = self.load_package(self.local_config) else: self.package = Package() if self.package.product: self.prompt = set_product_prompt(self.package.product) else: self.prompt = 'uhu> ' self.arg = None self.history = InMemoryHistory() @staticmethod def load_package(fn): try: return load_package(fn) except ValueError as err: print('Error: Invalid configuration file: {}'.format(err)) sys.exit(1) def repl(self): """Starts a new interactive prompt.""" print('UpdateHub Utils {}'.format(get_version())) while True: try: expression = prompt(self.prompt, completer=self.completer, history=self.history) except CancelPromptException: sys.exit(1) # User has typed Ctrl C try: command = self.get_command(expression) except TypeError: # Invalid expression print('ERROR: Invalid command') continue except ValueError: # Empty prompt continue else: self.run_command(command) def get_command(self, expression): """Given an expression, returns a valid command. :param expression: The full expression typed by user on prompt. """ expression = GRAMMAR.match(expression) if expression is None: raise TypeError vars_ = expression.variables() cmd, cmd_group = vars_.get('command'), vars_.get('group') self.arg = vars_.get('arg') if cmd is not None: command = COMMANDS.get(cmd) elif cmd_group is not None: group, cmd = GROUPS.get(cmd_group), vars_.get(cmd_group) command = group.get(cmd) else: raise ValueError('Invalid command') return command def run_command(self, command): """Executes an given command. If command runs successfully, persists the configuration state into a file. Otherwise, shows the error message to user. """ if command is None: print('Invalid command') return try: command(self) except Exception as err: # pylint: disable=broad-except print('\033[91mError:\033[0m {}'.format(err)) else: # save package in every successful command dump_package(self.package.to_template(), self.local_config)
def initializeGrammarCompleter(self): self.grammarCompleter = GrammarCompleter(self.grammar, self.completers)