Skip to content

Commit

Permalink
refactor: unset onlcr in system transport
Browse files Browse the repository at this point in the history
  • Loading branch information
carlmontanari committed May 4, 2024
1 parent 26b68c8 commit 207ace7
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 2 deletions.
36 changes: 36 additions & 0 deletions scrapli/transport/plugins/system/ptyprocess.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
from typing import List, Optional, Type, TypeVar

from scrapli.exceptions import ScrapliValueError
from scrapli.helper import user_warning


class PtyProcessError(Exception):
Expand Down Expand Up @@ -176,6 +177,35 @@ def _setecho(fd: int, state: bool) -> None:
raise


def _setonlcr(fd: int, state: bool) -> None:
import termios

try:
attr = termios.tcgetattr(fd)
except termios.error as err:
if err.args[0] == errno.EINVAL:
raise OSError(err.args[0], "{}: {}.".format(err.args[1], errmsg))
raise

if state:
attr[1] = attr[1] | termios.ONLCR
else:
attr[1] = attr[1] & ~termios.ONLCR

try:
termios.tcsetattr(fd, termios.TCSANOW, attr)
except OSError as err:
if err.args[0] == errno.EINVAL:
title = "Set ONLCR!"
message = (
"_setonlcr() failed -- if you encounter this error please open an issue! unless you "
"are seeing this when using scrapli_netconf you can *probably* ignore this though!"
)

user_warning(title=title, message=message)
raise


class PtyProcess:
def __init__(self, pid: int, fd: int) -> None:
"""
Expand Down Expand Up @@ -355,6 +385,12 @@ def spawn(
if err.args[0] not in (errno.EINVAL, errno.ENOTTY, errno.ENXIO):
raise

# attrs = termios.tcgetattr(fd)
# attrs[1] &= ~termios.ONLCR
# termios.tcsetattr(fd, termios.TCSANOW, attrs)

_setonlcr(fd, True)

return inst

def __repr__(self) -> str:
Expand Down
9 changes: 7 additions & 2 deletions tests/unit/test_helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -267,8 +267,13 @@ def test_format_user_warning():
def test_format_user_warning_really_long_title():
terminal_width = get_terminal_size().columns

warning_string = format_user_warning(title=("blah" * 30), message="something")
assert warning_string.lstrip().startswith("*" * terminal_width)
title = "z" * (terminal_width - 5)

warning_string = format_user_warning(title=title, message="something")

assert warning_string.strip().startswith("*")
assert warning_string.strip().endswith("*")
assert title in warning_string


def test_user_warning():
Expand Down

0 comments on commit 207ace7

Please sign in to comment.