unitelabs.bus.transports.write_transport

Modified from https://github.com/pyserial/pyserial-asyncio/blob/master/serial_asyncio/__init__.py

Classes

  • WriteTransport

    Interface for write-only transports.

    Bases
    asyncio.WriteTransport,

    Methods

    • __init__(self) -> None

    • open(self) -> None

      Opens the transport and if write buffer currently contains data, sets state to allow future write operations.

    • close(self) -> None

      Closes the transport. If write buffer is empty, sets state to disallow further write operations.

    • get_write_buffer_size(self) -> int

      Calculate the current size of the write buffer.

      Response

      Type
      int
      Description

      The number of bytes in the write buffer.

    • get_write_buffer_limits(self) -> tuple[int, int]

      Get the high and low watermarks for write flow control.

      Response

      Type
      tuple[int, int]
      Description

      a tuple (low, high) where low and high are positive number of bytes.

    • set_write_buffer_limits(self, high : typing.Optional[int], low : typing.Optional[int]) -> None

      Set the high- and low-water limits for write flow control. These two values control when to call the protocol's `pause_writing()` and `resume_writing()` methods. If specified, the low-water limit must be less than or equal to the high-water limit. Neither value can be negative. The defaults are implementation-specific. If only the high-water limit is given, the low-water limit defaults to an implementation-specific value less than or equal to the high-water limit. Setting high to zero forces low to zero as well, and causes `pause_writing()` to be called whenever the buffer becomes non-empty. Setting low to zero causes `resume_writing()` to be called only once the buffer is empty. Use of zero for either limit is generally sub-optimal as it reduces opportunities for doing I/O and computation concurrently.

      Parameters

      • Name
        self
        Type
        Default
        Description

      • Name
        high
        Type
        typing.Optional[int]
        Default
        = None
        Description

        The maximum allowed number of bytes in the write buffer.

      • Name
        low
        Type
        typing.Optional[int]
        Default
        = None
        Description

        The minimum allowed number of bytes in the write buffer.

    • write(self, data : typing.Union[bytes, bytearray, memoryview]) -> None

      Write some data bytes to the transport. This does not block; it buffers the data and arranges for it to be sent out asynchronously.

      Parameters

      • Name
        self
        Type
        Default
        Description

      • Name
        data
        Type
        typing.Union[bytes, bytearray, memoryview]
        Default
        Description

        The bytes to write to the Transport.

    • can_write_eof(self) -> bool

      Whether or not this transport has implemented `write_eof()` method.

      Response

      Type
      bool
      Description

      True if this transport supports `write_eof()`, False if not.

    • write_eof(self) -> None

      Close the write with end-of-file after flushing buffered data. (This is like typing ^D into a UNIX program reading from stdin.) Data may still be received.

    • writelines(self, list_of_data : typing.Iterable[typing.Union[bytes, bytearray, memoryview]]) -> None

      Write a list (or any iterable) of data bytes to the transport. The default implementation concatenates the arguments and calls `write()` on the result.

      Parameters

      • Name
        self
        Type
        Default
        Description

      • Name
        list_of_data
        Type
        typing.Iterable[typing.Union[bytes, bytearray, memoryview]]
        Default
        Description

        The list of bytes to concatenate and write to the Transport.

    • flush(self) -> None

      Flush the write buffer and disable further writing.

    • _ensure_writer(self) -> None

      Sets state to allow future write operations, if transport is still open.

    • _remove_writer(self) -> None

      Sets state to disallow future write operations.

    • _abort(self, exception) -> None

      Closes the transport immediately and sets state to disallow further write operations.

      Parameters

      • Name
        self
        Type
        Default
        Description

      • Name
        exception
        Type
        Default
        = None
        Description

        The exception to propagate to the transport when closing.

    • _safe_write(self) -> None

      Asynchronously write buffered data. This method is called back asynchronously as a writer registered with the asyncio event-loop against the underlying file descriptor for the serial port. If this method is invoked while the transport is closing, and the write-buffer is then emptied by this method, the protocol's `connection_lost()` method will be called with None as its argument.

    • _write(self, data : bytes) -> int

      Write data to the Transport. Called within `_safe_write` for asynchronous writing.

      Parameters

      • Name
        self
        Type
        Default
        Description

      • Name
        data
        Type
        bytes
        Default
        Description

        The bytes to write to the Transport.

      Response

      Type
      int
      Description

      the number of bytes written.

    • _maybe_pause_writing(self) -> None

      To be called whenever the write-buffer size increases. Tests the current write-buffer size against the high water mark configured for this transport. If the high water mark is exceeded, the `Protocol` is instructed to `pause_writing()`.

    • _maybe_resume_protocol(self) -> None

      To be called whenever the write-buffer size decreases. Tests the current write-buffer size against the low water mark configured for this transport. If writing is currently paused and the write-buffer size is below the low water mark, the `Protocol` is instructed to `resume_writing()`.

    • _set_write_buffer_limits(self, low : typing.Optional[int], high : typing.Optional[int]) -> None

      Set the high- and low-water limits for write flow control. By default, the high-water limit is 4 times the high-water limit and if neither is specified, (16384, 65536).

      Parameters

      • Name
        self
        Type
        Default
        Description

      • Name
        low
        Type
        typing.Optional[int]
        Default
        = None
        Description

        The low-water limit for write flow control.

      • Name
        high
        Type
        typing.Optional[int]
        Default
        = None
        Description

        The high-water limit for write flow control.

    • get_protocol(self) -> typing.Optional[P_co]

      Get the current `Protocol` associated with this transport.

      Response

      Type
      typing.Optional[P_co]
      Description

      The current `Protocol` instance.

    • set_protocol(self, protocol : P_co) -> None

      Associate a new `Protocol` with this transport.

      Parameters

      • Name
        self
        Type
        Default
        Description

      • Name
        protocol
        Type
        P_co
        Default
        Description

        The new `Protocol` instance.

      Exceptions

      Type
      TypeError
      Description
      If the protocol is not an instance of asyncio.BaseProtocol or None.
    • is_closing(self) -> bool

      Whether the transport is closing or closed.

      Response

      Type
      bool
      Description

      True if the transport is closing or closed, False otherwise.

    • _open(self) -> None

      Underlying action performed when calling `open`. Subclasses override this method to specify behavior when opening the connection.

    • _close(self) -> None

      Underlying action performed when calling `close`. Subclasses override this method to specify behavior when closing the connection.

    • abort(self) -> None

      Close the transport immediately.

    • _exception(
        self,
        exception : Exception,
        message : str
      ) -> None

      Report a fatal error to the event-loop and abort the transport.

      Parameters

      • Name
        self
        Type
        Default
        Description

      • Name
        exception
        Type
        Exception
        Default
        Description

        The Exception to pass on the the loop's exception handler.

      • Name
        message
        Type
        str
        Default
        Description

        Human-readable text describing the exception's execution state, cause, etc.

    Attributes

    • Name
      _has_writer
      Type
      Value

      = False

      Description

    • Name
      _is_writing_paused
      Type
      Value

      = False

      Description

    • Name
      _write_buffer
      Type
      list[typing.Union[bytes, bytearray, memoryview]]
      Value

      = []

      Description

    • Name
      writes_pending
      Type
      bool
      Value

      = None

      Description

      Whether or not there is data in the write buffer waiting to be written.

    • Name
      _loop
      Type
      Value

      = asyncio.get_event_loop_policy().get_event_loop()

      Description

    • Name
      _protocol
      Type
      typing.Optional[P_co]
      Value

      = None

      Description

    • Name
      _is_closing
      Type
      Value

      = True

      Description

Copyright © 2024