Skip to content
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

Removing escape sequence from output in cmd_output function! #142

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
32 changes: 26 additions & 6 deletions aexpect/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -259,7 +259,8 @@ def _get_aexpect_helper(self, helper_cmd, pass_fds, echo, command):
full_output += output
sub_status = sub.poll()
if sub_status is not None:
raise ExpectProcessTerminatedError(pattern, sub_status, full_output)
raise ExpectProcessTerminatedError(
pattern, sub_status, full_output)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Was this change intentional? I think this is incompatible with black linting and doesn't relate to the topic of this pull request.

else:
raise ExpectTimeoutError(pattern, full_output)
return sub
Expand Down Expand Up @@ -1188,7 +1189,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 +1204,19 @@ 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 remove the escape sequence from the output
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Whether to remove the ... followed by a full stop.

In serial session there are escape sequences present,if it is not
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In serial sessions.. with a space after the comma.

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

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This empty line is not needed

"""
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 @@ -1223,16 +1228,22 @@ def cmd_output(self, cmd, timeout=60, internal_timeout=None,
raise ShellTimeoutError(cmd, output) from error
except ExpectProcessTerminatedError as error:
output = self.remove_command_echo(error.output, cmd)
raise ShellProcessTerminatedError(cmd, error.status, output) from error
raise ShellProcessTerminatedError(
cmd, error.status, output) from error
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Similar statement to the "black" statement above

except ExpectError as error:
output = self.remove_command_echo(error.output, cmd)
raise ShellError(cmd, output) from error

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

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How about a much simpler

out = astring.strip_console_codes(out) if strip_console_codes else 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,6 +1255,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 remove the escape sequence from the output
In serial session there are escape sequences present,if it is not
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same adaptations would be needed here.

expected while reading the output remove it by passing
serial_console_codes = True

:return: The output of cmd
:raise ShellTimeoutError: Raised if timeout expires
Expand Down Expand Up @@ -1278,6 +1293,11 @@ 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
output_no_escape = astring.strip_console_codes(out)
return self.remove_last_nonempty_line(self.remove_command_echo(output_no_escape,
cmd))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Similar simplification to above

return self.remove_last_nonempty_line(self.remove_command_echo(out,
cmd))

Expand Down
Loading