Skip to content

Commit

Permalink
[serial] Hide private functions from pdoc
Browse files Browse the repository at this point in the history
  • Loading branch information
niklaut committed Aug 20, 2024
1 parent 22c78c4 commit a5bafe8
Showing 1 changed file with 10 additions and 9 deletions.
19 changes: 10 additions & 9 deletions src/emdbg/serial/protocol.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
from .utils import find_serial_port
from ..utils import add_datetime as add_dt

LOGGER = logging.getLogger("serial:nsh")
_LOGGER = logging.getLogger("serial:nsh")


class _NshReader(Protocol):
Expand Down Expand Up @@ -74,6 +74,7 @@ def __init__(self, reader_thread: ReaderThread, protocol: _NshReader):
self._serial._data_received = self._print
self._print_data = ""
self.filter_ansi_escapes = True
"""Filter ANSI escape codes from the output."""
self._logfile = None
self.clear()

Expand All @@ -85,7 +86,7 @@ def _print(self, data: str):
if Nsh._NEWLINE in self._print_data:
*lines, self._print_data = self._print_data.split(Nsh._NEWLINE)
for line in self._filter(lines):
LOGGER.debug(line)
_LOGGER.debug(line)
if self._logfile is not None:
self._logfile.write(line + "\n")

Expand Down Expand Up @@ -175,7 +176,7 @@ def wait_for(self, pattern: str, timeout: float = _TIMEOUT) -> str | None:
if re.search(pattern, line):
return self._join(lines)
time.sleep(0.1)
LOGGER.warning(f"Waiting for '{pattern}' timed out after {timeout:.1f}s!")
_LOGGER.warning(f"Waiting for '{pattern}' timed out after {timeout:.1f}s!")
return None

def wait_for_prompt(self, timeout: float = _TIMEOUT) -> list[str]:
Expand All @@ -191,7 +192,7 @@ def wait_for_prompt(self, timeout: float = _TIMEOUT) -> list[str]:
if prompts := self._read_packets(Nsh._NEWLINE + Nsh._PROMPT, timeout):
prompt = Nsh._PROMPT + Nsh._PROMPT.join(prompts)
return self._join(self._filter(prompt.split(Nsh._NEWLINE)))
LOGGER.warning(f"Waiting for 'nsh> ' prompt timed out after {timeout:.1f}s!")
_LOGGER.warning(f"Waiting for 'nsh> ' prompt timed out after {timeout:.1f}s!")
return None


Expand Down Expand Up @@ -271,7 +272,7 @@ def nsh(serial_or_port: str, baudrate: int = 57600):
else:
ttyDevice = find_serial_port(serial_or_port).device
try:
LOGGER.info(f"Starting on port '{serial_or_port}'..."
_LOGGER.info(f"Starting on port '{serial_or_port}'..."
if serial_or_port else "Starting...")
device = Serial(ttyDevice, baudrate=baudrate)
reader_thread = ReaderThread(device, lambda: _NshReader(device))
Expand All @@ -280,13 +281,13 @@ def nsh(serial_or_port: str, baudrate: int = 57600):
yield nsh
finally:
if nsh is not None: nsh.log_to_file(None)
LOGGER.debug("Stopping.")
_LOGGER.debug("Stopping.")


# -----------------------------------------------------------------------------
# We need to monkey patch the ReaderThread.run() function to prevent a
# "device not ready" error to abort the reader thread.
def patched_run(self):
def _patched_run(self):
from serial import SerialException
self.serial.timeout = 0.1
self.protocol = self.protocol_factory()
Expand All @@ -304,7 +305,7 @@ def patched_run(self):
data = self.serial.read(self.serial.in_waiting or 1)
except SerialException as e:
if self.alive and "readiness" in str(e):
# LOGGER.debug(e)
# _LOGGER.debug(e)
continue
error = e
break
Expand All @@ -319,4 +320,4 @@ def patched_run(self):
self.protocol.connection_lost(error)
self.protocol = None

ReaderThread.run = patched_run
ReaderThread.run = _patched_run

0 comments on commit a5bafe8

Please sign in to comment.