Skip to content

Commit 622a435

Browse files
committed
Add __repr__ methods to interface types and Node, for nicer printing/logging
1 parent 56680f8 commit 622a435

File tree

4 files changed

+46
-0
lines changed

4 files changed

+46
-0
lines changed

meshtastic/ble_interface.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,17 @@ def __init__(
8383
# Note: the on disconnected callback will call our self.close which will make us nicely wait for threads to exit
8484
self._exit_handler = atexit.register(self.client.disconnect)
8585

86+
def __repr__(self):
87+
rep = f"BLEInterface(address={self.client.address if self.client else None!r}"
88+
if self.debugOut is not None:
89+
rep += f", debugOut={self.debugOut!r}"
90+
if self.noProto:
91+
rep += ", noProto=True"
92+
if self.noNodes:
93+
rep += ", noNodes=True"
94+
rep += ")"
95+
return rep
96+
8697
def from_num_handler(self, _, b: bytes) -> None: # pylint: disable=C0116
8798
"""Handle callbacks for fromnum notify.
8899
Note: this method does not need to be async because it is just setting a bool.

meshtastic/node.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,15 @@ def __init__(self, iface, nodeNum, noProto=False, timeout: int = 300):
4242

4343
self.gotResponse = None
4444

45+
def __repr__(self):
46+
r = f"Node({self.iface!r}, 0x{self.nodeNum:08x}"
47+
if self.noProto:
48+
r += ", noProto=True"
49+
if self._timeout.expireTimeout != 300:
50+
r += ", timeout={self._timeout.expireTimeout!r}"
51+
r += ")"
52+
return r
53+
4554
def showChannels(self):
4655
"""Show human readable description of our channels."""
4756
print("Channels:")

meshtastic/serial_interface.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,17 @@ def __init__(self, devPath: Optional[str]=None, debugOut=None, noProto: bool=Fal
6666
self, debugOut=debugOut, noProto=noProto, connectNow=connectNow, noNodes=noNodes
6767
)
6868

69+
def __repr__(self):
70+
rep = f"SerialInterface(devPath={self.devPath!r}"
71+
if hasattr(self, 'debugOut') and self.debugOut is not None:
72+
rep += f", debugOut={self.debugOut!r}"
73+
if self.noProto:
74+
rep += ", noProto=True"
75+
if hasattr(self, 'noNodes') and self.noNodes:
76+
rep += ", noNodes=True"
77+
rep += ")"
78+
return rep
79+
6980
def close(self) -> None:
7081
"""Close a connection to the device"""
7182
if self.stream: # Stream can be null if we were already closed

meshtastic/tcp_interface.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,21 @@ def __init__(
4343

4444
super().__init__(debugOut=debugOut, noProto=noProto, connectNow=connectNow, noNodes=noNodes)
4545

46+
def __repr__(self):
47+
rep = f"TCPInterface({self.hostname!r}"
48+
if self.debugOut is not None:
49+
rep += f", debugOut={self.debugOut!r}"
50+
if self.noProto:
51+
rep += ", noProto=True"
52+
if self.socket is None:
53+
rep += ", connectNow=False"
54+
if self.portNumber != DEFAULT_TCP_PORT:
55+
rep += f", portNumber={self.portNumber!r}"
56+
if self.noNodes:
57+
rep += ", noNodes=True"
58+
rep += ")"
59+
return rep
60+
4661
def _socket_shutdown(self) -> None:
4762
"""Shutdown the socket.
4863
Note: Broke out this line so the exception could be unit tested.

0 commit comments

Comments
 (0)