Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 11 additions & 5 deletions flask_seeder/generator.py
Original file line number Diff line number Diff line change
@@ -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

Expand All @@ -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
Expand All @@ -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

Expand Down
1 change: 1 addition & 0 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
license="MIT",
install_requires=[
"Flask>=1.0.2",
"importlib_resources ; python_version<'3.9'"
],
classifiers=[
"Programming Language :: Python :: 3",
Expand Down
7 changes: 4 additions & 3 deletions tests/generator/test_generator.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from pathlib import Path
from unittest import TestCase
from unittest.mock import MagicMock, patch, mock_open

Expand All @@ -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 = [
Expand Down