One of Moler’s core design goals is to be IO-agnostic. The sameDocumentation Index
Fetch the complete documentation index at: https://mintlify.com/nokia/moler/llms.txt
Use this file to discover all available pages before exploring further.
Command and Event objects work identically whether the underlying transport is a local terminal, a TCP socket, an SSH shell, or an in-memory buffer used for testing.
This is achieved by splitting the connection into two layers:
- External IO (
io_connection) — handles actual bytes: opening/closing the channel, reading, writing. Examples:ThreadedTerminal,ThreadedTcp,ThreadedSshShell. - Moler connection (
moler_connection) — anAbstractMolerConnectionthat dispatches decoded data to all registeredConnectionObservers (commands and events) and exposessendline()/send().
Getting a connection
Theget_connection() function is the standard way to obtain a connection object:
io_type or name (a named connection from config), but not both.
Using a connection as a context manager
Connections implement the context manager protocol. Usewith connection.open(): to ensure the connection is always closed, even if an exception occurs:
Pass
terminal.moler_connection (not terminal itself) to command and event constructors. Commands and events operate on the moler-connection layer, not the IO layer directly.Connection types
io_type | Description | Platform |
|---|---|---|
terminal | Spawns a local PTY shell process | Unix only |
terminal_no_fork | Terminal without forking | Unix only |
tcp | Raw TCP socket | All |
sshshell | SSH shell session via Paramiko | All |
memory | In-memory FIFO buffer (for testing) | All |
Variants (execution model)
Eachio_type can have multiple variants that differ in their concurrency model:
| Variant | Description |
|---|---|
threaded / multi-threaded | Uses threads for I/O. Default for terminal and sshshell. |
single-threaded | Uses a single-thread runner (MolerConnectionForSingleThreadRunner). |
asyncio | Uses asyncio event loop (available for terminal and tcp). |
asyncio-in-thread | Runs the asyncio loop in a dedicated thread. |
terminal and sshshell is threaded. You can override defaults in config:
Building a connection manually
For full control, build the IO and moler layers yourself:get_connection(io_type='terminal', variant='multi-threaded') but without going through the factory.
Named connections
Connections can be named in the config file and referenced by name:Registering custom connections
ConnectionFactory is a plugin system. Register any callable that produces a connection object for a given (io_type, variant) pair:
Connection responsibilities
The moler-connection (AbstractMolerConnection) is responsible for:
- Sending —
send(data)andsendline(data)write encoded bytes through the external IO layer. - Receiving and dispatching — incoming bytes are decoded and delivered to every subscribed
ConnectionObservervia theirdata_received()method. - Encoding/decoding — configurable encoder/decoder pair (defaults to UTF-8 for most types; terminal uses VT100-cleaned Unicode).
- Logging — raw I/O is optionally written to device log files.
Commands
How commands use the moler connection to send and receive
Devices
How devices manage their connection lifecycle