Skip to content

Commit 20409ca

Browse files
tguenthnashif
authored andcommitted
doc: add get_filtered_output method documentation
Add documentation for the get_filtered_output method in both the Python implementation and the Integration with pytest chapter. The method is used to filter command outputs by removing prompts and log messages, making it easier to process shell command results. Signed-off-by: Thomas Günther <[email protected]> removed traling whitespace
1 parent 9e908b1 commit 20409ca

File tree

2 files changed

+40
-11
lines changed
  • doc/develop/test
  • scripts/pylib/pytest-twister-harness/src/twister_harness/helpers

2 files changed

+40
-11
lines changed

doc/develop/test/pytest.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -219,6 +219,8 @@ Shell
219219

220220
.. automethod:: wait_for_prompt
221221

222+
.. automethod:: get_filtered_output
223+
222224

223225
Examples of pytest tests in the Zephyr project
224226
**********************************************

scripts/pylib/pytest-twister-harness/src/twister_harness/helpers/shell.py

Lines changed: 38 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,9 @@ class Shell:
2222
Helper class that provides methods used to interact with shell application.
2323
"""
2424

25-
def __init__(self, device: DeviceAdapter, prompt: str = 'uart:~$', timeout: float | None = None) -> None:
25+
def __init__(
26+
self, device: DeviceAdapter, prompt: str = 'uart:~$', timeout: float | None = None
27+
) -> None:
2628
self._device: DeviceAdapter = device
2729
self.prompt: str = prompt
2830
self.base_timeout: float = timeout or device.base_timeout
@@ -48,7 +50,9 @@ def wait_for_prompt(self, timeout: float | None = None) -> bool:
4850
return True
4951
return False
5052

51-
def exec_command(self, command: str, timeout: float | None = None, print_output: bool = True) -> list[str]:
53+
def exec_command(
54+
self, command: str, timeout: float | None = None, print_output: bool = True
55+
) -> list[str]:
5256
"""
5357
Send shell command to a device and return response. Passed command
5458
is extended by double enter sings - first one to execute this command
@@ -63,20 +67,42 @@ def exec_command(self, command: str, timeout: float | None = None, print_output:
6367
self._device.write(command_ext.encode())
6468
lines: list[str] = []
6569
# wait for device command print - it should be done immediately after sending command to device
66-
lines.extend(self._device.readlines_until(regex=regex_command, timeout=1.0, print_output=print_output))
70+
lines.extend(
71+
self._device.readlines_until(
72+
regex=regex_command, timeout=1.0, print_output=print_output
73+
)
74+
)
6775
# wait for device command execution
68-
lines.extend(self._device.readlines_until(regex=regex_prompt, timeout=timeout, print_output=print_output))
76+
lines.extend(
77+
self._device.readlines_until(
78+
regex=regex_prompt, timeout=timeout, print_output=print_output
79+
)
80+
)
6981
return lines
7082

7183
def get_filtered_output(self, command_lines: list[str]) -> list[str]:
84+
"""
85+
Filter out prompts and log messages
86+
87+
Take the output of exec_command, which can contain log messages and command prompts,
88+
and filter them to obtain only the command output.
89+
90+
Example:
91+
>>> # equivalent to `lines = shell.exec_command("kernel version")`
92+
>>> lines = [
93+
>>> 'uart:~$', # filter prompts
94+
>>> 'Zephyr version 3.6.0', # keep this line
95+
>>> 'uart:~$ <dbg> debug message' # filter log messages
96+
>>> ]
97+
>>> filtered_output = shell.get_filtered_output(output)
98+
>>> filtered_output
99+
['Zephyr version 3.6.0']
100+
101+
:param command_lines: List of strings i.e. the output of `exec_command`.
102+
:return: A list of strings containing, excluding prompts and log messages.
103+
"""
72104
regex_filter = re.compile(
73-
'|'.join([
74-
re.escape(self.prompt),
75-
'<dbg>',
76-
'<inf>',
77-
'<wrn>',
78-
'<err>'
79-
])
105+
'|'.join([re.escape(self.prompt), '<dbg>', '<inf>', '<wrn>', '<err>'])
80106
)
81107
return list(filter(lambda l: not regex_filter.search(l), command_lines))
82108

@@ -106,6 +132,7 @@ class ShellMCUbootCommandParsed:
106132
"""
107133
Helper class to keep data from `mcuboot` shell command.
108134
"""
135+
109136
areas: list[ShellMCUbootArea] = field(default_factory=list)
110137

111138
@classmethod

0 commit comments

Comments
 (0)