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

refactor: override core scrapli reads in async/sync channels #144

Merged
merged 1 commit into from
May 4, 2024
Merged
Show file tree
Hide file tree
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
25 changes: 21 additions & 4 deletions scrapli_netconf/channel/async_channel.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,10 +98,17 @@ async def _send_client_capabilities(

async def read(self) -> bytes:
"""
Read chunks of output from the channel
Read chunks of output from the channels

Prior to super-ing "normal" scrapli read, check if there is anything on our read_buf, if
there is, return that first
Prior to doing "normal" scrapli read things (*), check if there is anything on our read_buf,
if there is, return that first. Historically this actually did do "normal" scrapli read
things by super-ing read, but, "normal" scrapli replaces \r it finds in output which can
cause nc1.1 chunk parsing to fail since we've removed some chars that were counted as part
of the chunk. so, now we just copypasta the normal read things here without that .replace,
and no longer super.

Note that to *not* have the \r in output from system transport you will need >=2024.07.30
scrapli version, but replacing the super-ing with this sorts out other transports!

Args:
N/A
Expand All @@ -118,7 +125,17 @@ async def read(self) -> bytes:
self._read_buf = b""
return read_buf

return await super().read()
buf = await self.transport.read()

self.logger.debug(f"read: {buf!r}")

if self.channel_log:
self.channel_log.write(buf)

if b"\x1b" in buf.lower():
buf = self._strip_ansi(buf=buf)

return buf

async def _read_until_input(self, channel_input: bytes) -> bytes:
"""
Expand Down
23 changes: 20 additions & 3 deletions scrapli_netconf/channel/sync_channel.py
Original file line number Diff line number Diff line change
Expand Up @@ -200,8 +200,15 @@ def read(self) -> bytes:
"""
Read chunks of output from the channel

Prior to super-ing "normal" scrapli read, check if there is anything on our read_buf, if
there is, return that first
Prior to doing "normal" scrapli read things (*), check if there is anything on our read_buf,
if there is, return that first. Historically this actually did do "normal" scrapli read
things by super-ing read, but, "normal" scrapli replaces \r it finds in output which can
cause nc1.1 chunk parsing to fail since we've removed some chars that were counted as part
of the chunk. so, now we just copypasta the normal read things here without that .replace,
and no longer super.

Note that to *not* have the \r in output from system transport you will need >=2024.07.30
scrapli version, but replacing the super-ing with this sorts out other transports!

Args:
N/A
Expand All @@ -218,7 +225,17 @@ def read(self) -> bytes:
self._read_buf = b""
return read_buf

return super().read()
buf = self.transport.read()

self.logger.debug(f"read: {buf!r}")

if self.channel_log:
self.channel_log.write(buf)

if b"\x1b" in buf.lower():
buf = self._strip_ansi(buf=buf)

return buf

def _read_until_input(self, channel_input: bytes) -> bytes:
"""
Expand Down
Loading