Skip to content

Commit 1095f66

Browse files
committed
Merging pull request 142
Signed-off-by: Lukáš Doktor <[email protected]> * github.com:autotest/aexpect: Removing escape sequence from output in cmd_output function!
2 parents 6448665 + e240426 commit 1095f66

File tree

1 file changed

+17
-5
lines changed

1 file changed

+17
-5
lines changed

aexpect/client.py

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1188,7 +1188,7 @@ def read_up_to_prompt(self, timeout=60.0, internal_timeout=None,
11881188
print_func)[1]
11891189

11901190
def cmd_output(self, cmd, timeout=60, internal_timeout=None,
1191-
print_func=None, safe=False):
1191+
print_func=None, safe=False, strip_console_codes=False):
11921192
"""
11931193
Send a command and return its output.
11941194
@@ -1203,15 +1203,18 @@ def cmd_output(self, cmd, timeout=60, internal_timeout=None,
12031203
error messages that make read_up_to_prompt to timeout. Let's
12041204
try to be a little more robust and send a carriage return, to
12051205
see if we can get to the prompt when safe=True.
1206-
1206+
:param strip_console_codes: Whether to remove the escape sequence from the output.
1207+
In serial sessions, there are escape sequences present. If it is not
1208+
expected while reading the output remove it by passing
1209+
serial_console_codes = True.
12071210
:return: The output of cmd
12081211
:raise ShellTimeoutError: Raised if timeout expires
12091212
:raise ShellProcessTerminatedError: Raised if the shell process
12101213
terminates while waiting for output
12111214
:raise ShellError: Raised if an unknown error occurs
12121215
"""
12131216
if safe:
1214-
return self.cmd_output_safe(cmd, timeout)
1217+
return self.cmd_output_safe(cmd, timeout, strip_console_codes)
12151218
session_tag = f"[{self.output_prefix}] " if self.output_prefix else ""
12161219
LOG.debug("%sSending command: %s", session_tag, cmd)
12171220
self.read_nonblocking(0, timeout)
@@ -1229,10 +1232,13 @@ def cmd_output(self, cmd, timeout=60, internal_timeout=None,
12291232
raise ShellError(cmd, output) from error
12301233

12311234
# Remove the echoed command and the final shell prompt
1235+
if strip_console_codes:
1236+
# Removing the escape sequence
1237+
out = astring.strip_console_codes(out)
12321238
return self.remove_last_nonempty_line(self.remove_command_echo(out,
12331239
cmd))
12341240

1235-
def cmd_output_safe(self, cmd, timeout=60):
1241+
def cmd_output_safe(self, cmd, timeout=60, strip_console_codes=False):
12361242
"""
12371243
Send a command and return its output (serial sessions).
12381244
@@ -1244,7 +1250,10 @@ def cmd_output_safe(self, cmd, timeout=60):
12441250
:param cmd: Command to send (must not contain newline characters)
12451251
:param timeout: The duration (in seconds) to wait for the prompt to
12461252
return
1247-
1253+
:param strip_console_codes: Whether to remove the escape sequence from the output.
1254+
In serial sessions, there are escape sequences present. If it is not
1255+
expected while reading the output remove it by passing
1256+
serial_console_codes = True.
12481257
:return: The output of cmd
12491258
:raise ShellTimeoutError: Raised if timeout expires
12501259
:raise ShellProcessTerminatedError: Raised if the shell process
@@ -1278,6 +1287,9 @@ def cmd_output_safe(self, cmd, timeout=60):
12781287
raise ShellTimeoutError(cmd, out)
12791288

12801289
# Remove the echoed command and the final shell prompt
1290+
if strip_console_codes:
1291+
# Removing the escape sequence
1292+
out = astring.strip_console_codes(out)
12811293
return self.remove_last_nonempty_line(self.remove_command_echo(out,
12821294
cmd))
12831295

0 commit comments

Comments
 (0)