diff --git a/flask_seeder/generator.py b/flask_seeder/generator.py index 6a94c62..61b0bc1 100644 --- a/flask_seeder/generator.py +++ b/flask_seeder/generator.py @@ -1,12 +1,17 @@ """ Generators module """ -import uuid import random +import sys +import uuid from ipaddress import IPv4Address, IPv6Address -import pkg_resources from flask_seeder.parser import SGParser, Tokenizer +if sys.version_info >= (3, 9): + import importlib.resources as importlib_resources +else: + import importlib_resources + def resource_path(path): """ Get the resource path @@ -17,7 +22,7 @@ def resource_path(path): Returns the full filesystem path to the resource. Note that no validation is made to ensure the resource actually exist. """ - return pkg_resources.resource_filename("flask_seeder", "data/" + path) + return importlib_resources.files("flask_seeder") / ("data/" + path) def read_resource(path): """ Read resource text file @@ -31,8 +36,9 @@ def read_resource(path): A list with the file contents. """ lines = [] - with open(resource_path(path)) as source: - lines = source.read().splitlines() + with importlib_resources.as_file(resource_path(path)) as ref: + with open(ref) as source: + lines = source.read().splitlines() return lines diff --git a/setup.py b/setup.py index c20f2fd..2a74859 100755 --- a/setup.py +++ b/setup.py @@ -17,6 +17,7 @@ license="MIT", install_requires=[ "Flask>=1.0.2", + "importlib_resources ; python_version<'3.9'" ], classifiers=[ "Programming Language :: Python :: 3", diff --git a/tests/generator/test_generator.py b/tests/generator/test_generator.py index 1c08061..dbbbca2 100644 --- a/tests/generator/test_generator.py +++ b/tests/generator/test_generator.py @@ -1,3 +1,4 @@ +from pathlib import Path from unittest import TestCase from unittest.mock import MagicMock, patch, mock_open @@ -16,13 +17,13 @@ def test_generate_raise_NotImplementedError(self): with self.assertRaises(NotImplementedError): self.generator.generate() - @patch("flask_seeder.generator.pkg_resources") + @patch("flask_seeder.generator.importlib_resources") def test_resource_path(self, m_pkg): resource_path("test") - m_pkg.resource_filename.assert_called_once() + m_pkg.files.assert_called_once() - @patch("flask_seeder.generator.resource_path", return_value="test") + @patch("flask_seeder.generator.resource_path", return_value=Path("test")) @patch("flask_seeder.generator.open", mock_open(read_data=MOCK_CONTENTS)) def test_read_resource_return_contents_as_list(self, m_open): expected = [