@@ -22,7 +22,9 @@ class Shell:
22
22
Helper class that provides methods used to interact with shell application.
23
23
"""
24
24
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 :
26
28
self ._device : DeviceAdapter = device
27
29
self .prompt : str = prompt
28
30
self .base_timeout : float = timeout or device .base_timeout
@@ -48,7 +50,9 @@ def wait_for_prompt(self, timeout: float | None = None) -> bool:
48
50
return True
49
51
return False
50
52
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 ]:
52
56
"""
53
57
Send shell command to a device and return response. Passed command
54
58
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:
63
67
self ._device .write (command_ext .encode ())
64
68
lines : list [str ] = []
65
69
# 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
+ )
67
75
# 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
+ )
69
81
return lines
70
82
71
83
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
+ """
72
104
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>' ])
80
106
)
81
107
return list (filter (lambda l : not regex_filter .search (l ), command_lines ))
82
108
@@ -106,6 +132,7 @@ class ShellMCUbootCommandParsed:
106
132
"""
107
133
Helper class to keep data from `mcuboot` shell command.
108
134
"""
135
+
109
136
areas : list [ShellMCUbootArea ] = field (default_factory = list )
110
137
111
138
@classmethod
0 commit comments