From 84cf0da0ade4930ef81f1126f5af6182984d5172 Mon Sep 17 00:00:00 2001 From: Matthew Messinger Date: Thu, 11 Jan 2024 10:56:28 -0500 Subject: [PATCH] Completely replace pkg_resources with importlib.resources --- scrapli/helper.py | 11 ++++------- tests/unit/test_helper.py | 4 ++-- 2 files changed, 6 insertions(+), 9 deletions(-) diff --git a/scrapli/helper.py b/scrapli/helper.py index 06eeb776..1465b773 100644 --- a/scrapli/helper.py +++ b/scrapli/helper.py @@ -1,5 +1,6 @@ """scrapli.helper""" import importlib +import importlib.resources import sys import urllib.request from io import BytesIO, TextIOWrapper @@ -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]: diff --git a/tests/unit/test_helper.py b/tests/unit/test_helper.py index 7e0fbdac..7ad51d85 100644 --- a/tests/unit/test_helper.py +++ b/tests/unit/test_helper.py @@ -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, @@ -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"