Skip to content

Commit

Permalink
Merge pull request #144 from scrapli/fix/override-core-channel-reads
Browse files Browse the repository at this point in the history
refactor: override core scrapli reads in async/sync channels
  • Loading branch information
carlmontanari authored May 4, 2024
2 parents 361382e + ce201d8 commit da02d1d
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 7 deletions.
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

0 comments on commit da02d1d

Please sign in to comment.