From 622a435465872fd316054f1a4a7dd6d4d44bb3ec Mon Sep 17 00:00:00 2001 From: Ian McEwen Date: Thu, 8 May 2025 21:22:49 -0700 Subject: [PATCH] Add __repr__ methods to interface types and Node, for nicer printing/logging --- meshtastic/ble_interface.py | 11 +++++++++++ meshtastic/node.py | 9 +++++++++ meshtastic/serial_interface.py | 11 +++++++++++ meshtastic/tcp_interface.py | 15 +++++++++++++++ 4 files changed, 46 insertions(+) diff --git a/meshtastic/ble_interface.py b/meshtastic/ble_interface.py index 76e5dc3..5f32edf 100644 --- a/meshtastic/ble_interface.py +++ b/meshtastic/ble_interface.py @@ -83,6 +83,17 @@ class BLEInterface(MeshInterface): # Note: the on disconnected callback will call our self.close which will make us nicely wait for threads to exit self._exit_handler = atexit.register(self.client.disconnect) + def __repr__(self): + rep = f"BLEInterface(address={self.client.address if self.client else None!r}" + if self.debugOut is not None: + rep += f", debugOut={self.debugOut!r}" + if self.noProto: + rep += ", noProto=True" + if self.noNodes: + rep += ", noNodes=True" + rep += ")" + return rep + def from_num_handler(self, _, b: bytes) -> None: # pylint: disable=C0116 """Handle callbacks for fromnum notify. Note: this method does not need to be async because it is just setting a bool. diff --git a/meshtastic/node.py b/meshtastic/node.py index 53c91db..e54963c 100644 --- a/meshtastic/node.py +++ b/meshtastic/node.py @@ -42,6 +42,15 @@ class Node: self.gotResponse = None + def __repr__(self): + r = f"Node({self.iface!r}, 0x{self.nodeNum:08x}" + if self.noProto: + r += ", noProto=True" + if self._timeout.expireTimeout != 300: + r += ", timeout={self._timeout.expireTimeout!r}" + r += ")" + return r + def showChannels(self): """Show human readable description of our channels.""" print("Channels:") diff --git a/meshtastic/serial_interface.py b/meshtastic/serial_interface.py index 39f2648..7594ddf 100644 --- a/meshtastic/serial_interface.py +++ b/meshtastic/serial_interface.py @@ -66,6 +66,17 @@ class SerialInterface(StreamInterface): self, debugOut=debugOut, noProto=noProto, connectNow=connectNow, noNodes=noNodes ) + def __repr__(self): + rep = f"SerialInterface(devPath={self.devPath!r}" + if hasattr(self, 'debugOut') and self.debugOut is not None: + rep += f", debugOut={self.debugOut!r}" + if self.noProto: + rep += ", noProto=True" + if hasattr(self, 'noNodes') and self.noNodes: + rep += ", noNodes=True" + rep += ")" + return rep + def close(self) -> None: """Close a connection to the device""" if self.stream: # Stream can be null if we were already closed diff --git a/meshtastic/tcp_interface.py b/meshtastic/tcp_interface.py index 7f5b700..4263da3 100644 --- a/meshtastic/tcp_interface.py +++ b/meshtastic/tcp_interface.py @@ -43,6 +43,21 @@ class TCPInterface(StreamInterface): super().__init__(debugOut=debugOut, noProto=noProto, connectNow=connectNow, noNodes=noNodes) + def __repr__(self): + rep = f"TCPInterface({self.hostname!r}" + if self.debugOut is not None: + rep += f", debugOut={self.debugOut!r}" + if self.noProto: + rep += ", noProto=True" + if self.socket is None: + rep += ", connectNow=False" + if self.portNumber != DEFAULT_TCP_PORT: + rep += f", portNumber={self.portNumber!r}" + if self.noNodes: + rep += ", noNodes=True" + rep += ")" + return rep + def _socket_shutdown(self) -> None: """Shutdown the socket. Note: Broke out this line so the exception could be unit tested.