Refactor Transport from Protocol to ABC for explicit interface implementation

This commit is contained in:
2025-06-25 19:25:42 +00:00
parent 11a100832f
commit 82a587a720
2 changed files with 26 additions and 14 deletions

View File

@@ -1,31 +1,38 @@
"""Base transport protocol for instrument communication."""
"""Base transport interface for instrument communication."""
from typing import Protocol
from abc import ABC, abstractmethod
class Transport(Protocol):
class Transport(ABC):
"""Abstract transport interface for instrument communication.
This protocol defines the interface that all transport implementations
(TCP, VISA, etc.) must satisfy. It provides basic connection management
and communication primitives for SCPI-based instruments.
This abstract base class defines the interface that all transport
implementations (TCP, VISA, etc.) must implement. It provides basic
connection management and communication primitives for SCPI-based
instruments.
Implementations must inherit from this class and implement all abstract
methods.
"""
@abstractmethod
def connect(self) -> None:
"""Establish connection to instrument.
Raises:
ConnectionError: If connection fails.
"""
...
pass
@abstractmethod
def disconnect(self) -> None:
"""Close connection to instrument.
Should be idempotent - safe to call multiple times.
"""
...
pass
@abstractmethod
def write(self, command: str) -> None:
"""Send command to instrument.
@@ -36,8 +43,9 @@ class Transport(Protocol):
ConnectionError: If not connected.
IOError: If write fails.
"""
...
pass
@abstractmethod
def read(self, timeout: float | None = None) -> str:
"""Read response from instrument.
@@ -52,8 +60,9 @@ class Transport(Protocol):
TimeoutError: If read times out.
IOError: If read fails.
"""
...
pass
@abstractmethod
def query(self, command: str, timeout: float | None = None) -> str:
"""Send command and read response.
@@ -71,13 +80,14 @@ class Transport(Protocol):
TimeoutError: If read times out.
IOError: If communication fails.
"""
...
pass
@property
@abstractmethod
def is_connected(self) -> bool:
"""Check if connection is active.
Returns:
True if connected, False otherwise.
"""
...
pass