Skip to content

Commit

Permalink
Merge pull request #308 from carlmontanari/refactor/housekeeping
Browse files Browse the repository at this point in the history
refactor: dont use deprecated pkg_resources on >=3.9, some pyupgrade …
  • Loading branch information
carlmontanari authored Nov 7, 2023
2 parents 4ae027a + d4f8761 commit 71dfb2c
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 19 deletions.
2 changes: 1 addition & 1 deletion scrapli/channel/async_channel.py
Original file line number Diff line number Diff line change
Expand Up @@ -241,7 +241,7 @@ async def _read_until_prompt_or_time(

if (time.time() - start) > read_duration:
break
if any((channel_output in search_buf for channel_output in channel_outputs)):
if any(channel_output in search_buf for channel_output in channel_outputs):
break
if re.search(pattern=regex_channel_outputs_pattern, string=search_buf):
break
Expand Down
2 changes: 1 addition & 1 deletion scrapli/channel/sync_channel.py
Original file line number Diff line number Diff line change
Expand Up @@ -238,7 +238,7 @@ def _read_until_prompt_or_time(

if (time.time() - start) > read_duration:
break
if any((channel_output in search_buf for channel_output in channel_outputs)):
if any(channel_output in search_buf for channel_output in channel_outputs):
break
if re.search(pattern=regex_channel_outputs_pattern, string=search_buf):
break
Expand Down
2 changes: 1 addition & 1 deletion scrapli/driver/generic/base_driver.py
Original file line number Diff line number Diff line change
Expand Up @@ -337,7 +337,7 @@ def _pre_send_from_file(file: str, caller: str) -> List[str]:
raise ScrapliTypeError(f"`{caller}` expects a string path to a file, got {type(file)}")
resolved_file = resolve_file(file)

with open(resolved_file, "r", encoding="utf-8") as f:
with open(resolved_file, encoding="utf-8") as f:
commands = f.read().splitlines()

return commands
Expand Down
19 changes: 16 additions & 3 deletions scrapli/helper.py
Original file line number Diff line number Diff line change
@@ -1,18 +1,29 @@
"""scrapli.helper"""
import importlib
import sys
import urllib.request
from io import BytesIO, TextIOWrapper
from pathlib import Path
from shutil import get_terminal_size
from typing import Any, Dict, List, Optional, TextIO, Union
from warnings import warn

import pkg_resources

from scrapli.exceptions import ScrapliValueError
from scrapli.logging import logger
from scrapli.settings import Settings

if sys.version_info >= (3, 9):
import importlib.resources as importlib_resources
else:
import pkg_resources as importlib_resources


def _textfsm_get_template_directory() -> str:
if sys.version_info >= (3, 9):
return f"{importlib_resources.files('ntc_templates')}/templates"

return importlib_resources.resource_filename("ntc_templates", "templates")


def _textfsm_get_template(platform: str, command: str) -> Optional[TextIO]:
"""
Expand Down Expand Up @@ -43,7 +54,9 @@ def _textfsm_get_template(platform: str, command: str) -> Optional[TextIO]:
)
user_warning(title=title, message=message)
return None
template_dir = pkg_resources.resource_filename("ntc_templates", "templates")

template_dir = _textfsm_get_template_directory()

cli_table = CliTable("index", template_dir)
template_index = cli_table.index.GetRowMatch({"Platform": platform, "Command": command})
if not template_index:
Expand Down
26 changes: 13 additions & 13 deletions scrapli/transport/plugins/system/ptyprocess.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ def _make_eof_intr() -> None:
raise ValueError("No stream has a fileno")
intr = ord(termios.tcgetattr(fd)[6][VINTR])
eof = ord(termios.tcgetattr(fd)[6][VEOF])
except (ImportError, OSError, IOError, ValueError, termios.error):
except (ImportError, OSError, ValueError, termios.error):
# unless the controlling process is also not a terminal,
# such as cron(1), or when stdin and stdout are both closed.
# Fall-back to using CEOF and CINTR. There
Expand Down Expand Up @@ -141,9 +141,9 @@ def _setecho(fd: int, state: bool) -> None:
None
Raises:
IOError: if termios raises an exception getting the fd
OSError: if termios raises an exception getting the fd or raises an exception setting the
echo state on the fd
termios.error: also if termios rasies an exception gettign fd... unclear why the two errors!
IOError: if termios raises an exception setting the echo state on the fd
"""
import termios
Expand All @@ -157,7 +157,7 @@ def _setecho(fd: int, state: bool) -> None:
attr = termios.tcgetattr(fd)
except termios.error as err:
if err.args[0] == errno.EINVAL:
raise IOError(err.args[0], "%s: %s." % (err.args[1], errmsg))
raise OSError(err.args[0], "{}: {}.".format(err.args[1], errmsg))
raise

if state:
Expand All @@ -169,9 +169,9 @@ def _setecho(fd: int, state: bool) -> None:
# I tried TCSADRAIN and TCSAFLUSH, but these were inconsistent and
# blocked on some platforms. TCSADRAIN would probably be ideal.
termios.tcsetattr(fd, termios.TCSANOW, attr)
except IOError as err:
except OSError as err:
if err.args[0] == errno.EINVAL:
raise IOError(err.args[0], "%s: %s." % (err.args[1], errmsg))
raise OSError(err.args[0], "{}: {}.".format(err.args[1], errmsg))
raise


Expand All @@ -196,8 +196,8 @@ def __init__(self, pid: int, fd: int) -> None:
_make_eof_intr() # Ensure _EOF and _INTR are calculated
self.pid = pid
self.fd = fd
readf = io.open(fd, "rb", buffering=0)
writef = io.open(fd, "wb", buffering=0, closefd=False)
readf = open(fd, "rb", buffering=0)
writef = open(fd, "wb", buffering=0, closefd=False)
self.fileobj = io.BufferedRWPair(readf, writef) # type: ignore

self.terminated = False
Expand Down Expand Up @@ -244,7 +244,7 @@ def spawn(
ScrapliValueError: if no ssh binary found on PATH
Exception: IOError - if unable to set window size of child process
Exception: OSError - if unable to spawn command in child process
IOError: failing to reset window size
OSError: failing to reset window size
exception: if we get an exception decoding output
"""
Expand Down Expand Up @@ -283,15 +283,15 @@ def spawn(
if pid == CHILD:
try:
_setwinsize(fd=STDIN_FILENO, rows=rows, cols=cols)
except IOError as err:
except OSError as err:
if err.args[0] not in (errno.EINVAL, errno.ENOTTY):
raise

# disable echo if requested
if echo is False:
try:
_setecho(STDIN_FILENO, False)
except (IOError, termios.error) as err:
except (OSError, termios.error) as err:
if err.args[0] not in (errno.EINVAL, errno.ENOTTY):
raise

Expand Down Expand Up @@ -350,7 +350,7 @@ def spawn(

try:
inst.setwinsize(rows=rows, cols=cols)
except IOError as err:
except OSError as err:
if err.args[0] not in (errno.EINVAL, errno.ENOTTY, errno.ENXIO):
raise

Expand Down Expand Up @@ -481,7 +481,7 @@ def read(self, size: int = 1024) -> bytes:
"""
try:
s = self.fileobj.read1(size)
except (OSError, IOError) as err:
except OSError as err:
if err.args[0] == errno.EIO:
# Linux-style EOF
self.flag_eof = True
Expand Down

0 comments on commit 71dfb2c

Please sign in to comment.