-# 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):
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):
# 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')
@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(