Skip to content

Commit

Permalink
Merge pull request #311 from killjoy1221/refactor/importlibresources
Browse files Browse the repository at this point in the history
Completely replace pkg_resources with importlib.resources
  • Loading branch information
carlmontanari authored Jan 11, 2024
2 parents bbb145a + 84cf0da commit e02100d
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 9 deletions.
11 changes: 4 additions & 7 deletions scrapli/helper.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
"""scrapli.helper"""
import importlib
import importlib.resources
import sys
import urllib.request
from io import BytesIO, TextIOWrapper
Expand All @@ -12,17 +13,13 @@
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 f"{importlib.resources.files('ntc_templates')}/templates"

return importlib_resources.resource_filename("ntc_templates", "templates")
with importlib.resources.path("ntc_templates", "templates") as path:
return str(path)


def _textfsm_get_template(platform: str, command: str) -> Optional[TextIO]:
Expand Down
4 changes: 2 additions & 2 deletions tests/unit/test_helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@
from pathlib import Path
from shutil import get_terminal_size

import pkg_resources # pylint: disable=C041
import pytest

from scrapli.exceptions import ScrapliValueError
from scrapli.helper import (
_textfsm_get_template,
_textfsm_get_template_directory,
format_user_warning,
genie_parse,
resolve_file,
Expand All @@ -26,7 +26,7 @@

def test_textfsm_get_template():
template = _textfsm_get_template("cisco_nxos", "show ip arp")
template_dir = pkg_resources.resource_filename("ntc_templates", "templates")
template_dir = _textfsm_get_template_directory()
assert isinstance(template, TextIOWrapper)
assert template.name == f"{template_dir}/cisco_nxos_show_ip_arp.textfsm"

Expand Down

0 comments on commit e02100d

Please sign in to comment.