예제 #1
0
    async def _interact(self) -> None:
        if self._chan is None:
            # Should not happen.
            raise Exception("`_interact` called before `connection_made`.")

        if hasattr(self._chan,
                   "set_line_mode") and self._chan._editor is not None:
            # Disable the line editing provided by asyncssh. Prompt_toolkit
            # provides the line editing.
            self._chan.set_line_mode(False)

        term = self._chan.get_terminal_type()

        self._output = Vt100_Output(self.stdout,
                                    self._get_size,
                                    term=term,
                                    write_binary=False)
        with create_app_session(input=self._input,
                                output=self._output) as session:
            self.app_session = session
            try:
                await self.interact(self)
            except BaseException:
                traceback.print_exc()
            finally:
                # Close the connection.
                self._chan.close()
                self._input.close()
예제 #2
0
    async def run_application(self) -> None:
        """
        Run application.
        """
        def handle_incoming_data() -> None:
            data = self.conn.recv(1024)
            if data:
                self.feed(data)
            else:
                # Connection closed by client.
                logger.info("Connection closed by client. %r %r" % self.addr)
                self.close()

        # Add reader.
        loop = get_event_loop()
        loop.add_reader(self.conn, handle_incoming_data)

        try:
            # Wait for v100_output to be properly instantiated
            await self._ready.wait()
            with create_app_session(input=self.vt100_input,
                                    output=self.vt100_output):
                self.context = contextvars.copy_context()
                await self.interact(self)
        except Exception as e:
            print("Got %s" % type(e).__name__, e)
            import traceback

            traceback.print_exc()
            raise
        finally:
            self.close()
예제 #3
0
def feed_app_with_input(type, message, text, **kwargs):
    """
    Create an application, feed it with the given user input and return
    the CLI object.

    This returns a (result, CLI) tuple.
    note: this only works if you import your prompt and then this function!!
    """
    # If the given text doesn't end with a newline, the interface won't finish.
    assert text.endswith('\r')

    inp = PosixPipeInput(text)

    try:
        with create_app_session(input=inp, output=DummyOutput()) as session:
            application = getattr(prompts, type).question(message, **kwargs)
            #print(application.input)
            #breakpoint()

            if isinstance(application, Application):
                result = application.run()
            elif isinstance(application, PromptSession):
                result = application.prompt()
            return result
    finally:
        inp.close()
예제 #4
0
    async def run_application(self):
        """
        Run application.
        """
        def handle_incoming_data():
            data = self.conn.recv(1024)
            if data:
                self.feed(data)
            else:
                # Connection closed by client.
                logger.info('Connection closed by client. %r %r' % self.addr)
                self.close()

        async def run():
            # Add reader.
            loop = get_event_loop()
            loop.add_reader(self.conn, handle_incoming_data)

            try:
                await self.interact(self)
            except Exception as e:
                print('Got %s' % type(e).__name__, e)
                import traceback
                traceback.print_exc()
                raise
            finally:
                self.close()

        with create_app_session(input=self.vt100_input,
                                output=self.vt100_output):
            self.context = contextvars.copy_context()
            return await run()
    async def run_application(self) -> None:
        """
        Run application.
        """
        def handle_incoming_data() -> None:
            data = self.conn.recv(1024)
            if data:
                self.feed(data)
            else:
                # Connection closed by client.
                logger.info('Connection closed by client. %r %r' % self.addr)
                self.close()

        async def run() -> None:
            # Add reader.
            loop = get_event_loop()
            loop.add_reader(self.conn, handle_incoming_data)

            try:
                await self.interact(self)
            except Exception as e:
                print('Got %s' % type(e).__name__, e)
                import traceback; traceback.print_exc()
                raise
            finally:
                self.close()

        with create_app_session(input=self.vt100_input, output=self.vt100_output):
            self.context = contextvars.copy_context()
            await run()
예제 #6
0
def mock_input() -> Generator[MockInput, None, None]:
    with create_pipe_input() as pipe_input:
        with create_app_session(input=pipe_input):

            def factory(
                    input_sequence: Sequence[str]) -> None:  # noqa: WPS 430
                pipe_input.send_text("".join(input_sequence))

            yield factory
예제 #7
0
 def _main_wrapped(self):
     asyncio.set_event_loop(asyncio.new_event_loop())
     with create_app_session(input=self.input, output=self.output):
         # Wait until here to redirect stdout and/or stderr otherwise errors
         # and output from the previous code would be written to the Terminal
         # node rather than editor Output.
         if self.redirect_stdout:
             sys.stdout = self.stdout
         if self.redirect_stderr:
             sys.stderr = self.stdout
         self.main()
예제 #8
0
    async def _interact(self) -> None:
        if self._chan is None:
            # Should not happen.
            raise Exception("`_interact` called before `connection_made`.")

        # Disable the line editing provided by asyncssh. Prompt_toolkit
        # provides the line editing.
        self._chan.set_line_mode(False)

        with create_app_session(input=self._input, output=self._output) as session:
            self.app_session = session
            try:
                await self.interact()
            except BaseException:
                traceback.print_exc()
            finally:
                # Close the connection.
                self._chan.close()
    async def _interact(self) -> None:
        if self._chan is None:
            # Should not happen.
            raise Exception('`_interact` called before `connection_made`.')

        # Disable the line editing provided by asyncssh. Prompt_toolkit
        # provides the line editing.
        self._chan.set_line_mode(False)

        with create_app_session(input=self._input, output=self._output) as session:
            self.app_session = session
            try:
                await self.interact()
            except BaseException:
                traceback.print_exc()
            finally:
                # Close the connection.
                self._chan.close()