Skip to content

Removing escape sequence from output in cmd_output function! #142

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 10, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 17 additions & 5 deletions aexpect/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -1188,7 +1188,7 @@ def read_up_to_prompt(self, timeout=60.0, internal_timeout=None,
print_func)[1]

def cmd_output(self, cmd, timeout=60, internal_timeout=None,
print_func=None, safe=False):
print_func=None, safe=False, strip_console_codes=False):
"""
Send a command and return its output.

Expand All @@ -1203,15 +1203,18 @@ def cmd_output(self, cmd, timeout=60, internal_timeout=None,
error messages that make read_up_to_prompt to timeout. Let's
try to be a little more robust and send a carriage return, to
see if we can get to the prompt when safe=True.

:param strip_console_codes: Whether to remove the escape sequence from the output.
In serial sessions, there are escape sequences present. If it is not
expected while reading the output remove it by passing
serial_console_codes = True.
:return: The output of cmd
:raise ShellTimeoutError: Raised if timeout expires
:raise ShellProcessTerminatedError: Raised if the shell process
terminates while waiting for output
:raise ShellError: Raised if an unknown error occurs
"""
if safe:
return self.cmd_output_safe(cmd, timeout)
return self.cmd_output_safe(cmd, timeout, strip_console_codes)
session_tag = f"[{self.output_prefix}] " if self.output_prefix else ""
LOG.debug("%sSending command: %s", session_tag, cmd)
self.read_nonblocking(0, timeout)
Expand All @@ -1229,10 +1232,13 @@ def cmd_output(self, cmd, timeout=60, internal_timeout=None,
raise ShellError(cmd, output) from error

# Remove the echoed command and the final shell prompt
if strip_console_codes:
# Removing the escape sequence
out = astring.strip_console_codes(out)
return self.remove_last_nonempty_line(self.remove_command_echo(out,
cmd))

def cmd_output_safe(self, cmd, timeout=60):
def cmd_output_safe(self, cmd, timeout=60, strip_console_codes=False):
"""
Send a command and return its output (serial sessions).

Expand All @@ -1244,7 +1250,10 @@ def cmd_output_safe(self, cmd, timeout=60):
:param cmd: Command to send (must not contain newline characters)
:param timeout: The duration (in seconds) to wait for the prompt to
return

:param strip_console_codes: Whether to remove the escape sequence from the output.
In serial sessions, there are escape sequences present. If it is not
expected while reading the output remove it by passing
serial_console_codes = True.
:return: The output of cmd
:raise ShellTimeoutError: Raised if timeout expires
:raise ShellProcessTerminatedError: Raised if the shell process
Expand Down Expand Up @@ -1278,6 +1287,9 @@ def cmd_output_safe(self, cmd, timeout=60):
raise ShellTimeoutError(cmd, out)

# Remove the echoed command and the final shell prompt
if strip_console_codes:
# Removing the escape sequence
out = astring.strip_console_codes(out)
return self.remove_last_nonempty_line(self.remove_command_echo(out,
cmd))

Expand Down
Loading