Python prompt_toolkit.shortcuts.create_eventloop() Examples

The following are 6 code examples of prompt_toolkit.shortcuts.create_eventloop(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module prompt_toolkit.shortcuts , or try the search function .
Example #1
Source File: prompt.py    From aq with MIT License 6 votes vote down vote up
def __init__(self, parser, engine, options=None):
        self.parser = parser
        self.engine = engine
        self.options = options if options is not None else {}
        util.ensure_data_dir_exists()
        application = create_prompt_application(
            message='> ',
            lexer=PygmentsLexer(SqlLexer),
            history=FileHistory(os.path.expanduser('~/.aq/history')),
            completer=AqCompleter(schemas=engine.available_schemas, tables=engine.available_tables),
            auto_suggest=AutoSuggestFromHistory(),
            validator=QueryValidator(parser),
            on_abort=AbortAction.RETRY,
        )
        loop = create_eventloop()
        self.cli = CommandLineInterface(application=application, eventloop=loop)
        self.patch_context = self.cli.patch_stdout_context() 
Example #2
Source File: utils.py    From contrail-api-cli with MIT License 5 votes vote down vote up
def eventloop():
    # Allow to keep gevent greenlets running
    # while waiting for some input on the cli
    def inputhook(context):
        while not context.input_is_ready():
            gevent.sleep(0.1)

    return create_eventloop(inputhook=inputhook) 
Example #3
Source File: app.py    From azure-cli-shell with MIT License 5 votes vote down vote up
def create_interface(self):
        """ instantiates the intereface """
        return CommandLineInterface(
            application=self.create_application(),
            eventloop=create_eventloop()) 
Example #4
Source File: app.py    From azure-cli-shell with MIT License 5 votes vote down vote up
def example_repl(self, text, example, start_index, continue_flag):
        """ REPL for interactive tutorials """

        if start_index:
            start_index = start_index + 1
            cmd = ' '.join(text.split()[:start_index])
            example_cli = CommandLineInterface(
                application=self.create_application(
                    full_layout=False),
                eventloop=create_eventloop())
            example_cli.buffers['example_line'].reset(
                initial_document=Document(u'{}\n'.format(
                    add_random_new_lines(example)))
            )
            while start_index < len(text.split()):
                if self.default_command:
                    cmd = cmd.replace(self.default_command + ' ', '')
                example_cli.buffers[DEFAULT_BUFFER].reset(
                    initial_document=Document(
                        u'{}'.format(cmd),
                        cursor_position=len(cmd)))
                example_cli.request_redraw()
                answer = example_cli.run()
                if not answer:
                    return "", True
                answer = answer.text
                if answer.strip('\n') == cmd.strip('\n'):
                    continue
                else:
                    if len(answer.split()) > 1:
                        start_index += 1
                        cmd += " " + answer.split()[-1] + " " +\
                               u' '.join(text.split()[start_index:start_index + 1])
            example_cli.exit()
            del example_cli
        else:
            cmd = text

        return cmd, continue_flag

    # pylint: disable=too-many-branches 
Example #5
Source File: app.py    From aws-shell with Apache License 2.0 5 votes vote down vote up
def create_cli_interface(self, display_completions_in_columns):
        # A CommandLineInterface from prompt_toolkit
        # accepts two things: an application and an
        # event loop.
        loop = create_eventloop()
        app = self.create_application(self.completer,
                                      self.file_history,
                                      display_completions_in_columns)
        cli = CommandLineInterface(application=app, eventloop=loop,
                                   input=self._input, output=self._output)
        return cli 
Example #6
Source File: main.py    From cycli with MIT License 4 votes vote down vote up
def run(self):
    labels = self.neo4j.get_labels()
    relationship_types = self.neo4j.get_relationship_types()
    properties = self.neo4j.get_property_keys()

    if self.filename:
      with io.open(self.filename, "r") as f:
        queries = split_queries_on_semicolons(f.read())

        for query in queries:
          print("> " + query)
          self.handle_query(query)
          print()

        return

    click.secho(" ______     __  __     ______     __         __    ", fg="red")
    click.secho("/\  ___\   /\ \_\ \   /\  ___\   /\ \       /\ \   ", fg="yellow")
    click.secho("\ \ \____  \ \____ \  \ \ \____  \ \ \____  \ \ \  ", fg="green")
    click.secho(" \ \_____\  \/\_____\  \ \_____\  \ \_____\  \ \_\ ", fg="blue")
    click.secho("  \/_____/   \/_____/   \/_____/   \/_____/   \/_/ ", fg="magenta")

    print("Cycli version: {}".format(__version__))
    print("Neo4j version: {}".format(".".join(map(str, self.neo4j.neo4j_version))))
    print("Bug reports: https://github.com/nicolewhite/cycli/issues\n")

    completer = CypherCompleter(labels, relationship_types, properties)

    layout = create_prompt_layout(
      lexer=CypherLexer,
      get_prompt_tokens=get_tokens,
      reserve_space_for_menu=8,
    )

    buff = CypherBuffer(
      accept_action=AcceptAction.RETURN_DOCUMENT,
      history=FileHistory(filename=os.path.expanduser('~/.cycli_history')),
      completer=completer,
      complete_while_typing=True,
    )

    application = Application(
      style=PygmentsStyle(CypherStyle),
      buffer=buff,
      layout=layout,
      on_exit=AbortAction.RAISE_EXCEPTION,
      key_bindings_registry=CypherBinder.registry
    )

    cli = CommandLineInterface(application=application, eventloop=create_eventloop())

    try:
      while True:
        document = cli.run()
        query = document.text
        self.handle_query(query)
    except UserWantsOut:
      print("Goodbye!")
    except Exception as e:
      print(e)