From e153f0ab4200b217ab19bd925335f22818a03b47 Mon Sep 17 00:00:00 2001 From: "Karl O. Pinc" Date: Wed, 20 Mar 2024 12:52:33 -0500 Subject: [PATCH] Switch from pkg_resources to importlib.resources to obtain resources --- setup.py | 1 + src/pgwui_develop/pgwui.py | 28 +++++++++++++++++----------- tests/test_pgwui.py | 27 +++++++++------------------ 3 files changed, 27 insertions(+), 29 deletions(-) diff --git a/setup.py b/setup.py index fecd198..f54b674 100644 --- a/setup.py +++ b/setup.py @@ -132,6 +132,7 @@ setup( # Run-time dependencies. install_requires=[ 'click', + 'importlib_resources >= 5.10 ; python_version < "3.12"', 'mako', 'setuptools', # for pkg_resources module 'pyramid', diff --git a/src/pgwui_develop/pgwui.py b/src/pgwui_develop/pgwui.py index 081825b..94e0aa9 100644 --- a/src/pgwui_develop/pgwui.py +++ b/src/pgwui_develop/pgwui.py @@ -1,4 +1,5 @@ -# Copyright (C) 2020, 2021 The Meme Factory, Inc. http://www.karlpinc.com/ +# Copyright (C) 2020, 2021, 2024 The Meme Factory, Inc. +# http://www.karlpinc.com/ # This file is part of PGWUI_Develop. # @@ -20,14 +21,22 @@ # Karl O. Pinc -import click -import mako.exceptions -import mako.template import os import pathlib -import pkg_resources import sys -import tempfile + +if sys.version_info < (3, 12): + import importlib_resources + + class importlib: + pass + setattr(importlib, 'resources', importlib_resources) +else: + import importlib.resources + +import click +import mako.exceptions +import mako.template def validate_target(target): @@ -102,11 +111,8 @@ def traverse_templates(target, settings, template_path): def deliver_target(target, settings): - with tempfile.TemporaryDirectory() as tmpdir: - pkg_resources.set_extraction_path(tmpdir) - template_path = pkg_resources.resource_filename(__name__, 'TEMPLATE') - traverse_templates(target, settings, template_path) - pkg_resources.cleanup_resources() + develop_top_dir = importlib.resources.files() + traverse_templates(target, settings, develop_top_dir / 'TEMPLATE') def path_or_default(path): diff --git a/tests/test_pgwui.py b/tests/test_pgwui.py index b4f2edd..b93dd5f 100644 --- a/tests/test_pgwui.py +++ b/tests/test_pgwui.py @@ -32,16 +32,12 @@ import pgwui_develop.pgwui as pgwui # Mock fixtures for module level objects MockTemplate = testing.make_magicmock_fixture( pgwui.mako.template, 'Template') +# Late binding because, depending on the python version, importlib.resources +# may be a class or a module. And, in any case, it may not be "complete" +# enough to mock so just set the attribute with the late binding. +mock_importlib_resources_files = testing.late_instance_mock_fixture('files') mock_text_error_template = testing.make_magicmock_fixture( pgwui.mako.exceptions, 'text_error_template') -MockTemporaryDirectory = testing.make_magicmock_fixture( - pgwui.tempfile, 'TemporaryDirectory') -mock_set_extraction_path = testing.function_mock_fixture( - pgwui.pkg_resources.set_extraction_path) -mock_resource_filename = testing.function_mock_fixture( - pgwui.pkg_resources.resource_filename) -mock_cleanup_resources = testing.function_mock_fixture( - pgwui.pkg_resources.cleanup_resources) mock_scandir = testing.make_magicmock_fixture( pgwui.os, 'scandir') @@ -372,22 +368,17 @@ mock_traverse_templates = testing.make_mock_fixture( @pytest.mark.unittest def test_deliver_target( - MockTemporaryDirectory, mock_set_extraction_path, - mock_resource_filename, mock_traverse_templates, - mock_cleanup_resources): + mock_importlib_resources_files, mock_traverse_templates): '''All the mocks are called ''' - mocked_set_extraction_path = mock_set_extraction_path() - mocked_resource_filename = mock_resource_filename() - mocked_cleanup_resources = mock_cleanup_resources() + mocked_importlib_resources_files = mock_importlib_resources_files( + pgwui.importlib.resources) + mocked_importlib_resources_files.return_value = pathlib.Path('sample/path') pgwui.deliver_target(None, None) - assert MockTemporaryDirectory.call_count - assert mocked_set_extraction_path.call_count - assert mocked_resource_filename.call_count + assert mocked_importlib_resources_files.call_count assert mock_traverse_templates.call_count - assert mocked_cleanup_resources.call_count mock_deliver_target = testing.make_mock_fixture( -- 2.34.1