moler.io.asyncio package

Submodules

moler.io.asyncio.tcp module

External-IO connections based on asyncio.

The only 3 requirements for these connections are: (1) store Moler’s connection inside self.moler_connection attribute (2) plugin into Moler’s connection the way IO outputs data to external world:

self.moler_connection.how2send = self.send
  1. forward IO received data into self.moler_connection.data_received(data)
class moler.io.asyncio.tcp.AsyncioInThreadTcp(moler_connection, port, host='localhost', receive_buffer_size=262144, logger=None)

Bases: moler.io.io_connection.IOConnection

Implementation of TCP connection using asyncio running in dedicated thread.

close()

Close TCP connection.

Connection should allow for calling close on closed/not-open connection.

open()

Open TCP connection.

class moler.io.asyncio.tcp.AsyncioTcp(moler_connection, port, host='localhost', receive_buffer_size=262144, logger=None)

Bases: moler.io.io_connection.IOConnection

Implementation of TCP connection using asyncio.

close()

Close TCP connection.

Connection should allow for calling close on closed/not-open connection.

forward_connection_read_data()
open()

Open TCP connection.

send(data)

Send data via TCP service.

Parameters:data (str) – data

moler.io.asyncio.terminal module

External-IO connections based on asyncio.

The only 3 requirements for these connections are: (1) store Moler’s connection inside self.moler_connection attribute (2) plugin into Moler’s connection the way IO outputs data to external world:

self.moler_connection.how2send = self.send
  1. forward IO received data into self.moler_connection.data_received(data)
class moler.io.asyncio.terminal.AsyncioInThreadTerminal(moler_connection, cmd=None, first_prompt=None, dimensions=(100, 300), logger=None)

Bases: moler.io.io_connection.IOConnection

Implementation of Terminal connection using asyncio running in dedicated thread.

close()

Close TCP connection.

Connection should allow for calling close on closed/not-open connection.

logger
name
notify(callback, when)

Adds subscriber to list of functions to call :param callback: reference to function to call when connection is open/established :param when: connection state change :return: None

open()

Open TCP connection.

class moler.io.asyncio.terminal.AsyncioTerminal(moler_connection, cmd=None, first_prompt=None, dimensions=(100, 300), logger=None)

Bases: moler.io.io_connection.IOConnection

Implementation of Terminal connection using asyncio.

close()

Close AsyncioTerminal connection.

Connection should allow for calling close on closed/not-open connection.

data_received(data, recv_time)

Await initial prompt of started shell command.

After that - do real forward

name
open()

Open AsyncioTerminal connection & start running it inside asyncio loop.

send(data)

Write data into AsyncioTerminal connection.

class moler.io.asyncio.terminal.PtySubprocessProtocol(pty_fd=None)

Bases: asyncio.protocols.SubprocessProtocol

connection_made(transport)

Called when a connection is made.

The argument is the transport representing the pipe connection. To receive data, wait for data_received() calls. When the connection is closed, connection_lost() is called.

data_received(data, recv_time)
on_process_exited(return_code)
on_pty_close(exc)
on_pty_open()
pipe_connection_lost(fd, exc)

Called when a file descriptor associated with the child process is closed.

fd is the int file descriptor that was closed.

pipe_data_received(fd, data)

Called when the subprocess writes data into stdout/stderr pipe.

fd is int file descriptor. data is bytes object.

process_exited()

Called when subprocess has exited.

send(data)
moler.io.asyncio.terminal.open_terminal(dimensions)

Open pseudo-Terminal and configure it’s dimensions

Parameters:dimensions – terminal dimensions (rows, columns)
Returns:(master, slave) file descriptors of Pty
moler.io.asyncio.terminal.run_command(cmd, cwd)
moler.io.asyncio.terminal.start_reading_pty(protocol, pty_fd)

Make asyncio to read file descriptor of Pty

Parameters:
  • protocol – protocol of subprocess speaking via Pty
  • pty_fd – file descriptor of Pty (dialog with subprocess goes that way)
Returns:

moler.io.asyncio.terminal.start_subprocess_in_terminal(protocol_class, cmd=None, cwd=None, env=None, dimensions=(100, 300))

Start subprocess that will run cmd inside terminal.

Some commands run differently when they detect “I’m running at terminal” (stdin/stdout/stderr are bound to terminal device) They assume human interaction so, for example they display “Password:” prompt.

Parameters:
  • protocol_class
  • cmd – command to be run at terminal
  • cwd – working directory when to start that command
  • env – environment for command
  • dimensions – terminal dimensions (rows, columns)
Returns:

moler.io.asyncio.terminal.terminal_io_test()

Module contents

External-IO connections based on asyncio

The only 3 requirements for these connections is: (1) store Moler’s connection inside self.moler_connection attribute (2) plugin into Moler’s connection the way IO outputs data to external world:

self.moler_connection.how2send = self.send
  1. forward IO received data into self.moler_connection.data_received(data)