Switch from pkg_resources to importlib.resources to obtain resources
authorKarl O. Pinc <kop@karlpinc.com>
Wed, 20 Mar 2024 17:52:33 +0000 (12:52 -0500)
committerKarl O. Pinc <kop@karlpinc.com>
Wed, 20 Mar 2024 17:52:33 +0000 (12:52 -0500)
setup.py
src/pgwui_develop/pgwui.py
tests/test_pgwui.py

index fecd198d54159cb83d1d35855e0a0d499ea95468..f54b6741bd8c31a84d27dc548c23a7bc8e3984d5 100644 (file)
--- 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',
index 081825be9ee2c34eddf2aa30723d68d8bd6b0406..94e0aa9a66f2a649d55ba94ff6a080a17bc51de4 100644 (file)
@@ -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.
 #
 
 # Karl O. Pinc <kop@karlpinc.com>
 
-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):
index b4f2edd1925f02a1ef74f4cd7d31c0ef1f8a6f4e..b93dd5f35d29beb91d74bf844dc6fc1ccd40ff1e 100644 (file)
@@ -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(